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

What is the difference between list and set in java

王林
王林Original
2020-05-18 17:01:353408browse

What is the difference between list and set in java

The differences between the List method and the set method are:

1. Duplicate objects

The list method allows duplicate objects, but the set method does not. Duplicate objects;

2. Null elements

list can insert multiple null elements, while set only allows one null element to be inserted;

(Video tutorial recommendation: java video)

3. Is the container in order?

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

4. Commonly used implementation classes

List methods are 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 implemented based on HashMap; TreeSet also implements the SortedSet interface, so TreeSet is an ordered container sorted according to its compare() and compareTo() definitions.

Recommended tutorial: Getting started with java development

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