Home  >  Article  >  Java  >  What is the difference between set and list in java?

What is the difference between set and list in java?

青灯夜游
青灯夜游Original
2019-12-26 17:19:182282browse

Collections in Java include three major categories, namely Set, List and Map. They are all in java.util and are interfaces. Each of them has its own implementation class. Let me introduce to you the difference between set and list. I hope it will be helpful to you.

What is the difference between set and list in java?

The difference between set and list in java

1. Duplicate objects

The list method can allow duplication Object, and the set method does not allow duplicate objects

2. Null elements

list can insert multiple null elements, while set only allows the insertion of one null element

3. Container Whether it is ordered

list is an ordered container that maintains the insertion order of each element. That is, the output order is the input order, and the set method is an unordered container, which cannot guarantee the storage order of each element. TreeSet maintains a sorting order through Comparator or Comparable

What is the difference between set and list in java?

4 , Commonly used implementation classes

List method Commonly used implementation classes include ArrayList, LinkedList and Vector. Among them, ArrayList is the most popular, which provides random access using indexes, while LinkedList is more suitable for situations where elements often need to be added or removed from the List. Vector represents the underlying array and is the most thread-safe

Set method. Several popular implementation classes are HashSet, LinkedHashSet and TreeSet. The most popular is HashSet based on HashMap; TreeSet also implements the SortedSet interface, so TreeSet is an ordered container that is sorted according to the definition of compare() and compareTo()

Recommended learning: Java video tutorial

The above is the detailed content of What is the difference between set and list in java?. 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