search
HomeJavaJavaBaseThe 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
collectionjavalistsetthe differenceinterface

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:

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());

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

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.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools