List is an ordered sequence of elements whereas Set is a distinct list of elements which is unordered (thank you, Quinn Taylor).
List<E>:
An ordered collection (also known as a sequence). The user of this interface has precise control over where in the list each element is inserted. The user can access elements by their integer index (position in the list), and search for elements in the list.
Set<E>:
Answer from Andrew Hare on Stack OverflowA collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element. As implied by its name, this interface models the mathematical set abstraction.
java - What is the difference between Set and List? - Stack Overflow
When to use Sets vs Lists .
Help with setting up vscode for java on wsl
Setting JAVA_HOME path
I acknowledge the difference between them on a basic level, like how sets don't allow duplicates and aren't in order, whereas lists have an order and allow duplicates, but from a performance standpoint, what are they better at doing? For example, if I have a group of integers that I want to store in no particular order, and contain no duplicates, will it always be better to store them in a set rathern than a list (with the exception of maybe a few very special cases)?
List is an ordered sequence of elements whereas Set is a distinct list of elements which is unordered (thank you, Quinn Taylor).
List<E>:
An ordered collection (also known as a sequence). The user of this interface has precise control over where in the list each element is inserted. The user can access elements by their integer index (position in the list), and search for elements in the list.
Set<E>:
A collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element. As implied by its name, this interface models the mathematical set abstraction.
| List | Set | |
|---|---|---|
| Duplicates | Yes | No |
| Order | Ordered | Depends on implementation |
| Position Access | Yes | No |