The difference between set and list interfaces in Java Collection
Essentially, List and Set are interfaces and inherit the Collection interface. The ArrayList and HashSet we often use inherit the List and Set interfaces respectively. Due to the use of generics, the actual type can be specified for use in actual applications. Usually we use them to store objects. Of course, Map is also used more often. They all provide interfaces for insertion, deletion and search, and support the use of Iterator. So, what is the difference between List and Set, and how should they be distinguished during use?
The difference between List and Set
(1) A very important difference between List and Set is whether duplicate elements are allowed to exist. In List, duplicate elements are allowed to be inserted, while in Set Duplicate elements are not allowed and will be replaced even if the same element is inserted. I verified that the same elements were inserted into ArrayList and HashSet respectively:
HashSet<String> hset = new HashSet<String>(); ArrayList<String> arrlst = new ArrayList<String>(); hset.add("hello"); hset.add("hello"); arrlst.add("hello"); arrlst.add("hello"); System.out.println("hset size: "+hset.size()+" toString: "+hset.toString()); System.out.println("arrlst size: "+arrlst.size()+" toString: "+arrlst.toString());
Running results:
PS: If you look at the JDK source code, you will see the implementation of HashSet It is done through HashMap.
Java learning video recommendation: Getting started with java
(2) Another very important difference between List and Set is related to the order in which elements are stored. List is an ordered collection, while Set is an unordered collection. List will preserve the order in which elements are inserted, that is, the index of previously inserted elements is smaller than the index of elements inserted later. Set does not preserve the order of insertion. Similarly, let’s verify:
HashSet<String> hset = new HashSet<String>(); ArrayList<String> arrlst = new ArrayList<String>(); hset.add("1"); hset.add("3"); hset.add("2"); arrlst.add("1"); arrlst.add("3"); arrlst.add("2"); System.out.println("hset size: "+hset.size()+" toString: "+hset.toString()); System.out.println("arrlst size: "+arrlst.size()+" toString: "+arrlst.toString());
Running results:
PS: ArrayList uses an object array to store objects, inserting new ones each time The object will be inserted at size; as for HashSet, as mentioned earlier, it is implemented through HashMap. The stored object is used as the key of HashMap. If the key is the same, the value will be replaced. Of course, SortedSet (which inherits the Set interface) can save elements in a specified sorting manner.
(3) List can be accessed through subscripts, but Set cannot.
Common implementation classes of the List interface include ArrayList, Vector and LinkedList, while common implementation classes of the Set interface include HashSet, TreeSet and LinkedHashSet.
More java related article recommendations: java introductory tutorial
The above is the detailed content of The difference between set and list interfaces in Java Collection. For more information, please follow other related articles on 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











PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

Laravel and ThinkPHP are both popular PHP frameworks and have their own advantages and disadvantages in development. This article will compare the two in depth, highlighting their architecture, features, and performance differences to help developers make informed choices based on their specific project needs.

Discussing the hierarchical architecture in back-end development. In back-end development, hierarchical architecture is a common design pattern, usually including controller, service and dao three layers...

With the continuous development of PHP framework technology, Yi2 and TP5 have attracted much attention as the two mainstream frameworks. They are all known for their outstanding performance, rich functionality and robustness, but they have some differences and advantages and disadvantages. Understanding these differences is crucial for developers to choose frameworks.

Created by Ripple, Ripple is used for cross-border payments, which are fast and low-cost and suitable for small transaction payments. After registering a wallet and exchange, purchase and storage can be made.

AI can help optimize the use of Composer. Specific methods include: 1. Dependency management optimization: AI analyzes dependencies, recommends the best version combination, and reduces conflicts. 2. Automated code generation: AI generates composer.json files that conform to best practices. 3. Improve code quality: AI detects potential problems, provides optimization suggestions, and improves code quality. These methods are implemented through machine learning and natural language processing technologies to help developers improve efficiency and code quality.

The difference between decentralized exchanges and hybrid exchanges is mainly reflected in: 1. Trading mechanism: Decentralized exchanges use smart contracts to match transactions, while hybrid exchanges combine centralized and decentralized mechanisms. 2. Asset control: Decentralized exchange users control assets, and mixed exchange ownership centralization and decentralization. 3. Privacy protection: Decentralized exchanges provide high anonymity, and hybrid exchanges require KYC in centralized mode. 4. Trading speed and liquidity: Decentralized exchanges are slower, liquidity depends on user pool, and hybrid exchanges are more fast and liquid in centralized mode. 5. Platform governance: Decentralized exchanges are governed by community governance, and hybrid exchanges are jointly governed by communities and centralized teams.

Java's platform independence means that the code written can run on any platform with JVM installed without modification. 1) Java source code is compiled into bytecode, 2) Bytecode is interpreted and executed by the JVM, 3) The JVM provides memory management and garbage collection functions to ensure that the program runs on different operating systems.
