The Java collection class is detailed

Source: Internet
Author: User
Tags repetition

First, the container class introduction
①java container classes can automatically adjust their size.
The ②collection interface is a set of objects that allow duplicates.
The ③set interface inherits Collection, does not allow repetition, and uses one of its own internal arrangement mechanisms.
The ④list interface inherits Collection, allows repetition, places elements in the order in which they are placed, and does not rearrange.
The ⑤map interface is a pair of key-value objects that hold some key-value pairs. There cannot be duplicate keys in the map. have their own internal arrangement mechanism.


Ii. Introduction to several more important interfaces and classes

1. List (ordered, indexed, repeatable)
The list contains objects that are ordered and repeatable, and the list focuses on indexes and has a series of index-related methods.
ArrayList (array, quick access)
ArrayList can be understood as an array that can grow, so it can be accessed quickly through the index.
LinkedList (linked list, quick Insert, delete)
LinkedList is a two-way link that has the feature of quick insertion and deletion of linked lists.
Vector
ArrayList is a thread-safe version, but with low performance.
2. Set (unique, unordered)
HashSet
The HashSet is implemented by HashMap, and set uses the key in the map, so set is unique. In HashMap, the hashcode and equals methods are used to ensure uniqueness. The contains and remove bases of the HashSet are hashcode methods, and the Equals method is judged if the method returns the same value.
TreeSet
TreeSet is implemented by TreeMap and can be sorted.
3, Map (key value pair, key unique)
The map is primarily used to store the health-value pairs, and the key is not allowed to repeat (overwriting is repeated), but allows the values to be duplicated.
HashMap
Hashmap is the most commonly used map, it stores data according to the hashcode value of the key, can get its value directly according to the key, has the fast access speed, when traversing, obtains the data the order is completely random. HashMap allows a maximum of one record's key to be null, allowing multiple records to have a value of NULL
Hashtable
Hashtable, like HashMap, is a thread-safe version of HashMap, which inherits from the dictionary class, unlike the following: it does not allow logging of keys or values to be empty and inefficient.
Linkedhashmap
Linkedhashmap preserves the order in which records are inserted, and when traversing linkedhashmap with iterator, the first record must be inserted first. You can also use the parameters in the construction to sort by the number of applications. The traversal will be slower than HashMap, but there is an exception, when the HashMap capacity is large, the actual data
less, traversing may be slower than Linkedhashmap, because the Linkedhashmap traverse speed is only related to the actual data, not the capacity, and HashMap's traverse speed is related to his capacity.
TreeMap
TreeMap implements the Sortmap interface, it can save the record according to the key sort, the default is the key value of ascending order, you can also specify a sort of comparator, when using iterator traversal treemap, the resulting records are ordered.
in general, we use the most is the hashmap,hashmap inside the key value of the pair when it is taken out is random, it according to the hashcode value of the key to store data, according to the key can directly get its value, with fast access speed. HashMap is the best choice for inserting, deleting, and locating elements in a map.
The TreeMap takes out the sorted key-value pairs. But if you want to traverse the key in natural order or in a custom order, TreeMap is better.

Linkedhashmap is a subclass of HashMap, and if the order of output needs to be the same as the input, then the LINKEDHASHMAP can be implemented.


Three, the performance analysis of aggregate class

y TD valign= "Top" >
Interface Implementation class Keep the Insertion Order Repeatable Sort Instructions for use
  list ArrayList y n random access element But inserting and deleting elements is slow (array attributes).
linkedlist y y N inserts, deletes elements faster, but accesses slower (list attributes).
      set HashSet n n n use hash, The quickest way to get an element.
TreeSet N N Y Stores elements in a red-black tree data structure. The default is ascending.
Linkedhashset Y N N Use a hash to maintain the insertion order of elements using a linked list.
      map HashMap n n n use hash, Provides the fastest search technology.
TreeMap N N Y The default is to save the key in ascending order of comparison results.
Linkedhashmap Y N N Save the key in the order in which it was inserted, and use the hash to increase the lookup speed.
Summarize
① If you are involved in stacks, queues and other operations, you should consider using list. If you want to do a lot of random access, you should use ArrayList, and if you frequently insert and delete operations, use LinkedList.
②hashmap is designed for quick access, while TreeMap keeps the "key" always in the sorted state, so there is no hashmap fast. Linkedhashmap preserves the order in which elements are inserted, but also provides quick access through hashing.
③set does not accept duplicate elements. HashSet provides the fastest query speed, while TreeSet keeps elements in a sorted state. Linkedhashset saves the element in an insertion order.
④ the operation of the hash table, the object to be key is to rewrite the Equals and Hashcode methods correctly.
⑤ try to return the interface rather than the actual type (for abstract programming), such as returning a list instead of ArrayList, so that if you later need to change ArrayList to LinkedList, the client code does not have to be changed.
obsolete vector\hashtable\stack should not be used in ⑥ programs.

The Java collection class is detailed

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.