核心技術java基礎_JAVA核心技術I---JAVA基礎知識(集合set)

一:集合了解

(一)確定性,互異性,無序性

確定性:對任意對象都能判定其是否屬于某一個集合

互異性:集合內每個元素都是無差異的,注意是內容差異

無序性:集合內的順序無關

(二)集合接口HashSet,TreeSet,LinkedHashSet

–HashSet (基于散列函數的集合,無序,不支持同步)

–TreeSet (基于樹結構的集合,可排序的,不支持同步)

–LinkedHashSet(基于散列函數和雙向鏈表的集合,可排序的,不支持同步

3e01e692354509759451a02b36498d23.png

二:HashSet

(一)基礎方法

–基于HashMap實現的,可以容納null元素, 不支持同步

Set s= Collections.synchronizedSet(newHashSet(...));

–add 添加一個元素

–clear 清除整個HashSet

–contains 判定是否包含一個元素

–remove 刪除一個元素 size 大小

–retainAll 計算兩個集合交集

(二)HashSet實現

HashSet hs = new HashSet(); //<>是泛型編程,類似于C++模板

hs.add(null);

hs.add(10000);

hs.add(22);

hs.add(1010);

hs.add(50001010);

hs.add(101035);

hs.add(3);

System.out.println(hs.size());if(!hs.contains(6)) {

hs.add(6);

}

System.out.println(hs.size());

hs.remove(4);  //存在,則刪除,不存在,則不操作for(Integer item : hs) {

System.out.println(item);

}

7

8

null  //無序性

10000

1010

3

22

6

50001010

101035

(三)性能測試:因為無序性,無索引操作。for效率高

public static void trverseByIterator(HashSeths) {//使用迭代器遍歷

System.out.println("==========迭代器遍歷===========");long startTime = System.nanoTime(); //獲取開始時間,以納秒為單位返回正在運行的Java虛擬機的高分辨率時間源的當前值。

Iterator iter = hs.iterator(); //獲取迭代指針

while(iter.hasNext()) {

iter.next();

}long endTime =System.nanoTime();long duration = endTime-startTime;

System.out.println("iterator使用納秒:"+duration);

}public static void trverseByFor(HashSeths) {//使用迭代器遍歷

System.out.println("==========for索引遍歷===========");long startTime = System.nanoTime(); //獲取開始時間,以納秒為單位返回正在運行的Java虛擬機的高分辨率時間源的當前值。

for(Integer item : hs) {

;

}long endTime = System.nanoTime(); //獲取開始時間,以納秒為單位返回正在運行的Java虛擬機的高分辨率時間源的當前值。

long duration = endTime-startTime;

System.out.println("for使用納秒:"+duration);

}

==========迭代器遍歷===========iterator使用納秒:5738665

==========for索引遍歷===========for使用納秒:2721950

(四)retainAll交集測試

//測試交集

HashSet hs1 = new HashSet();

HashSet hs2 = new HashSet();

hs1.add("a");

hs1.add("b");

hs1.add("c");

hs2.add("c");

hs2.add("d");

hs2.add("e");

hs1.retainAll(hs2);//將交集保存在hs1中

for(String item : hs1) {

System.out.println(item);

}

c

三:LinkedHashSet(與HashSet一致)

–繼承HashSet,也是基于HashMap實現的,可以容納null元素,按照插入順序有序

–不支持同步

Set s= Collections.synchronizedSet(newLinkedHashSet(...));

–方法和HashSet基本一致

add, clear, contains, remove, size

–通過一個雙向鏈表維護插入順序

四:TreeSet

(一)基本方法

–基于TreeMap實現的,不可以容納null元素,不支持同步

SortedSet s= Collections.synchronizedSortedSet(newTreeSet(...));

–add 添加一個元素

–clear 清除整個TreeSe

–contains 判定是否包含一個元素

–remove 刪除一個元素 size 大小

–根據compareTo方法或指定Comparator排序

(二)實現(有序,會自動排序,紅黑樹)

TreeSet ts = new TreeSet(); //<>是泛型編程,類似于C++模板

ts.add(1000);

