Java Collection Basics (single thread)

Source: Internet
Author: User

1. Set tree-like:collection├list│├linkedlist│├arraylist│└vector│└stack└set│├hashset│├treeset│├linkedhashset  The Map├hashtable├hashmap└weakhashmap 2.collection interface collection is the most basic collection interface, and a collection represents a group of Object,java The JDK does not provide an implementation class for the collection, and it provides the implementation class for the sub-interface set and list. Therefore, the collection provides a parameterless and parameter-free constructor that constructs an empty collection without a parameter, and a parameter creates a collection based on its size. Regardless of the actual type of collection, it supports the iterator () method, which returns an iterator object that accesses each element in the collection one at a time. Usage is as follows: Iterator it = Collection.iterator (); Gets an iterator while (It.hasnext ()) {Object obj = It.next ();//Gets the next element} Although the implementation class implements the set list map interface, most of the methods are already in the Collect Ion interface. So can write collection collection = new ArrayList (); The advantage is that you don't need to modify the ArrayList feature method when you turn LinkedList. There is also the time when it is not clear which collection to use. Common methods: Add (e E)//adding elements, return Booleanaddall (collection<? extend e>)//Can add a subclass set of E Clear ()//Remove all elements contains (object o)//Yes No contains an element size ()//Collection sizes remove (); Removes an element.  3.set List Map specific implementation class   Interface implementation History Collection class set  hashset treesetlist ArrayList Vector L Inkedlist stackmap   HASHMAP Hashtable TreeMap   The   Properties set:set interface does not allow duplicate elements, the original method is out of the box, no new methods are introduced, and the Set implementation class relies on the Equals () and Hashcode method methods of the added object to check for equivalence;  set The main two implementation classes HashSet and treeset  take efficiency into account, the objects added to  HashSet  need to implement the hashcode ()   method in a way that allocates the hash code appropriately. While most system classes override the default  hashcode () implementation in  Object , do not forget to overwrite  hashcode () when creating your own classes to add to  HashSet .   When you need to set up in a certain order to use TreeSet, but add less efficient than hashset, the general practice is to first use HashSet storage, complete and then turn to the Treeset list:list interface to allow the existence of duplicate elements. The list  interface adds a number of other conventions to the  iterator, add, Remove, equals , and  hashCode  methods, exceeding the  collection The conventions specified in the   interface. For convenience, there are also declarations of other inherited methods that are most commonly used by two implementation classes ArrayList and LinkedList based on linear table implementations, which are based on a linked list. The difference is: LinkedList insert fast, read slow ArrayList read fast, insert slow  set and List differences collection is a collection interface     | ———— set subinterface: unordered, not allowed to repeat.     | ———— List Sub-interface: Ordered, can have repeating elements.     difference: Collections is a collection class     set and List contrast:    set: Retrieving elements is inefficient, the removal and insertion efficiency is high, Insertions and deletions do not cause changes in the position of the element.     list: And arrays, lists can grow dynamically, find elements that are efficient, and insert delete elements with less efficiency because they cause other elements to change position.     Set and ListSpecific subclasses:    set     | ———— HashSet: Storing elements as a hash table, inserting deletes quickly.     list     | ———— ArrayList: dynamic array      | ———— linkedlist: Linked list, queue Stack.     Array and java.util.vector    Vector is an old dynamic array that is thread-synchronized, inefficient, and generally deprecated. The   Map:Map  interface is not an inheritance of  Collection  interfaces. Instead, start with your own interface hierarchy for maintaining key-value associations. By definition, this interface describes a key-to-value mapping that is never duplicated. The main implementation class is: HashMap Linkedhashmap treeMap Hashtable common method: Object put (object key, Object value) return value is the value that is replaced. Object remove (object key) void Putall (Map mapping) void Clear () object get (Object key) Boolean ContainsKey (Object key) Boolean Containsvalue (Object value) int size () Boolean isEmpty ()   Because the collection of keys in the map must be unique, with  Set  support. Because the collection of values in the map may not be unique, it is supported with collection . The last method returns the element  set of an implementation  Map.Entry  interface. The  entryset ()   method of the Map.entry interface map  returns a collection of objects that implement the map.entry  interface. Each object in the collection is a specific key-value pair in the bottom  Map . The  hashmap class and the TreeMap class are the best choice for inserting, deleting, and locating elements,hashmap  in map . But if you want to traverse the keys sequentially, then Treemap  will be better. Depending on the size of the collection, it is possible to add the element to the  hashmap first, and then convert the mapping to a  TreeMap  that is used for sequential key traversal. To optimize the use of  HashMap  space, you can tune the initial capacity and load factor. There is no tuning option for this treemap  because the tree is always in a balanced state. What is the difference between HashMap and Hashtable?   Comparisons between Hashtable and HashMap HashMap and Hashtable are roughly equivalent, except for non-synchronous and null values (HASHMAP allows null values as key and value, while Hashtable is not possible).   HashMap cannot guarantee that the order of the mappings is constant, but as a subclass of HashMap Linkedhashmap, if you want to predict the sequential iterations (by default in order of insertion), you can easily replace the HashMap, if you use Hashtable is not so easy.   The HashMap is not synchronous, and Hashtable is synchronous. Iterative HashMap uses a fast failure mechanism, and Hashtable is not, so this is the design consideration.   Summary: Mastering the characteristics and implementation of each set will help you to have a clearer choice in real-world development.         

Java Collection Basics (single thread)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.