集合知識點總結

2021-09-25 00:00:26 字數 2592 閱讀 5458

1.常用集合介面:

a、collection介面:最基本的集合介面,儲存不唯一,無序的物件,list介面和set介面的父介面;

set set = new treeset<>(new comparator();   list weights = new

arraylist<>();//list是父類,作為乙個介面,也是乙個集合,<>是乙個集合表示的方式。integer泛型

b、list介面:乙個有序、可以重複的集合,常用實現類arraylist和linkedlist;

c、set介面:乙個無序、不可重複的集合,常用實現類hashset、linkedhashset、treeset;

set hashset = new hashset(); 元素無序,不可重複,執行緒不安全,集合元素可以為 null

hashset set = new hashset<>()底層採用鍊錶和雜湊表的演算法,保證元素有序,唯一性(即不可以重複,有序),執行緒不安全

treeset ts = new treeset<>(new comparator() 底層使用紅黑樹演算法,擅長於範圍查詢,元素有序,不可重複,執行緒不安全

d、map介面:key-value的鍵值對,key不允許重複,value可以,key-value通過對映關係關聯,常用實現類hashmap和treemap;

採用雜湊表演算法,key無序且不允許重複,key判斷重複的標準是:key1和key2是否equals為true,並且hashcode相等

maphashmap = new hashmap();

採用紅黑樹演算法,key有序且不允許重複,key判斷重複的標準是:compareto或compare返回值是否為0

4 maptreemap = new treemap();

set和list的區別:

a、set例項儲存是無序的,不重複的資料;list例項儲存的是有序的,可以重複的元素;

b、set檢索效率低下,刪除和插入效率高,刪除和插入不會引起元素位置改變;

c、list可以根據儲存的資料長度自動增長list長度,查詢元素效率高,插入刪除效率低,插入和刪除時會引起其他元素位置改變;

3. map和set的關係:

a、hashmap、hashset 都採雜湊表演算法,treemap、treeset 都採用紅黑樹演算法、linkedhashmap、linkedhashset 都採用雜湊表演算法和紅黑樹演算法;

b、分析set的底層原始碼,set 集合就是由map集合的key組成;

案例:mappoker = new hashmap<>();

//2.建立花色集合和數字集合

list color = new arraylist<>();

list num = new arraylist<>();

collections.addall(color,"紅桃","黑桃","梅花","方塊");

collections.addall(num,"2", "a", "k", "q", "j", "10", "9", "8", "7", "6", "5", "4", "3");

int count = 1;

poker.put(count++,"大王");//返回1

poker.put(count++,"小王");//返回2

for(string c:num)

}//以大小存

//得到set,取出poker集合鍵

setset = poker.keyset();

listlist = new arraylist<>();

list.addall(set);

collections.shuffle(list);//打亂

listdipai = new arraylist<>();

listp1 = new arraylist<>();

listp2 = new arraylist<>();

listp3 = new arraylist<>();

for(int i = 0;i=51)else if(i % 3 ==0)else if(i % 3 ==1) else if(i % 3 ==2)

}collections.sort(p1);

collections.sort(p2);

collections.sort(p3);

arraylistplay1 = new arraylist<>();

arraylistplay2 = new arraylist<>();

arraylistplay3 = new arraylist<>();

arraylistdipai1 = new arraylist<>();

for(integer integer: dipai)//值大小變成牌的值

for(integer integer: p1)

for(integer integer: p2)

for(integer integer: p3)

system.out.println(play1);

system.out.println(play2);

system.out.println(play3);

system.out.println(dipai1);

}

Java集合知識點總結

list有序且允許元素重複。map也屬於集合系統,但和collection介面沒關係。map是key對value的對映集合,其中key列就是乙個集合。key不能重複,但是value可以重複。sortedset和sortedmap介面對元素按指定規則排序,sortedmap是對key列進行排序。has...

集合相關知識點總結

集合 解決 陣列定長 頂層介面 iterator迭代器 collection 單列集合 map 雙列集合 collection 可以存放重複元素的list子介面 arraylist 動態陣列 linkedlist 雙向鍊錶集合 vector 向量 add a 末尾新增 add index,a 向指定...

集合知識點

1.集合框架中包含哪些集合 collection list arraylist linkedlist vector sethashset treeset maphashmap treemap 2.list集合與set集合的特點 list 有序並且允許重複 set 無需並且不允許重複 3.arrayl...