Java三大集合 1

2021-08-10 13:20:49 字數 2764 閱讀 2105

一. set集合

1.hashset集合,linkedhashset集合

@test

public

void test01()

}

2.treeset集合

@test

public

void

test02()

});treeset2.add(new entity(1));

treeset.add(1);

treeset.add(2);

//第乙個元素

treeset.first();

//最後乙個元素

treeset.last();

//元素小於2的子集

treeset.headset(2);

//元素大於等於2的子集

treeset.tailset(2);

//元素大於等於1,小於2的子集

treeset.subset(1, 2);

}

public

class entity

}

3.enumset集合

public void test03()
4.集合總結

效能:enumset > hashset = linkedhashset > treeset

特點總結:

(1)hashset:新增,刪除操作

(2)linkedhashset:遍歷

(3)treeset:排序

(4)enumset:列舉值

四種集合都是執行緒不安全

二. list集合

1.arraylist集合,vector集合

@test

public

void test01()

2.stack集合

public

void test02()

3.linkedlist集合,arraydeque集合

@test

public

void test03()

4.集合總結

(1) 實現方式:vector,stack,arraylist,arraydeque採用陣列。linkedlist採用雙向鍊錶。

(2) 執行緒安全:vector,stack安全。arraylist,arraydeque,linkedlist不安全。

(3) 特點總結:stack集合可以當棧。arraydeque集合,linkedlist集合可以當雙向佇列。

(4)時間複雜度:陣列集合查改遍歷效率高。鍊錶集合增刪效率高。

(5)空間複雜度:陣列集合預留空間容量。鍊錶集合元素本事消耗空間。

(6)遍歷方式:

(陣列集合)for>iterator>>foreach>>(鍊錶集合)iterator>foreach>>>for

三.map集合

1.hashmap集合,hashtable集合,linkedhashmap集合

@test

public

void test01()

//遍歷集合

for(integer key : map

.keyset())

map2.put(null,null);

}

2.treemap集合

@test

public

void test02()

});bean b1 = new bean(3);

bean b2 = new bean(2);

map.put(b1,"b1");

map.put(b2, null);

//獲取key的各種方法

map.firstkey();

map.lowerkey(b2);

//獲取鍵值對的各種方法

entryentry = map.lastentry();

entry = map.ceilingentry(b1);

}class

bean

implements

comparable

@override

public

int compareto(bean bean)

}

3.weakhashmap集合

@test

public

void test03()

system.out.println(map);

}

4.identityhashmap集合

@test

public

void test04()

system.out.println(map);

}

5.enummap集合

@test

public

void test05()

四.集合列表

Java三大集合類總結

1.所有常用集合類特性比較 集合類 常用具體實現類 是否有序 執行緒安全 元素可否為 null 元素是否可重複 操作效率 底層 實現 list arraylist linkedlist vector 都有序,可以使用 get index 方法取值 非安全非安全安全 都允許都可重 查詢快,增刪慢 增刪...

三大集合Map List Set

1.list是有序集合 map和set是無序集合。2.list和set是collection介面的子介面 map是乙個介面。3.list允許有重複物件 set不允許有重複物件 map可以有重複值但是鍵不可重複。4.list可以有多個null set最多隻允許有乙個null map可以有多個null值...

JAVA中的三大集合框架

1 list 實現的超級父類介面 collection 2 了解 arraylist類 a 定義的格式 arraylist 具體型別 集合名 new arraylist 具體型別 b 資料的儲存方式 底層還是用陣列儲存 c 物件的型別 在 list 集合中,如果定義集合物件時沒有定義定義物件的型別,...