collection classes for Java

Source: Internet
Author: User

In Java's java.util package, there is a batch of classes called collection classes

The collection class, as the name implies, is the class that holds the collection of objects, which can be "ArrayList" in array form or "LinkedList" in the form of a list, or it can be combined with "HashMap" in two forms.

The parent class of the Java collection class is the collection interface, which is divided into three major categories: List, Map, Set.

Among them, list is a set of elements, the most commonly used for ArrayList, LinkedList;

Map is a set of object "key-value pairs", according to key to access value, most commonly used for hashmap and LinkedList;

Set is a true set of elements in a mathematical sense, which does not contain duplicate elements, most commonly used for hashset and TreeSet.



In the JDK source code, the underlying implementation of set is implemented using the corresponding map, the elements that are accessed in set as the key of the map, using the same object as value. For example, in HashSet, a hashmap is used as an internal storage implementation,



TreeSet is also the case, the internal use of a treemap to store elements, the picture is not affixed.


The difference between ArrayList and LinkedList has been written in front of the blog.

The difference between HashMap and TreeMap:

HashMap is based on the hash list implementation, the internal storage structure is adjacency table "array + linked list";

TreeMap is based on red-black trees, and the internal storage structure is red-black.

Therefore, key-value pairs that are deposited into the hashmap require that key be provided with the Hashcode () method as the hash basis, and this method is already available in the object class;

For TreeMap, either provide a comparator as the comparison key size when initializing TreeMap, or the class that requires key must implement comparable. If neither of these conditions is met, it will be reported as an exception when put in the TreeMap.



collection classes for Java

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.