日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

集合(collection)

發布時間:2023/12/20 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 集合(collection) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

使用數組存放數據的弊端:長度不可變,而集合可以動態的添加值

?

java集合類不僅可以存儲數量不等的多個對象,還可以保存具有映射關系的關聯數組

?

?

?

/*
* 1.存儲對象可以考慮:①數組 ②集合
* 2.數組存儲對象的特點:Student[] stu = new Student[20]; stu[0] = new Student();....
* >弊端:①一旦創建,其長度不可變。②真實的數組存放的對象的個數是不可知。
* 3.集合
* Collection接口
*    |------List接口:存儲有序的,可以重復的元素
*      |------ArrayList(主要的實現類)、LinkedList(對于頻繁的插入、刪除操作)、Vector(古老的實現類、線程安全的)
*    |------Set接口:存儲無序的,不可重復的元素
*      |------HashSet、LinkedHashSet、TreeSet
* Map接口:存儲“鍵-值”對的數據
*    |-----HashMap、LinkedHashMap、TreeMap、Hashtable(子類:Properties)
*/

?

?

java集合有Collection和Map集合兩種

Collection接口:

  Set集合:元素之間無序,不可重復

  List集合:元素有序、可以重復

Map集合:具有映射關系的鍵值對

?

Collection的繼承體系:

?

?Map集合的繼承體系

Collection接口:

Collection接口是List、Set、Queue接口的父接口

在java5之前,java集合會丟失容器中所有對象的數據類型,把所有的對象都當做Object對象,java5之后,增加了泛型,java集合可以記住容器中的對象的數據類型

?

List接口的實現類之一:ArrayList

?

使用Iterator接口遍歷集合中的元素,還可以使用增強for進行遍歷

?

?

List接口中的元素都有一個整數序號來記錄元素的位置,可以根據序號來獲取元素

List接口的常用實現類:ArrayList、LinkedList、Vector

?

?ArrayList是List接口的典型實現類,本質上,ArrayList是對象引用的一個變長數組

ArrayList是線程不安全的,而Vector是線程安全的,即使要保證List是安全的,也不用Vector

Arrays.asList()方法返回的List集合是一個固定長度的List集合,不是ArrayList實例,也不是Vector的實例

?

?

