Java集合框架及相關方法

1.Collection接口
(1)addAll

Collection<Integer> collection = new ArrayList<Integer>(Arrays.asList(1,2,3,4,5));
Integer[] moreInts = {6,7,8,9,10};
collection.addAll(Arrays.asList(moreInts));

(2)public boolean add(E e):把給定的對象添加到當前集合中
(3)public void clear():清空集合中的元素
(4)public boolean remove(E e):把給定的對象在當前集合中刪除
public boolean removeAll(Collection c):從集合中刪除c集合中所有的元素
(5)public boolean contains(E e):判斷當前集合中是否包含給定的對象
public boolean containsAll(Collection c):檢查Collection集合中是否包含c的全部對象,全部包含則返回true
(6)public boolean isEmpty():判斷當前集合是否爲空
(7)public int size():返回集合中元素的個數
(8)pubic Object[] toArray():把集合中的元素,存儲到數組中
(9)Object的equal方法:是用"=="實現,即比較地址是否一樣

public boolean equals(Object obj){
    return (this == obj);
}

(10)public int hashCode():返回此Collection集合的哈希碼值
(11)public void retainAll(Collection c):集合中僅保留c集合中的所有元素(兩者的交集)
(12)public Object[] toArray():返回包含此collection集合中所有元素的數組

2.List接口
(1)public void add(int index,E e):將指定元素,添加到該集合中的指定位置上,後面的元素都往後移一個元素
public boolean addAll(int index,Collection c):在指定位置插入c集合全部元素,如果集合中沒有元素,則返回false
(2)public E get(int index):返回集合中指定位置的元素
(3)public E remove(int index):移除列表中指定位置的元素,返回的是被移除的元素
(4)public E set(int index,E e):用指定元素替換集合中指定位置的元素,返回值爲更新前的元素
(5)public int indexOf(Object[] o):返回該對象在List中所處位置的索引編號,若沒有則返回-1
(6)List subList(int fromIndex,int toIndex):返回從索引fromIndex到toIndex的元素集合,包左不包右
3.LinkedList
(1)public void addFirst(E e):將指定元素插入此列表的開頭
(2)public void addLast(E e):將指定元素添加到此列表的結尾
(3)public E getFirst():返回此列表的第一個元素
(4)public E getLast():返回此列表的最後一個元素
(5)public E removeFirst():移除並返回此列表的第一個元素
(6)public E removeLast():移除並返回此列表的最後一個元素
(7)public E pop(): 從此列表所表示的堆棧處彈出一個元素
(8)public void push(E e):將元素推入此列表所表示的堆棧
(9)public boolean isEmpty():如果列表不包含元素,則返回true

4.Stack
(1)public void push(num):入棧
(2)public E pop():棧頂元素出棧並返回值
(3)public boolean empty():判定棧是否爲空
(4)public E peek():獲取棧頂元素
(5)public int search(num):判斷元素num是否在棧中,若在返回1,不在返回-1
5.Queue
這組方法,成功返回true,操作失敗拋出異常
(1)public void add(E e):添加一個元素到隊尾
(2)public E remove():獲取隊首元素,並從隊列中移除
(3)public E element():獲取隊首的元素,但不從隊列中移除
這組方法,成功返回true,失敗時返回一個特殊值(NULL或false)
(4)public void offer(E e):添加一個元素到隊尾
(5)public E poll():獲取隊首的元素,並從隊列中移除
(6)public E peek():獲取隊首的元素,但不從隊列中移除
6.PriorityQueue
(1)PriorityQueue priorityqueue = new PriorityQueue();自然順序排列
(2)PriorityQueue priorityqueue = new PriorityQueue(10,Collections.reverseOrder());逆序排列
(3)按自己意願排序:

PriorityQueue<PriorityObject> queue = new PriorityQueue<PriorityObject>(10,new Comparator<PriorityObject>(){
    @Override  
    public int compare(PriorityObject paramT1,  PriorityObject paramT2) {  
        return  paramT2.mPriority - paramT1.mPriority;  
    }
});  

7.Set集合繼承自Collection接口,沒有額外的方法
8.HashSet:重寫equal和hashcode
9.LinkedHashSet:有順序,重寫equal和hashcode

