The Container Class (Conllection) is one of the most powerful tools for a developer to significantly improve programming capabilities. A container is an object that combines multiple elements into a single cell, an object that represents a set of objects, and an object in the container becomes its element.
Containers are designed to handle aggregation of objects of various types, such as storing, acquiring, manipulating aggregated data, and communicating aggregated data. The container holds only the reference to object because this is the base class for all classes, so the container can hold objects of any class.
The container framework includes the following elements:
Interfaces: They represent abstract data types for container types. The base of the entire Java container class is the container interface, not the class. The biggest benefit is separating the container's implementation from the container's interface.
Implementations: They are concrete implementations of container interfaces.
Algorithms: They really implement methods of performing operations on a collection interface object, such as sorting and searching. These methods are called polymorphic, which means that the same method can handle many different implementations of an interface, and the algorithm is a reusable feature.
Advantages and disadvantages of the Java container framework
1, reduce the number of programming, container framework provides a useful data structure and algorithm of high-performance, high-quality implementation, so that programmers do not have to hard to write and maintain their own data structure, reduce the likelihood of errors, can have more time to improve the quality and performance of the program.
2, improve the operation speed and quality of the program
3. Allow interoperability between independent APIs
4. Reduce the difficulty of learning and using the new API
5. Reduce the workload of designing new APIs
6, promote the reuse of software.
1, Collection interface: Is the most basic set interface, a collection represents a set of object, that is, the collection element (Elements). Some collection allow the same elements while others do not. Some can sort and others can't. The Java SDK does not provide classes that inherit directly from collection, and the provided classes are sub-interfaces that inherit from collection such as list and set. How do I traverse every element in the collection? Regardless of the actual type of collection, it supports the iterator () method, which returns an iteration, which allows each element in the collection to be accessed one at a time using the iteration. Such as:
Iterator it = Collection.iterator ();//Get an iteration
while (It.hasnext ()) {
Object obj = It.next ();//Get Next element
}
2, the list interface, it is an ordered collection, using this interface can precisely control the location of each element insertion, the user can use the index (the position of the element in the list, similar to the subscript of the array) to access the elements in the list, which is similar to the array, Unlike the set mentioned below, the list appears to have the same element. In addition to the standard iterator method, the list also provides a Listiterator () method. Compared to the standard iterator interface
As you can tell from the above two images, Listiterator inherits the iterator and extends the other methods while inheriting iterator properties and methods. The list has a set (Index,object), which replaces the index element in the list. The common classes that implement the list interface are: Linkedlist,arraylist,vector, stack, and so on.
LinkedList class : It implements the list interface, allows null elements, and LinkedList provides an additional Get,remove,insert method at the header or tail of the LinkedList. These operations enable the LinkedList to be used as a stack (stack), queue, or bidirectional queue (deque). LinkedList There is no synchronization method, if multiple threads access a list at the same time, you must implement access synchronization yourself. One solution is to construct a synchronized list:list list = collections.synchronizedlist (new LinkedList (...) when creating a list.
ArrayList class
ArrayList implements a variable-size array. It allows all elements, including null. ArrayList is not synchronized.
Size,isempty,get,set method run time is constant. But the Add method cost is the allocated constant, and adding n elements requires an O (n) time. Other methods run at a linear time.
Each ArrayList instance has a capacity (capacity), which is the size of the array used to store the elements. This capacity automatically increases as new elements are added, but the growth algorithm is not defined. When you need to insert a large number of elements, you can call the Ensurecapacity method before inserting to increase the capacity of the ArrayList to improve insertion efficiency.
Like LinkedList, ArrayList is also unsynchronized (unsynchronized).
Vector class
Vectors are very similar to ArrayList, but vectors are synchronous. The iterator created by the vector, although the same interface as the iterator created by ArrayList, but because the vector is synchronous, when a iterator is created and is being used, another thread changes the state of the vector (for example, Some elements have been added or removed, Concurrentmodificationexception will be thrown when the iterator method is called, so the exception must be caught.
Stack class
Stack inherits from Vector and implements a last-in-first-out stack. The stack provides 5 additional ways to make the vector available as a stack. The basic push and pop methods, and the Peek method to get the stack top element, the empty method tests if the stack is empty, and the search method detects the position of an element on the stack. Stack has just been created as an empty stack.
Set interface
Set is a collection that contains no duplicate elements, that is, any two elements E1 and E2 have E1.equals (E2) =false,set have a maximum of one null element.
Obviously, the constructor of a set has a constraint that the passed-in collection parameter cannot contain duplicate elements.
Note: Variable objects (Mutable object) must be handled with care. If a mutable element in a set changes its state, causing Object.Equals (Object) =true will cause some problems.
Map interface
Note that map does not inherit the collection interface, and map provides a key-to-value mapping. A map cannot contain the same key, and each key can only map one value. The map interface provides views of 3 collections, and the contents of the map can be treated as a set of key sets, a set of value collections, or a set of key-value mappings.
Hashtable class
Hashtable inherits the map interface to implement a key-value mapped hash table. Any object that is not empty (non-null) can be either a key or a value.
Add data using put (key, value), take out the data using get (key), the time overhead for these two basic operations is constant.
The Hashtable adjusts performance through the initial capacity and load factor two parameters. Normally the default load factor 0.75 is a good way to achieve a balanced time and space. Increasing the load factor can save space but the corresponding lookup time will increase, which will affect operations like get and put.
A simple example of using Hashtable is to put the three-to-one in the Hashtable, with their key being "single", "Two", "three":
Hashtable numbers = new Hashtable ();
Numbers.put ("One", New Integer (1));
Numbers.put ("n", New Integer (2));
Numbers.put ("Three", New Integer (3));
To take out a number, say 2, with the corresponding key:
Integer n = (integer) numbers.get ("a");
System.out.println ("both =" + N);
Because an object that is a key will determine the position of its corresponding value by calculating its hash function, any object that is a key must implement the Hashcode and Equals methods. The hashcode and Equals methods inherit from the root class object, and if you use a custom class as key, be quite careful, as defined by the hash function, if two objects are the same, i.e. obj1.equals (OBJ2) =true, Their hashcode must be the same, but if two objects are different, their hashcode may not be different, if the hashcode of two different objects is the same, this phenomenon is called a conflict, the conflict causes the time overhead of manipulating the hash table, so define the hashcode () as much as possible. method to speed up the operation of the hash table.
If the same object has different hashcode, the operation of the hash table will have unexpected results (expecting the Get method to return null), to avoid this problem, you need to keep in mind one: to replicate both the Equals method and the Hashcode method, rather than write only one of them.
The Hashtable is synchronous.
HashMap class
HashMap and Hashtable are similar, except that HashMap is non-synchronous and allows NULL, which is null value and null key. , but when HashMap is treated as collection (the values () method can return collection), its iteration sub-operation time overhead is proportional to the capacity of HashMap. Therefore, if the performance of the iterative operation is quite important, do not set the initialization capacity of the hashmap too high or load factor too low.
Weakhashmap class
Weakhashmap is an improved hashmap, which implements a "weak reference" to key, which can be recycled by GC if a key is no longer referenced externally.
Summarize
If it involves operations such as stacks, queues, and so on, you should consider using the list, for quick insertions, for deleting elements, should use LinkedList, and if you need to quickly randomly access elements, you should use ArrayList.
If the program is in a single-threaded environment, or if access is done only in one thread, it is more efficient to consider non-synchronous classes, and if multiple threads may operate on a class at the same time, the synchronized classes should be used.
Pay special attention to the operation of the hash table, and the object as key should correctly replicate the Equals and Hashcode methods.
Try to return the interface rather than the actual type, such as returning a list instead of ArrayList, so that if you need to change ArrayList to LinkedList later, the client code does not have to be changed. This is for abstract programming.
Reference http://blog.csdn.net/wwww1988600/article/details/8646191
Understanding of Java Containers (collection)