1 public void collection1() { 2 Collection coll = new ArrayList(); 3 // 1.size():返回集合中元素的個數 4 System.out.println(coll.size()); 5 // 2.add(Object obj):向集合中添加一個元素 6 coll.add(123); 7 coll.add("AA"); 8 coll.add(new Date()); 9 coll.add("BB"); 10 System.out.println(coll.size()); 11 // 3.addAll(Collection coll):將形參coll中包含的所有元素添加到當前集合中 12 Collection coll1 = Arrays.asList(1, 2, 3); 13 coll.addAll(coll1); 14 System.out.println(coll.size()); 15 // 查看集合元素 16 System.out.println(coll); 17 // 4.isEmpty():判斷集合是否為空 18 System.out.println(coll.isEmpty()); 19 // 5.clear():清空集合元素 20 coll.clear(); 21 System.out.println(coll.isEmpty()); 22 } 1 public void collection2() { 2 Collection coll = new ArrayList(); 3 coll.add(123); 4 coll.add(new String("AA")); 5 coll.add(new Date()); 6 coll.add("BB"); 7 // Person p = new Person("MM",23); 8 coll.add(new Person("MM", 23)); 9 System.out.println(coll); 10 // 6.contains(Object obj):判斷集合中是否包含指定的obj元素。如果包含,返回true,反之返回false 11 // 判斷的依據:根據元素所在的類的equals()方法進行判斷 12 // 明確:如果存入集合中的元素是自定義類的對象。要求:自定義類要重寫equals()方法! 13 boolean b1 = coll.contains(123); 14 b1 = coll.contains(new String("AA")); 15 System.out.println(b1); 16 boolean b2 = coll.contains(new Person("MM", 23)); 17 System.out.println(b2); 18 // 7.containsAll(Collection coll):判斷當前集合中是否包含coll中所有的元素 19 Collection coll1 = new ArrayList(); 20 coll1.add(123); 21 coll1.add(new String("AA")); 22 23 boolean b3 = coll.containsAll(coll1); 24 System.out.println("#" + b3); 25 coll1.add(456); 26 // 8.retainAll(Collection coll):求當前集合與coll的共有的元素,返回給當前集合 27 coll.retainAll(coll1); 28 System.out.println(coll); 29 // 9.remove(Object obj):刪除集合中的obj元素。若刪除成功,返回true。否則,返回false 30 boolean b4 = coll.remove("BB"); 31 System.out.println(b4); 32 33 } 1 public void collection3() { 2 Collection coll = new ArrayList(); 3 coll.add(123); 4 coll.add(new String("AA")); 5 coll.add(new Date()); 6 coll.add("BB"); 7 coll.add(new Person("MM", 23)); 8 9 Collection coll1 = new ArrayList(); 10 coll1.add(123); 11 coll1.add(new String("AA")); 12 // 10.removeAll(Collection coll):從當前集合中刪除包含在coll中的元素。 13 coll.removeAll(coll1); 14 System.out.println(coll); 15 //11.equals(Object obj):判斷集合中的所有元素是否完全相同 16 Collection coll2 = new ArrayList(); 17 coll2.add(123); 18 coll2.add(new String("AA1")); 19 System.out.println(coll1.equals(coll2)); 20 //12.hashCode(): 21 System.out.println(coll.hashCode()); 22 System.out.println(); 23 //13.toArray() :將集合轉化為數組 24 Object[] obj = coll.toArray(); 25 for(int i = 0;i < obj.length;i++){ 26 System.out.println(obj[i]); 27 } 28 System.out.println(); 29 //14.iterator():返回一個Iterator接口實現類的對象,進而實現集合的遍歷! 30 Iterator iterator = coll.iterator(); 31 //方式一:不用 32 /*System.out.println(iterator.next()); 33 System.out.println(iterator.next()); 34 System.out.println(iterator.next());*/ 35 //方式二:不用 36 // for(int i = 0;i < coll.size();i++){ 37 // System.out.println(iterator.next()); 38 // } 39 //方式三:使用 40 while(iterator.hasNext()){ 41 System.out.println(iterator.next()); 42 } 43 } 1 @Test 2 public void test(){ 3 String[] str = new String[]{"AA","BB","DD"}; 4 for(String s : str){ 5 s = "MM";//此處的s是新定義的局部變量,其值的修改不會對str本身造成影響。 6 System.out.println(s); 7 } 8 9 for(int i = 0;i < str.length;i++){ 10 System.out.println(str[i]); 11 } 12 } 13 @Test 14 public void testFor2(){ 15 String[] str = new String[]{"AA","BB","DD"}; 16 for(int i = 0;i < str.length;i++){ 17 str[i] = i + ""; 18 } 19 20 for(int i = 0;i < str.length;i++){ 21 System.out.println(str[i]); 22 } 23 }

?

?

?

List接口的實現類之二:LinkedList

對于頻繁的插入、刪除元素的操作,建議使用LinkedList,效率較高

?

List接口的實現類之三:Vector

Vector大多數操作和ArrayList差不多,但是Vector是線程安全的,當插入、刪除頻繁的時候,使用LinkedList,Vector總是比ArrayList慢

?

?

?

?

?

Iterator和ListIterator的區別:

兩者都有next()和hasNext(),可以實現向后遍歷,但是ListIterator有previous()和hasPrevious()方法,即可以實現向前遍歷

ListIterator可以定位當前位置,nextIndex()和previous()可以實現

ListIterator有add()方法,可以向list集合中添加數據

都可以實現刪除操作,但是ListIterator可以實現對對象的修改,set()可以實現,Iterator僅能遍歷,不能修改

?

?Set集合不允許包含兩個相同的元素,如果試圖把兩個相同的元素加入到同一個Set集合中,則添加操作失敗

Set集合判斷兩個對象是否相等不是使用==,而是使用equals()方法

?

HashSet使用hash算法進行存儲集合中的元素,因此有很好的存取和查找功能

HastSet的一下特點:

  不能保證元素的順序排列

  HashSet不是線程安全的

  集合元素可以是null

當向HashSet集合中添加一個元素時候,會調用hashCode()方法得到該對象的hashCode值,根據根據該值決定元素的存放位置

HashSet判斷兩個元素相等的的標準,兩個對象的hashCode()是否相等,并且equals()方法是否相等

?

hashCode()方法::

如果元素的equals()方法返回true,但是他們的hashCode()值不同,hashSet會將他們存放在不同的位置,依然可以添加成功

對于存放在集合中的對象,對象的類一定要重寫equals()和hashCode()方法,以實現對象的相等原則

?

Set實現類之二:LinkedHashSet

LinkedHashSset根據元素的hasCode值來決定元素的存儲位置,但是它同時使用鏈表維護次序,這使得元素看起來是有序的

LinkedHashSet的插入性能要低于HashSet,但在迭代訪問Set里面的元素是有很好的性能

LinkedHashSet不允許元素的重復

?

Set實現類之三:TreeSset

TreeSet是SortedSet的實現類,TreeSet可以保證元素處于排序狀態

?

TreeSet兩種排序方式:自然排序和定制排序,默認情況下,TreeSet采用自然排序

?

排序---自然排序

自然排序:TreeSet會調用集合元素的compareTo(Object object)方法來比較元素之間的大小關系,然后將元素按升序排列

如果試圖把一個元素添加到TreeSet中,則該對象必須實現Comparable接口

?

實現Comparable接口必須實現compareTo(Object object),兩個對象即通過這個方法進行比較

?

Comparable的典型實現

BigDecimal、BigInteger以及所有的數值類型對應的包裝類型,按對應的數值大小進行比較

Character:按字符的Unicode值進行比較

Boolean:true對應的包裝類實例大于false包裝類對應的實例

String:按字符對應的Unicode值進行比較

Date、Time:后面的時間、日期比前面的時間、日期大

?

向TreeSet中添加一個元素,只有第一個不需要使用compareTo()方法,后面的都要調用該方法

因為只有相同類的兩個實例才會比較大小,所以向TreeSet中添加的應該是同一個類的對象

對于TreeSet集合而言,它判斷兩個對象是否相等的唯一標準是兩個對象通過compareTo方法的返回值相等

當需要把一個對象放入TreeSet中,重寫對象對應的equals方法時候,應保證該方法與compareTo方法有一致的結果

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

轉載于:https://www.cnblogs.com/lzb0803/p/8963131.html

總結

以上是生活随笔為你收集整理的集合(collection)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。