1、collectioncollection
|-list
|-arraylist
|-linkedlist
|-vector
|-stack
|-set
|-hashset
|-treeset
|-linkedhashset
|-queue
collection是所有集合的根介面,jdk 不提供此介面的任何直接實現:它提供更具體的子介面(如 set 和 list)實現。
2、list
list是乙個介面,有序,可以對元素的插入位置進行控制,元素可重複
2.1 arraylist
實現list介面,有序,可重複,實現不同步,即多執行緒操作該arraylist例項會造成資料不統一的現象。底層為陣列
2.2 linkedlistclass
number
implements
runnable catch (interruptedexception e) }}
}public
class
text
extends
number
}
實現list介面,有序,可重複,實現不同步(與arraylist相同),不同在於底層為鍊錶(對兩者不同操作的開銷不同)
2.3 vector
實現list介面,動態陣列,執行緒同步
2.4 stackpublic
class text
}output:
[你好, 哦還有, 哈梅雷, 啊你哈噻有]
[你好, hello, 哈梅雷, 啊你哈噻有]
繼承vector,資料結構中的棧,先進後出
3、setpublic
class text
for(string book : books)
system.out.println("---------");
if(!books.isempty())
system.out.println("---------");
while(!books.isempty())
system.out.println("---------");
if(books.isempty())
}}/*
output:
西遊記紅樓夢
三國演義
水滸傳金瓶梅
---------
foreach後stack不為空
---------
金瓶梅水滸傳
三國演義
紅樓夢西遊記
---------
pop後stack為空
*/
介面,無重複(不存在滿足e1.equals(e2)的元素對),最多包含乙個null元素。
3.1 hashset
實現set介面,無序,實現不同步,底層為hashmap,新增物件時,要重寫hashcode()和equals()方法
3.2 treesetclass tea
public string getcolor()
public
intgetprice()
@override
public
inthashcode()
@override
public
boolean
equals(object obj) else
if (!color.equals(other.color))
return
false;
if (price != other.price)
return
false;
return
true;
}}public
class
text
}}/*output:
綠tea:6塊
綠tea:5塊
白tea:3塊
紅tea:5塊
//去掉equals()或hashcode()後:
白tea:3塊
綠tea:5塊
紅tea:5塊
綠tea:6塊
紅tea:5塊
*/
底層為treemap,實現不同步,可排序但需要有排序規則(string,integer等擁有自己的排序規則,自己寫的類也要自己定義規則)。
排序兩種方案:
1、使用treeset()構造方法,需要新增的元素實現comparable介面
2、使用treeset(comparator<?> comparator)構造方法,自定義乙個比較器class tree implements comparable
public string gettype()
public
intgetage()
@override
public
intcompareto(object obj)
if(tree.age > this.age)
if(tree.age == this.age)
return
0; }
}public
class text
}}/*output:
樸樹: 25歲了
松樹: 25歲了
榆樹: 20歲了
直上春樹: 20歲了
楊樹: 15歲了
*/
4、queueclass tree
public string gettype()
public
intgetage()
}class mycompare implements comparator
if(t1.getage() > t2.getage())
if(t1.getage() == t2.getage())
return
0; }
}public
class text
}}/*output:
樸樹: 25歲了
松樹: 25歲了
榆樹: 20歲了
直上春樹: 20歲了
楊樹: 15歲了
*/
介面,資料結構中的佇列,先進先出,linkedlist實現了deque介面,deque介面繼承queue介面。
java 集合學習之hashMap
1 hashmap類繼承關係 public class hashmapextends abstractmap implements map,cloneable,serializable 存放示意圖 由此可以看出hash值一樣的節點會被存放在同一條鍊錶上,比原始遍歷equals查詢效率高 hash值相...
java學習之單列集合
陣列在儲存資料的時候是有固定長度,而且必須儲存同一種資料型別的資料,集合的出現就是為了補充這個不足的 集合分為單列集合 collection 和雙列集合 map 這兩個只是實現集合的兩個介面 介面的特點是不能用來建立物件,所以在實際的開發中他們只是用來作為父類出現 單列集合 collection 下...
Java學習之集合 四
18.01 集合框架 map集合概述和特點 b map介面和collection介面的不同 1 map是雙列的,collection是單列的 2 map的鍵唯一,collection的子體系set是唯一的 3 map集合的資料結構值針對鍵有效,跟值無關 collection集合的資料結構是針對元素有...