Home Java JavaBase The difference between set and list interfaces in Java Collection

The difference between set and list interfaces in Java Collection

Nov 25, 2019 am 11:04 AM
collection java list set the difference interface

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());
Copy after login

Running results:

The difference between set and list interfaces in Java Collection

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());
Copy after login

Running results:

The difference between set and list interfaces in Java Collection

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1665
14
PHP Tutorial
1270
29
C# Tutorial
1250
24
PHP's Impact: Web Development and Beyond PHP's Impact: Web Development and Beyond Apr 18, 2025 am 12:10 AM

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

The difference between laravel and thinkphp The difference between laravel and thinkphp Apr 18, 2025 pm 01:09 PM

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.

In back-end development, how to distinguish the responsibilities of the service layer and the dao layer? In back-end development, how to distinguish the responsibilities of the service layer and the dao layer? Apr 19, 2025 pm 01:51 PM

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...

What are the differences between yi2 and tp5 What are the differences between yi2 and tp5 Apr 18, 2025 pm 11:06 PM

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.

What currency does Ripple (XRP currency) belong to? Detailed tutorial for beginners What currency does Ripple (XRP currency) belong to? Detailed tutorial for beginners Apr 28, 2025 pm 07:57 PM

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.

Composer: Aiding PHP Development Through AI Composer: Aiding PHP Development Through AI Apr 29, 2025 am 12:27 AM

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.

Popular science in the currency circle: What is the difference between decentralized exchanges and hybrid exchanges? Popular science in the currency circle: What is the difference between decentralized exchanges and hybrid exchanges? Apr 21, 2025 pm 11:30 PM

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.

What does 'platform independence' mean in the context of Java? What does 'platform independence' mean in the context of Java? Apr 23, 2025 am 12:05 AM

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.

See all articles