Home > Java > JavaBase > What are the unique collections in java?

What are the unique collections in java?

Release: 2019-12-27 09:47:13
Original
21290 people have browsed it

What are the unique collections in java?

The Java collection class is stored in the java.util package and is a container used to store objects. Let's take a look at non-duplicate collections in Java.

Set: A typical implementation of HashSet() is an unordered, non-repeatable set

1. Set hashSet = new HashSet();

①. HashSet: The order of the elements cannot be guaranteed; it is not repeatable; it is not thread-safe; the set elements can be NULL;

②. The bottom layer is actually an array, and the meaning of its existence is to speed up the query. We know that in a general array, the index position of the element in the array is random, and there is no definite relationship between the value of the element and the position of the element. Therefore, when searching for a specific value in the array, you need to change the search value Compare with a series of elements. The query efficiency at this time depends on the number of comparisons during the search process. The index and value of the underlying array of the HashSet collection have a certain relationship: index=hash(value), then you only need to call this formula to quickly find the element or index.

③. For HashSet: If two objects return true through the equals() method, the hashCode values ​​of the two objects should also be the same.

2. Set linkedHashSet = new LinkedHashSet();

①, cannot be repeated, in order

Because the underlying layer uses linked lists and hash tables algorithm. The linked list guarantees the order in which elements are added, and the hash table guarantees the uniqueness of elements

3. Set treeSet = new TreeSet();

TreeSet: ordered; non-repeatable, used at the bottom Red-black tree algorithm is good at range queries.

* If you use the TreeSet() parameterless constructor to create a TreeSet object, the class of the elements placed in it must implement the Comparable interface. Therefore, null elements cannot be placed in it.

Comparison of the implementation classes of the above three Set interfaces:

Common points:

1. None of the elements are allowed to be repeated

2. All It is not a thread-safe class. Solution: Set set = Collections.synchronizedSet(set object)

Differences:

HashSet: The order of adding elements is not guaranteed, and the bottom layer uses a hash table algorithm. The query efficiency is high. To determine whether two elements are equal, the equals() method returns true, and the hashCode() values ​​are equal. That is to say, the elements stored in HashSet are required to cover the equals() method and hashCode() method

LinkedHashSet: A subclass of HashSet. The bottom layer uses hash table algorithm and linked list algorithm, which ensures that the elements The order of addition also ensures query efficiency. However, the overall performance is lower than HashSet.

TreeSet: The adding order of elements is not guaranteed, but the elements in the set will be sorted. The bottom layer uses the red-black tree algorithm (the tree structure is more suitable for range queries)

Map: key-value key-value pair, key is not allowed to be repeated, and value can be

1. Strictly speaking, Map is not a set, but a mapping relationship between two sets.

2. Each piece of data in these two collections has a mapping relationship, and we can regard it as a piece of data. That is Entry(key,value). Map can be seen as composed of multiple Entries.

3. Because the Map collection neither implements the Collection interface nor the Iterable interface, you cannot perform for-each traversal on the Map collection.

For more java knowledge, please pay attention to the java basic tutorial column.

The above is the detailed content of What are the unique collections in java?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template