java集合彙總(一) Iterator Collection

Iterable

public interface Iterable<T>

實現此接口允許對象成爲“for-each loop”語句的目標  可以爲每個元素遍歷

Iterator iterator()    返回類型爲T元素的迭代器。

default void forEach(Consumer action)   對集合裏的每個元素執行給定的操作

Iterator

public interface Iterator<E>

迭代器  接口類

boolean hasNext()   判斷是否還有更多的迭代元素,有 返回true。

E next()      返回迭代中的下一個元素。

default void remove()    刪除此迭代器返回的元素

default void forEachRemaining(Consumer action)   對每個剩餘元素執行給定的操作,直到所有元素都被處理或動作引發異常。

Collection

interface Collection  extends Iterable

集合層次結構中的根界面    集合表示 一組稱爲元素 的對象

int size()   返回此集合中的元素數

boolean isEmpty()    如果此集合不包含元素,則返回true

boolean contains(Object o)    如果此集合包含指定的元素,則返回true

boolean containsAll(Collection<?> c)  如果此集合包含指定集合中的所有元素,則返回true。

Iterator<E> iterator()   返回此集合中的元素的迭代器。

<T> T[] toArray(T[] a)    返回包含此集合中所有元素的數組;返回的數組的運行時類型是指定數組的運行時類型。

boolean add(E e)    將指定的元素加到此列表中

boolean addAll(Collection<? extends E> c)    將指定集合中的所有元素添加到此集合

boolean remove(Object o)  從該集合中刪除指定元素的單個實例(如果存在)

boolean removeAll(Collection<?> c)  刪除指定集合中包含的所有此集合的元素

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

boolean retainAll(Collection<?> c)    保留此集合中包含在指定集合中的元素

AbstractCollection

abstract class AbstractCollectionimplements Collection

Collection的子類  實現了部分方法  

boolean isEmpty()      如果此集合不包含元素,返回true

public boolean isEmpty() {
  return size() ==0;   // 判斷集合中的元素數量是否爲0  0代表是一個空集合
}

boolean contains(Object o)    如果此集合包含指定的元素,則返回true

 public boolean contains(Object o) {
        Iterator<E> it = iterator(); //獲取該集合的迭代器
        if (o==null) {     //如果o
            while (it.hasNext())   //循環遍歷  找到null
                if (it.next()==null)  
                    return true;
        } else {
            while (it.hasNext())
                if (o.equals(it.next())) //循環遍歷 通過equals()找到一樣的元素
                    return true;
        }
        return false;
    }

Object[] toArray()    返回包含此集合中所有元素的數組

public Object[] toArray() {
        Object[] r = new Object[size()];  //初始化一個等長的數組
        Iterator<E> it = iterator();
        for (int i = 0; i < r.length; i++) {  //遍歷
             ...
            r[i] = it.next();  //給數組賦值
        }
        return  ... r;
    }

boolean remove(Object o)     從該集合中刪除指定元素的單個實例(如果存在)

public boolean remove(Object o) {
        Iterator<E> it = iterator();
        if (o==null) {
            while (it.hasNext()) { 
                if (it.next()==null) {    //循環遍歷  找到null
                    it.remove();  //移除該元素
                    return true;
                }
            }
        } else {
            while (it.hasNext()) {
                if (o.equals(it.next())) {  //循環遍歷 通過equals()找到一樣的元素
                    it.remove();   //移除該元素
                    return true;
                }
            }
        }
        return false;
    }

 boolean containsAll(Collection<?> c)  如果此集合包含指定集合中的所有元素,則返回true

public boolean containsAll(Collection<?> c) {
        for (Object e : c)
            if (!contains(e))  // 如果有一個不一樣的返回false
                return false;
        return true;
    }

 boolean addAll(Collection<? extends E> c)    將指定集合中的所有元素添加到此集合

public boolean addAll(Collection<? extends E> c) {
        boolean modified = false; //標識是否改變
        for (E e : c)     //遍歷指定集合  
            if (add(e))   //加到該集合中
                modified = true;
        return modified;
    }

boolean removeAll(Collection<?> c)   刪除指定集合中包含的所有此集合的元素

 public boolean removeAll(Collection<?> c) {
        boolean modified = false;
        Iterator<?> it = iterator();
        while (it.hasNext()) {   //遍歷此集合
            if (c.contains(it.next())) { // 判斷指定集合是否包含此集合的遍歷元素
                it.remove();  //包含  刪除
                modified = true;
            }
        }
        return modified;
    }

 boolean retainAll(Collection<?> c)   保留此集合中包含指定集合的元素  (求交集)

public boolean retainAll(Collection<?> c) { 
        boolean modified = false;
        Iterator<E> it = iterator();
        while (it.hasNext()) {
            if (!c.contains(it.next())) { // 判斷指定集合是否包含此集合的遍歷元素
                it.remove();  // 刪除
                modified = true;
            }
        }
        return modified;
    }

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

 public void clear() {
        Iterator<E> it = iterator();
        while (it.hasNext()) {  // 遍歷
            it.next(); 
            it.remove();
        }
    }

 

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