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!

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于设计模式的相关问题,主要将装饰器模式的相关内容,指在不改变现有对象结构的情况下,动态地给该对象增加一些职责的模式,希望对大家有帮助。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver CS6
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
