A container is something that holds items, places items, and, for java, Everything is an object, and his container is the class that holds Java's Objects. Because the data container holds the object reference which we may need to use at any time, the General data container should be able to provide convenient query, traverse, modify and other basic interface Functions. The early OOP language implements the centralized management and maintenance of the reference set by means of arrays. But arrays, where the size of the array needs to be determined in advance, does not allow the size to be modified, causing its ability to function as a flexible data container greatly reduced. In order to conveniently use the data container for reference management, Java provides a wealth of data containers to meet the diverse needs of Programmers. The java container is an instance of a series of classes provided by javaapi, used to store objects in the program, primarily in the Java.util package, with unlimited lengths and unlimited types, and you can still hold the integer class when you hold the string class, which does not conflict. collection interface collection is the most basic collection interface, and a collection represents a set of object, the element of Collection. 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 Java SDK provides classes that inherit from collection, such as list and Set. The list interface list is an ordered collection (linetype Store) that allows precise control of where each element is Inserted. The user is able to access the elements in the list using an index (where the element is positioned in the list, similar to an array subscript), i.e. it is sequential, similar to an array of Java. Unlike set, list allows the same elements. The list container classes provided by J2SDK are arraylist, linkedlist, and so On. Unlike the set mentioned below, the list allows the same elements. In addition to the iterator () method, which has the collection interface prerequisites, The list also provides a Listiterator () method that returns a Listiterator interface, compared to the standard iterator interface. Listiterator has a number of add () methods that allow you to add, delete, set elements, and traverse forward or Backward. Common classes that implement the list interface are Linkedlist,arraylist,vectorand Stack. The LinkedList class LinkedList implements the list interface, allowing null Elements. In addition LinkedList provides an additional Get,remove,insert method at the first or the tail of the linkedlist, which is implemented in a doubly linked List. These operations make the LinkedList available as a stack (stack), queue, or two-way queue (deque). Note that LinkedList does not have a synchronization method. If multiple threads access a list at the same time, you must implement Access synchronization Yourself. One workaround is to construct a synchronized list: list = collections.synchronizedlist (new linkedlist (...) When the list is Created. 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 asynchronous (unsynchronized). 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. The most common is to test the attribution, find. obviously, the constructor of a set has a constraint that the Passed-in collection parameter cannot contain duplicate Elements. set container classes are primarily hashset (accessing objects in the container according to the hashing Algorithm) and TreeSet (sorting objects in the container). 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. Map connectionThe port provides a view 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. The put method adds an element to the container, and the Get method retrieves the value object corresponding to the key Object. Summary: Here are the most commonly used four containers: linkedlist : its data structure is linked list, The advantage of this kind of structure is the efficiency of deletion and adding is very high, but the efficiency of random access element is lower than ArrayList class. ArrayList: its data structure uses the linear table, this kind of structure advantage is the access and the query is very convenient, but adds and deletes when the efficiency is very low. The Hashset:set class does not allow duplicate elements (sets), and cannot add a repeating element (already present in set). HashSet uses the hash function to optimize the query efficiency, and its contain () method is often used to determine whether the relevant elements have been added. HashMap: provides a Key-value key value to the data storage mechanism, can be very convenient to find the corresponding elements through the key value, and through the hash hash mechanism, find very convenient. 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 is to correctly replicate the Equals and Hashcode methods. Try to return the interface instead of the actual type, such as returning a list instead of arraylist, so that if you need to change ArrayList to LinkedList later, the Client-side code does not have to be changed. This is for abstract programming.
Java container Collection class