1、集合框架的概述java
Java
容器。.txt
,.jpg
,.avi
,數據庫中)。String[] arr;
int[] arr1;
Object[] arr2
。2、Java集合可分爲 Collection 和 Map 兩種體系數據庫
① Collection
接口:單列集合,定義了存取一組對象的方法的集合 。用來存儲一個一個的對象。設計模式
List
:元素有序、可重複的集合ArrayList
、LinkedList
、Vector
Set
:元素無序、不可重複的集合HashSet
、LinkedHashSet
、TreeSet
② Map
接口:雙列集合,保存具備映射關係「key-value對」的集合。用來存儲一對的數據。數組
HashMap
、LinkedHashMap
、TreeMap
、Hashtable
、Properties
add(Object e)
:將元素e添加到集合coll
中。
size()
:獲取添加的元素的個數。
addAll(Collection coll)
:將coll
集合中的元素添加到當前的集合中。
clear()
:清空集合元素。
isEmpty()
:判斷當前集合是否爲空。框架
public void testCollection(){ Collection coll = new ArrayList(); //add(Object e):將元素e添加到集合coll中 coll.add("AA"); coll.add("BB"); coll.add("CC"); coll.add(123);// 自動裝箱了 coll.add(new Date()); //size():獲取添加的元素的個數 System.out.println(coll.size());// 5 //addAll(Collection coll):將coll集合中的元素添加到當前的集合中 Collection coll2 = new ArrayList(); coll2.add("CC"); coll2.add(123); coll.addAll(coll2); System.out.println(coll.size()); System.out.println(coll); //clear:清空集合元素 coll.clear(); // isEmpty():判斷當前集合是否爲空 System.out.println(coll.isEmpty()); }
contains(Object obj)
:是經過元素的equals方法來判斷是否 是同一個對象。
向Collection
接口的實現類的對象中添加數據obj
時,要求obj
所在類要重寫equals()
。設計
public void testCollection2() { Collection coll = new ArrayList(); coll.add("AA"); coll.add(123); coll.add(new String("Tom")); coll.add(false); Person p = new Person("Jerry", 20); coll.add(p); coll.add(new Person("Snake",20)); //contains():判斷當前集合是否包含Obj // 咱們在判斷時會調用obj對象所在的equals()。 boolean contains = coll.contains("AA"); System.out.println(contains); System.out.println(coll.contains(new String("Tom"))); System.out.println(coll.contains(p)); System.out.println(coll.contains(new Person("Snake", 20))); }
containsAll(Collection c)
:也是調用元素的equals方法來比 較的。拿兩個集合的元素挨個比較。指針
Collection coll1 = Arrays.asList(123,"AA"); System.out.println(coll.containsAll(coll1));// true
remove(Object obj)
:經過元素的equals
方法判斷是不是 要刪除的那個元素。只會刪除找到的第一個元素 。
removeAll(Collection coll)
:取當前集合的差集 七、取兩個集合的交集。code
public void test3() { // remove(Object obj) Collection coll = new ArrayList(); coll.add("AA"); coll.add(123); coll.add(false); coll.add(new String("Tom")); coll.add(new Person("Jerry", 20)); boolean remove = coll.remove(123); coll.remove(new Person("Jerry", 20)); System.out.println(remove);// true System.out.println(coll);// [AA, false, Tom] // removeAll(Collection coll1):從當前集合中移除coll1中全部的元素 Collection coll1 = Arrays.asList("AA",new String("Tom")); coll.removeAll(coll1); System.out.println(coll);// [false] }
retainAll(Collection coll)
:獲取當前集合和coll
集合的交集,並返回給當前集合。
equals(Object obj)
:集合是否相等 。要向返回true,須要當前集合和形參集合的元素都相同。對象
public void test4() { Collection coll = new ArrayList(); coll.add("AA"); coll.add(123); coll.add(false); coll.add(new String("Tom")); coll.add(new Person("Jerry", 20)); // coll.retainAll(Collection coll):獲取當前集合和coll1集合的交集,並返回給當前集合。 Collection coll1 = Arrays.asList(123,"AA","Snake"); coll.retainAll(coll1); System.out.println(coll);// [AA, 123] // equals(Object obj):要向返回true,須要當前集合和形參集合的元素都相同。 Collection coll2 = new ArrayList(); coll2.add("AA"); coll2.add(123); System.out.println(coll.equals(coll2));// true }
hashCode():
返回當前對象的哈希值。繼承
toArray()
Arrays
類的靜態方法asList()
public void test5() { Collection coll = new ArrayList(); coll.add("AA"); coll.add(123); coll.add(false); coll.add(new String("Tom")); coll.add(new Person("Jerry", 20)); // hashCode():返回當前對象的哈希值 System.out.println(coll.hashCode()); // 集合---> 數組: toArray() Object[] array = coll.toArray(); for (int i = 0; i < array.length; i++) { System.out.println(array[i]); } // 擴展:數組---> 集合:調用Arrays類的靜態方法asList() List<String> stringList = Arrays.asList(new String[]{"AA", "BB", "CC"}); System.out.println(stringList);java }
GOF
給迭代器模式的定義爲:提供一種方法訪問一個容器(container
)對象中各個元 素,而又不需暴露該對象的內部細節。迭代器模式,就是爲容器而生。相似於「公 交車上的售票員」、「火車上的乘務員」、「空姐」。
Iterator
對象稱爲迭代器(設計模式的一種),主要用於遍歷 Collection
集合中的元素。
Collection
接口繼承了java.lang.Iterable
接口,該接口有一個iterator()
方法,那麼所 有實現了Collection
接口的集合類都有一個iterator()
方法,用以返回一個實現了 Iterator
接口的對象。
Iterator
僅用於遍歷集合,Iterator
自己並不提供承裝對象的能力。若是須要建立 Iterator
對象,則必須有一個被迭代的集合。
集合對象每次調用iterator()
方法都獲得一個全新的迭代器對象,默認遊標都在集合 的第一個元素以前。
使用迭代器Iterator
接口,遍歷集合元素的。
內部的方法:hasNext()
和 next()
。
hasNext()
:判斷是否還有下一個元素。next()
:①指針下移 ②將下移後集合位置上的元素返回。public void test1() { Collection coll = new ArrayList(); coll.add("AA"); coll.add(123); coll.add(false); coll.add(new String("Tom")); coll.add(new Person("Jerry", 20)); // iterator():返回Iterator接口實例,用於遍歷集合元素。 Iterator iterator = coll.iterator(); // 方式一: System.out.println(iterator.next()); System.out.println(iterator.next()); System.out.println(iterator.next()); System.out.println(iterator.next()); System.out.println(iterator.next()); // 報異常:NoSuchElementException System.out.println(iterator.next()); // 方式二: for (int i = 0; i < coll.size(); i++) { System.out.println(iterator.next()); } // 方式三:推薦 // hasNext():判斷是否還有下一個元素 while(iterator.hasNext()) { // next():①指針下移 ②將下移後集合位置上的元素返回 System.out.println(iterator.next()); } }
remove()
:能夠在遍歷的時候,刪除集合中的元素。此方法不一樣於集合中的remove()。注意:若是還未調用next()
或在上一次調用 next()
方法以後已經調用了 remove()
方法, 再調用remove()
都會報IllegalStateException()
。
public void test3() { Collection coll = new ArrayList(); coll.add("AA"); coll.add(123); coll.add(false); coll.add(new String("Tom")); coll.add(new Person("Jerry", 20)); /** 刪除集合中的"Tom" */ Iterator iterator = coll.iterator(); while(iterator.hasNext()){ Object next = iterator.next(); if ("Tom".equals(next)){ iterator.remove(); } } /** 遍歷集合 */ Iterator iterator1 = coll.iterator(); while (iterator1.hasNext()) { System.out.println(iterator1.next()); } }