ts.add(15300);

ts.add(100);

ts.add(3);

ts.add(566000);if(!ts.contains(4)) {

ts.add(4);

}

for(Integer item : ts) {

System.out.println(item);;

}

4

100

1000

15300

566000

(三)性能測試:for更加高效

==========迭代器遍歷===========iterator使用納秒:9246423

==========for索引遍歷===========for使用納秒:3366874

五:HashSet, LinkedHashSet, TreeSet對象比較(元素重復)《重點》

(一)HashSet和LinkedHashSet判定元素重復的原則

–判定兩個元素的hashCode返回值是否相同,若不同,返回false

–若兩者hashCode相同,判定equals方法,若不同,返回false;否則返回true。

–hashCode和equals方法是所有類都有的,因為Object類有

比較之前會先調用hashCode,之后是equals方法

1.正常執行,含重復

classDog{intage;public Dog(inta) {this.age=a;

}

}public classCompareTest {public static voidmain(String[] args) {

Dog d1=new Dog(10);

Dog d2=new Dog(10);

HashSet hs=new HashSet();

hs.add(new Dog(10));

hs.add(new Dog(1));

hs.add(new Dog(3));

hs.add(new Dog(10));

hs.add(new Dog(10));

System.out.println(hs.size());

}

}

5

Dog類本身沒有hashCode方法,繼承于Object,而Object類的hashCOde會返回對象信息和內存地址經過運算后的一個值。兩個不同對象,其值必然不一致

2.實現對象的hashCode方法和equals方法實現去重

import java.util.*;classDog{intage;public Dog(inta) {this.age=a;

}public intgetAge() {return this.age;

}public inthashCode() {

System.out.println("hashCode exec...");return this.age;

}publicboolean equals(Object obj2) {

System.out.println("equals exec...");if(0==this.age-((Dog)obj2).getAge())return true;else

return false;

}

}public classCompareTest {public static voidmain(String[] args) {

Dog d1=new Dog(10);

Dog d2=new Dog(10);

HashSet hs=new HashSet();

hs.add(new Dog(10));

hs.add(new Dog(1));

hs.add(new Dog(3));

hs.add(new Dog(10));

hs.add(new Dog(10));

System.out.println(hs.size());

}

}

hashCode exec...

hashCode exec...

hashCode exec...

hashCode exec...

equals exec...

hashCode exec...

equals exec...3  //去重實現

先執行hashCode,只有hashCode通過,才會執行equals方法

publicString toString() {

System.out.println("toString exec...");return age+"";

}

要保持equals,hashCode和toString三位一體。都應該各自相同

(二) TreeSet去重

添加到TreeSet,需要實現Comparable接口,即實現compareTo方法

與hashCode和equals無關,只與compareTo有關

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

import java.util.*;classDog implements Comparable{intage;public Dog(inta) {this.age=a;

}public intgetAge() {return this.age;

}public inthashCode() {

System.out.println("hashCode exec...");return this.age;

}publicboolean equals(Object obj2) {

System.out.println("equals exec...");if(0==this.age-((Dog)obj2).getAge())return true;else

return false;

}publicString toString() {

System.out.println("toString exec...");return age+"";

}public intcompareTo(Object obj2) {

System.out.println("compareTo exec...");return this.age -((Dog)obj2).getAge();

}

}public classCompareTest {public static voidmain(String[] args) {

Dog d1=new Dog(10);

Dog d2=new Dog(10);

TreeSet hs=new TreeSet();

hs.add(new Dog(10));

hs.add(new Dog(1));

hs.add(new Dog(3));

hs.add(new Dog(10));

hs.add(new Dog(10));

System.out.println(hs.size());

}

}

View Code

compareTo exec...

compareTo exec...

compareTo exec...

compareTo exec...

compareTo exec...

compareTo exec...

compareTo exec...

compareTo exec...3

可以知道,去重和hashCode與equals無關,不執行。而是直接去找compareTo方法

六:總結

(一)HashSet, LinkedHashSet, TreeSet的元素都只能是對象

會進行自動裝箱

(二)HashSet和LinkedHashSet判定元素重復的原則《重點》

