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

The difference between set and list interfaces in Java Collection

王林
王林Original
2019-11-25 11:04:373592browse

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