Java collections framework Overview

  Java's official documents is that introduced this collection: A collection - sometimes called a container - is simply an object that groups multiple elements into a single unit Collections are used to store, retrieve, manipulate, and communicate aggregate data Typically, they.. represent data items that form a natural group , such as a poker hand (a collection of cards), a mail folder (a collection of letters), or a telephone directory (a mapping of names to phone numbers). If you have used the Java programming language - or just about any other programming language -.. you are already familiar with collections (collection ( sometimes also called container) is a simple object, it is the multiple elements organized into a set of cells can be used to store, retrieve, manipulate, communicate. typically, the collection represents a natural items, such as a set of hand (set of cards), mail folders (a collection of e-mail), telephone directories (names to the phone's mapping). If you used Java or other languages, you should be very familiar with Collection)

A set of basic interface framework Java / class hierarchy:

java.util.Collection [I]
+–java.util.List [I]
  +–java.util.ArrayList [C]
  +–java.util.LinkedList [C]
  +–java.util.Vector [C]
     +–java.util.Stack [C]
+–java.util.Set [I]
  +–java.util.HashSet [C]
  +–java.util.SortedSet [I]
    +–java.util.TreeSet [C]

Jawakutilkmap [e]
+ -jawakutilksortednap [e]
   + -jawakutilktrenap [g]
+ -jawakutilkःashtble [g]
+ -jawakutilkःashanap [g]
+ - Jawakutilklaidakedःashanap [g]
+ -jawakutilkWeakHashMap [g]

[I]: Interface
[C]: Class

Collection Interface

  Collection is the most basic set of interfaces, a set representative of a group of Object Collection, which is referred to as Object Collection element. Collection is the most basic set of interfaces, a set representative of a group of Object Collection, which is referred to as Object Collection element.

List Interface

  List inherited from the Collection interface. List is ordered Collection, use this interface to accurately control the position of insertion of each element. Users can use index (List elements in the position, similar to the array subscript) to access elements in the List, which is similar to an array of Java.
  Set with a collection of different, List allows duplicate elements. For satisfying that e1.equals (e2) e1 and e2 condition the element, may be present in the collection List simultaneously. Of course, there are also List of the implementation class does not allow duplicate elements exist.
  Collection iterator addition to the necessary interface () method, List also provides a listIterator () method returns a ListIterator interface and standard Iterator interface compared, a few more ListIterator add () method or the like, allows to add, delete, set elements, but also traversing forward or backward.
  List interfaces to achieve common class of LinkedList, ArrayList, Vector, and Stack.

LinkedList class

  LinkedList implements List interface that allows null elements. In addition to provide additional LinkedList get, remove, insert method in LinkedList the first or tail. LinkedList these operations may be used as a stack (stack), a queue (Queue) or double-ended queue (deque).
  Note that no synchronization LinkedList method. If multiple threads access a List, you must achieve their visit synchronization

#### ArrayList class
  ArrayList implements an array of variable size. It allows all the elements, including null. ArrayList is not synchronized.
size, isEmpty, get, set method of running time is constant. But add methods for the assessment of a constant, add n elements need to O (n) time. Other methods running time is linear.
  Each ArrayList instance has a capacity (Capacity), i.e., for the size of the array elements stored. With the capacity to add new elements to increase automatically, but growth algorithm is not defined. When the need to insert a large number of elements, the insert can be called ensureCapacity ArrayList method to increase the capacity to improve the efficiency of insertion. And LinkedList, ArrayList is non-synchronous (unsynchronized)

#### Vector class
  Vector is very similar to the ArrayList, but Vector is synchronized. Created by Vector Iterator, although ArrayList created Iterator is the same interface, but because Vector is synchronized, when a Iterator been created and is being used by another thread Vector change the status of (for example, add or delete some element), then call when the method Iterator will throw ConcurrentModificationException, it is necessary to catch the exception.

#### Stack class
  Stack inherited from Vector, to implement a LIFO stack. Stack provide five additional approach allows Vector to be used as a stack. The push and pop the basic method, there is obtained a method Zhanding elements peek, empty method to test whether the stack is empty, search method of detecting the position of an element in the stack. Stack after stack is empty just created.

Set Interface

  Set inherited from the Collection interface. Set is a set can not contain duplicate elements, i.e., for satisfying that e1.equals (e2) e1 and e2 condition the element can not exist simultaneously in the same Set the collection, in other words, any two elements of the collection Set e1 and e2 are met e1.equals (e2) == false condition, Set up to a null element.
  Because of this restriction Set, when using the Set collection, it should be noted:
  1, to achieve an effective equals (Object) method implementation class Set the collection of elements.
  2, the structure of the Set function, the incoming Collection parameter can not contain duplicate elements.

HashSet class

  This class implements the Set interface, backed by a hash table (actually a HashMap instance). It does not guarantee that the iteration sequence set; in particular, it does not guarantee that the order constancy. Such permit null elements. HashSet not synchronized.

TreeSet

  Is an ordered collection, its role is to provide an orderly Set collection. AbstractSet it inherited from the abstract class implements NavigableSet , The Cloneable
  TreeSet is based TreeMap implementation. TreeSet the element supports two kinds Sort: natural order or sorted according to a Comparator provided when creating TreeSet. Depending on the construction method used. TreeSet provides guaranteed log (n) time cost for the basic operations (add, remove, and contains). Further, TreeSet unsynchronized. It iterator method returns iterator is a fail-fast.

Map Interface

  No successor Map Collection interface. That Map and Collection are two different sets. Collection can be seen as (value) of the set, and Map can be seen as (key, value) collection. Map Map interface provided by the content of a set of three types of view, a key group, a group set value, or a set of key-value mappings group.

#### Hashtable class
  Hashtable inherited Map interface, the hash table a key-value mapping. Any object of non-empty (non-null) can be used as a key or value.
Add data using put (key, value), using the extracted data get (key), the two basic operation time overhead is constant.
Hashtable adjusted through initial capacity and load factor two parameters. Usually the default load factor 0.75 better achieved the balance of time and space. Increasing the load factor can save space but find the corresponding time will increase, which will affect operations such as get and put.

#### HashMap class
  HashMap and Hashtable similar, except that the HashMap non-synchronized, and allow null, null value and null key. , But when considered HashMap Collection (values () method returns the Collection), which is proportional to the capacity of the iterator operation time expenses and HashMap. Therefore, if the performance of iterative important operation, it will not HashMap initialization for the high capacity, or load factor too low.

Following summary

Collection: the collection hierarchy root. A collection represents a set of objects. Some orderly, some disorderly. Some repeat, a bit repetitive. Collection is not directly implemented, and only fulfill its corresponding sub-interface.
Set: can not contain duplicate elements, such as process poker hand, student elective program, a computer.
List: ordered set, can also contain duplicate elements. For the precise control of each element, such as inserting, indexed by index.
Map: mapping values for the keys, can not contain duplicate elements, also provided with two ordering Set Map

  If it comes to operating stacks, queues, etc., should consider the use of List, the need to quickly insert, delete elements, you should use LinkedList, if you need fast random access to elements, you should use ArrayList.
  If the program, or access only in a thread in a single-threaded environment, consider the asynchronous class, the higher the efficiency, if multiple threads may also operate a class, the class should be used to synchronize.
  In addition to the need to use TreeSet when ordering, outside TreeMap, should use HashSet, HashMap, because of their higher efficiency. Pay special attention to the operation of the hash table, as a key target to correct replication equals and hashCode methods.
  Containers held only object reference (pointer to the object), instead of the copy destination information to a number of columns in a certain position. Once the objects are placed within the container, they do not lose information about the object type.
  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.

note:

 1, Collection did not get () method to obtain an element. Can only be traversed by elements iterator ().
 2, Set and Collection has exactly the same interface.
 3, List, one element may be removed by a get () method. Selecting a pile using a digital object, GET (0) .. (the Add / GET)
 . 4, generally used ArrayList. LinkedList stack configuration with stack, queue queue.
 5, Map using put (k, v) / get (k), you may be used containsKey () / containsValue () to check whether they contain one wherein key / value. HashMap hashCode object will use to quickly find the key.
 6, Map elements may be key sequence, value sequence extracted separately. Use keySet () extracting a key sequence, all generated keys map in a Set. Using the values () value extraction sequence, all the values map to generate a Collection. Why generate a Set, a generation Collection? That is because, key is always unique, value allowed to be repeated.

Reference article: 51CTO blog

Original: Big Box  Java collections framework Overview


Guess you like

Origin www.cnblogs.com/petewell/p/11584955.html