–判定兩個元素的hashCode返回值是否相同,若不同,返回false

–若兩者hashCode相同,判定equals方法,若不同,返回false;否則返回true。

–hashCode和equals方法是所有類都有的,因為Object類有

(三)TreeSet判定元素重復的原則《重點》

–需要元素繼承自Comparable接口

–比較兩個元素的compareTo方法

(四)注意:對于基本類型的包裝類。本來就實現了compareTo接口和其他比較方法,所以HashSet,LinkedHashSet,TreeSet中對于包裝類是默認去重的

c87bb14eea71500140e9d0e14c223e1e.png

cb2f1a26d66cacd6d8ad1953727ac81e.png

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/392410.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/392410.shtml
英文地址,請注明出處:http://en.pswp.cn/news/392410.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

nba數據庫統計_NBA板塊的價值-從統計學上講

nba數據庫統計The idea is not to block every shot. The idea is to make your opponent believe that you might block every shot. — Bill Russel這個想法不是要阻止每一個鏡頭。 這個想法是讓你的對手相信你可能會阻擋每一個投籃。 —比爾羅素 The block in basketball ha…

leetcode 330. 按要求補齊數組(貪心算法)

給定一個已排序的正整數數組 nums&#xff0c;和一個正整數 n 。從 [1, n] 區間內選取任意個數字補充到 nums 中&#xff0c;使得 [1, n] 區間內的任何數字都可以用 nums 中某幾個數字的和來表示。請輸出滿足上述要求的最少需要補充的數字個數。 示例 1: 輸入: nums [1,3], …

【煉數成金 NOSQL引航 三】 Redis使用場景與案例分析

驗證redis的主從復制&#xff0c;將實驗過程抓圖 復制配置文件 更改slave的端口 和相關master配置 主從復制測試 研究在OAuth中的“一次數”nonce有什么用途&#xff1f;怎樣使用&#xff1f;以此熟悉OAuth的全流程 nonce &#xff0c;一個隨機的混淆字符串&#xff0c;僅僅被…

SQL Server需要監控哪些計數器 ---指尖流淌

http://www.cnblogs.com/zhijianliutang/p/4174697.html轉載于:https://www.cnblogs.com/zengkefu/p/7044095.html

akka 簡介_Akka HTTP路由簡介

akka 簡介by Miguel Lopez由Miguel Lopez Akka HTTP路由簡介 (An introduction to Akka HTTP routing) Akka HTTP’s routing DSL might seem complicated at first, but once you get the hang of it you’ll see how powerful it is.Akka HTTP的路由DSL乍一看似乎很復雜&…

leetcode 1046. 最后一塊石頭的重量(堆)

有一堆石頭&#xff0c;每塊石頭的重量都是正整數。 每一回合&#xff0c;從中選出兩塊 最重的 石頭&#xff0c;然后將它們一起粉碎。假設石頭的重量分別為 x 和 y&#xff0c;且 x < y。那么粉碎的可能結果如下&#xff1a; 如果 x y&#xff0c;那么兩塊石頭都會被完全…

java2d方法_Java SunGraphics2D.fillRect方法代碼示例

import sun.java2d.SunGraphics2D; //導入方法依賴的package包/類/*** Return a non-accelerated BufferedImage of the requested type with the* indicated subimage of the original image located at 0,0 in the new image.* If a bgColor is supplied, composite the orig…

js建立excel表格_建立Excel足球聯賽表格-傳統vs動態數組方法

js建立excel表格介紹 (Introduction) I am going to show you the different ways you can build a football league table in Excel. Some of the methods are old school but others utilise Excel’s new capabilities.我將向您展示在Excel中建立足球聯賽表格的不同方法。 其…

postman+newman生成html報告

作為測試菜鳥,在學習postmannewman的使用過程中真的是頗費周折......沒辦法技術太菜,只能多學習. postman的下載安裝不多言說,下載地址:https://www.getpostman.com/downloads/ newman的安裝過程: 1.首先需要安裝node.js,可以去官網下載,地址:https://nodejs.org/en/#download …

java jdk1.9新特性_JDK1.9-新特性

