Introduction to the relationship and difference between Set and List in Java
Both interfaces are inherited from Collection.
List (inteface)
Order is the most important feature of List, which ensures that the specific order of elements is maintained.
-ArrayList Allows fast random access to elements.
——LinkedList optimizes sequential access. The cost of inserting and removing from the middle of the List is not large. It has addFrist(), addLast(), getFirst, getLast, removeFirst and removeLast(). .These methods allow LinkedList to be used as a stack/queue/bidirectional queue.
Set (inteface)
Each element stored in Set must be unique, and the order of the elements is not guaranteed to be maintained. Add to Set The Object must define the equals() method
--HashSet A Set designed for fast search, and the HashSet object stored must define hashCode().
--TreeSet A Set that protects the order, which can be used to extract from the Set Ordered sequence.
-LinkedHashSet Has the query speed of HashSet, and internally uses a linked list to maintain the order of elements.
The storage methods between them are different:
TreeSet uses red and black The elements are sorted according to the tree structure.
HashSet uses a hash function, which is specially designed for fast query.
LinkedHashSet uses hash internally to speed up query, while using a linked list to maintain the order of elements.
When using HashSet/TreeSet, equals() must be defined for the class; HashCode() is for HashSet. As a programming style, when equals() is overridden, hashCode() should also be overridden.
For more related articles on the relationship and differences between Set and List in Java, please pay attention to the PHP Chinese website!

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics











Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

Start Spring using IntelliJIDEAUltimate version...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...
