Java Collections Framework --Collection

Java Collection class

Collection is the most basic set of interfaces, a Collection represents a group of Object. Java SDK does not provide directly inherited from the Collection of the class that provides classes are inherited from the Collection of the sub-interface (such as List and Set)

grammar

public interface Collection<E> extends Iterable<E>{}

Collection is an interface that is highly abstracted collection, he contains the basic set of operations: add, delete, search, clear, traverse, size and so on.

iterator Interface

Whatever the actual type Collection is supported by a iterator () method, iterate through each item.
#### Iterator method

boolean hasNext()                 //如果仍有元素可以迭代,则返回true;

E next()                          //返回迭代的下一个元素;

void remove()                     //从迭代器指向的集合中移除迭代器返回的最后一个元素。

Iterator instance

Iterator it = collection.iterator();          //获得一个迭代子
while( it.hasNext() ) {
        Object obj = it.next();               //得到下一个元素
}

Inheritance system

Collection
    | -List Ordered repeatable, may have a null value
        | --ArrayList non-thread-safe, the underlying array, fast queries, additions and deletions slow, high efficiency
        | --LinkedList non-thread-safe, the bottom for the list, the query is slow, fast additions and deletions high efficiency
        | --Vector thread-safe, the underlying array, fast queries, additions and deletions slow and inefficient.
    | -Set element unrepeatable Collection
        | --HashSet the HashMap by the underlying implementation, to ensure the uniqueness of the insertion element by a method of the object equals hashCode method disorderly storage (removed sequentially storing order and inconsistent)
            | --LinkedHashSet the bottom hash table and chain components. (Consistent storage and removal order) order of hash tables to ensure the uniqueness of the elements, ensure the list of elements
    | --TreeSet Based TreeMap of NavigableSet. Using elements of the natural order of the elements are sorted, sorted according to a Comparator provided when creating the set, the only element
Collection interface inheritance

to sum up

  1. List is an ordered queue, each element has its own index, the index value of the first element is 0. List implementation class has LinkedList, ArrayList, Vector, and Stack;
  2. Vector is similar to ArrayList, but Vector is thread-safe;
  3. Stack inherited from Vector, to implement a LIFO stack;
  4. It is not allowed to set a duplicate set of elements;
  5. Try to return to the interface instead of the actual type, such as return List rather than ArrayList, so that if you later need to be replaced ArrayList LinkedList, client code need not change. This is for the abstract programming.

Guess you like

Origin www.cnblogs.com/willwuss/p/12238938.html