1. Java平臺級模塊系統該特性使Java9最大的一個特性&#xff0c;Java提供該功能的主要的動機在于&#xff0c;減少內存的開銷&#xff0c;JVM啟動的時候&#xff0c;至少會有30~60MB的內存加載&#xff0c;主要原因是JVM需要加載rt.jar&#xff0c;不管其中的類是否被classload…

如何在10分鐘內讓Redux發揮作用

Hi everyone ??大家好?? For a while now I’ve been hearing my friends and colleagues complaining about how hard it was to get into Redux.一段時間以來&#xff0c;我一直在聽我的朋友和同事抱怨進入Redux有多困難。 I run a freeCodeCamp Study Group in the So…

兩個鏈接合并_如何找到兩個鏈接列表的合并點

兩個鏈接合并了解問題 (Understand the Problem) We are given two singly linked lists and we have to find the point at which they merge.我們給了兩個單鏈表&#xff0c;我們必須找到它們合并的點。 [SLL 1] 1--->3--->5 \ …

安裝veket到移動硬盤NTFS分區

如果你已經看過《手動安裝veket到硬盤》和《簡單的將veket安裝到U盤的方法》兩篇文章并且安裝成功的話&#xff0c;說明不適用本文的安裝環境&#xff0c;就不用往下看了。 《手動安裝veket到硬盤》一文采用grub4dos來引導硬盤上的veket&#xff0c;主要是用來在本機已安裝Wind…

簡書使用小技巧

1、不同字體  在 設置->基礎設置->富文本 模式下可以實現 2、添加圖片&#xff0c;讓文章更生動 3、添加代碼框 &#xff01;注意&#xff1a;設置為Markdown模式后&#xff0c;只對新創建的文章起作用。轉載于:https://www.cnblogs.com/HMJ-29/p/7049540.html

掩碼 項目編碼_每天進行20天的編碼項目

掩碼 項目編碼by Angela He通過何安佳 每天進行20天的編碼項目 (A coding project a day for 20 days) 我如何在20天內自學Web開發 (How I taught myself web development in 20 days) It was the first day of winter break for Stanford students. Back at home, I opened a…

java循環一年月份天數和_javawhile循環編寫輸入某年某月某日,判斷這一天是這一年的第幾…...

該樓層疑似違規已被系統折疊 隱藏此樓查看此樓public class ZuoYe9 {public static void main(String[] args) {int days0; //存儲變量這一年的第幾天//1.輸入年&#xff0c;月&#xff0c;日Scanner inputnew Scanner(System.in);System.out.println("請輸入年份&#xf…

leetcode 605. 種花問題(貪心算法)

假設你有一個很長的花壇&#xff0c;一部分地塊種植了花&#xff0c;另一部分卻沒有。可是&#xff0c;花卉不能種植在相鄰的地塊上&#xff0c;它們會爭奪水源&#xff0c;兩者都會死去。 給定一個花壇&#xff08;表示為一個數組包含0和1&#xff0c;其中0表示沒種植花&…

工程師的成熟模型_數據工程師的成熟度

工程師的成熟模型數據科學與機器學習 (DATA SCIENCE AND MACHINE LEARNING) What does a data engineer do?數據工程師做什么&#xff1f; Let’s start with three big wars that we need to understand before understanding what a data engineer does.讓我們從理解數據工…

杭電2064

此題是一道簡單的遞歸 此題是一道遞歸運算題&#xff0c;這題又是一道漢諾塔問題&#xff01;&#xff01;&#xff01;只要了解其規律&#xff0c;呵呵&#xff0c;你就可以很快AC了&#xff01;&#xff01; 這是一般的漢諾塔問題的解題方法照片&#xff01;&#xff01;&…

/ ./ ../ 的區別

/ 根目錄 &#xff08;絕對路徑&#xff09; ./ 當前目錄 ../父級目錄 &#xff08;相對路徑&#xff09; ./home是當前目錄下的一個叫home的目錄/home是絕對路徑的/home就是根下的home目錄轉載于:https://www.cnblogs.com/sjd1118/p/7055475.html