Java集合之collection體系

Java集合之collection體系大致內容爲下圖


1.collection接口

(1).集合的方法

a.增加

boolean add(XXX)將某元素放到集合中。(添加成功返回值爲true)

boolean addAll(xxx)把一個集合的內容放到另一個集合中。(添加成功返回值爲true)

	public static void main(String[] args) {
		//將字符串存到集合中
		Collection col = new ArrayList();
		col.add("1");
		//將col集合的內容放到coll中
		Collection col1 = new ArrayList();
		col1.addAll(col);
	}
b.刪除

void clear(),刪除集合中所有元素

boolean remove(xxx);刪除集合中指定元素(刪除什麼傳入什麼,刪除成功返回true)
boolean removeAll(xxx),從一個集合中刪除傳入集合的內容(刪除成功返回true)

	public static void main(String[] args) {
		//將字符串存到集合中
		Collection col = new ArrayList();
		col.add("1");
		//將col集合的內容放到coll中
		Collection col1 = new ArrayList();
		col1.add("2");
		col1.addAll(col);
		//從col中刪除"1"
		col.remove("1");
		//在col1中刪除col的內容
		col1.removeAll(col);
		//清空col1
		col1.clear();
	}

c.判斷

boolean contains(xxx)是否包含傳入的該元素

boolean containsAll(xxx)是否包含傳入集合元素

boolean isEmpty()該集合是否爲空

	public static void main(String[] args) {
		//將字符串存到集合中
		Collection col = new ArrayList();
		col.add("1");
		boolean contains = col.contains("1");//true
		boolean empty = col.isEmpty();//false
		Collection col1 = new ArrayList();
		col1.addAll(col);
		boolean con = col1.containsAll(col);//true
	}
f.數組的轉換

Object[] toArray(),以數組的形式返回該集合中的所有元素

	public static void main(String[] args) {
		//將字符串存到集合中
		Collection col = new ArrayList();
		col.add("1");
		col.add("2");
		Object[] array = col.toArray();//["1","2"]
	}

2.list

有序的 collection(也稱爲序列)。此接口的用戶可以對列表中每個元素的插入位置進行精確地控制。用戶可以根據元素的整數索引(在列表中的位置)訪問元素,並搜索列表中的元素。

a.添加

add(),addAll(),用法和collection接口中的方法一致,不過元素是被有序的添加到了集合的尾部,

boolean add(index,element);精確插入

	public static void main(String[] args) {
		//將字符串存到集合中
		List list = new ArrayList();
		list.add("1");
		list.add("2");//將"2"添加到"1"後面
		list.add(1, "3");//將"3"添加到二者之間
		List list1 = new ArrayList();
		list1.add("0");
		list.addAll(list1);//將list1集合添加到list尾部
	}

b.獲取

Object get(index);取出集合中索引爲index的元素。

	public static void main(String[] args) {
		//將字符串存到集合中
		List list = new ArrayList();
		list.add("1");
		list.add("2");//將"2"添加到"1"後面
		Object object = list.get(0);//"1"
	}

e.size();返回集合中元素的個數

	public static void main(String[] args) {
		//將字符串存到集合中
		List list = new ArrayList();
		list.add("1");
		list.add("2");//將"2"添加到"1"後面
		list.size();
	}

list.size()返回2

f.刪除

三個基本的刪除方法與collection一致

多出一個remove(index);

	public static void main(String[] args) {
		//將字符串存到集合中
		List list = new ArrayList();
		list.add("1");
		list.add("2");//將"2"添加到"1"後面
		list.remove(0);//刪除"1"
	}

3.ArrayList

ArrayList中的元素和可以重複是有序的集合,長度不固定。

 不是線程安全的。

 效率高(查的效率高,因爲可使用索引)

可存null。

其常用API與list接口基本一致

4.LinkedList

List 接口的鏈接列表實現。實現所有可選的列表操作,並且允許所有元素(包括null)。除了實現List接口外,LinkedList 類還爲在列表的開頭及結尾get、remove和 insert元素提供了統一的命名方法。這些操作允許將鏈接列表用作堆棧、隊列雙端隊列

不是線程安全的。

區別API



	public static void main(String[] args) {
		//將字符串存到集合中
		LinkedList list = new LinkedList();
		list.addFirst(1);
		list.addLast(2);
		list.removeFirst();
		list.removeLast();
		list.add(1);
		list.add(2);
		list.add(3);
		Object pop = list.pop();//1
		list.push(5);//[5,2,3]
	}
5.Vetor

和ArrayList功能類似,最主要的區別就在於vector是線程併發安全的。但是缺點是效率比較低。


6.set

無序,不包含重複元素,最多一個null,方法與collection中基本相同

7.HashSet

無序,不包含重複元素,最多一個null,不是線程安全,方法與collection中基本相同

8.LinkedHashSet

有迭代順序,不包含重複元素,最多一個null,不是線程安全,方法與collection中基本相同

9.TreeSet(可支持自定義排序,如果其所存儲的對象中沒有實現comparable接口)

不可重複,可支持自定義排序,不允許null存在,不是線程安全。



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章