10.Map接口
(1)public V put(K key,V value):把指定的鍵與指定的值添加到Map集合中
public void putAll(Map<K,V> m):向map集合中添加hiding集合中的所有元素
(2)public V remove(Object key):把指定的鍵所對應的鍵值對元素在Map集合中刪除,返回被刪除元素的值
(3)public V get(Object key):根據指定的鍵,在Map集合中獲取對應的值
(4)public boolean containsKey(Object key):檢查map集合中有沒有包含Key爲key的元素,如果有則返回true
(5)public boolean containsValue(Object value):檢查map集合中有沒有包含Value爲value的元素,如果有則返回true
(6)public boolean equals(Object o):判斷兩個map集合的元素是否相同(底層重寫了equals方法)
(7)public void clear():把map集合中所有鍵值刪除
(8)public int size():返回map集合中元素個數
(9)public int hashCode():返回map集合的哈希碼值
(10)public boolean isEmpty():檢查map集合中是否有元素,如果沒有則返回true

(11)public Set keySet():返回map集合中所有Key
(12)public Collection values():返回map集合中所有的Value到一個Collection集合
(13)遍歷方式:public Set<Map.Entry<K,V>> entrySet():返回map到一個Set集合中,以map集合中的Key=Value的形式返回到set中
(14)對Entry對象獲取鍵值:
public K getKey()
public V getValue()

HashMap<int,String> map = new HashMap<int,String>();
map.put(1,"a");map.put(2,"b");map.put(3,"c");
Set<Entry<int,String>> entrySet = map.entrySet();//獲取所有entry對象
//遍歷得到每一個entry對象
for(Entry<int,String> entry:entrySet){
    int key = entry.getKey();
    String value = entry.getValue();
    System.out.println(key+":"+value);
}

Map集合遍歷的三種方式:
a.使用keySet():獲取map集合中的所有鍵,再通過get方法獲取鍵對應的值
b.通過values獲取所有值,不能獲取到key對象
c.獲取Map.Entry對象集合
11.HashMap應用要複寫equals和hashcode方法
12.LinkedHashMap有序

13.Iterator迭代器
(1)public Iterator iterator():獲取集合對應的迭代器,用來遍歷集合中的元素
(2)public E next():返回迭代的下一個元素
(3)public boolean hasNext():如果仍有元素可以迭代,則返回true
(4)public void remove():刪除集合中Iterator指向位置後面的元素

public static void display(Iteration<Pet> it){
    while(it.hasNext()){
        Pet p = it.next();
        System.out.println(p);
    }   
}
display(pets.iterator());

有些地方使用Collection可以有相同的作用

public static void display(Collection<Pet> pets){
    for(Pet p : pets)
        System.out.println(p);
}

14.listIterator迭代器
(1)public void add(E e):將指定元素插入列表,插入位置爲迭代器當前位置之前
(2)public boolean hasNext():以正向遍歷列表時,如果列表迭代器後面還有元素,則返回true
(3)public boolean hasPrevious():以逆向遍歷列表,列表迭代器前面還有元素,則返回true
(4)public E next():返回列表中listIterator指向位置後面的元素
(5)public int nextIndex():返回列表中listIterator所指位置後面元素的索引
(6)public E previous():返回列表中listIterator所指位置前面的元素
(7)public int previousIndex():返回列表中listIterator所指位置前面元素的索引
(8)public void remove():刪除集合中Iterator指向位置後面的元素
(9)public void set(E e):從列表中將next()或previous()返回的最後一個元素更改爲指定元素e
(10)public ListIterator listIterator(int index):從指定位置開始,返回此列表元素的列表迭代器

15.Collections:
java.utils.Collections集合工具類
(1)public static boolean addAll(Collection c,T… elements):往集合中添加一些元素

Collections.addAll(collection,11,12,13,14,15);
Collections.addAll(collection,moreInts);

(2)public static void shuffle(List<?> list):打亂集合順序
(3)public static void sort(List list):將集合中元素按照默認規則排序
(4)public static void sort(List list,Comparator<? super T>):將集合中元素按照指定規則排序
(5)Comparable接口和Comparator接口的比較

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