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.
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)?
java - What is the difference between Set and List? - Stack Overflow
java - Performance and Memory allocation comparison between List and Set - Stack Overflow
[Java] When to use List, ArrayList etc. ?
[Java] Having trouble passing an array list into a JList
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 |
HashSet consumes about 5.5 times more memory than ArrayList for the same number of elements (although they're both still linear), and has significantly slower iteration (albeit with the same asymptotics); a quick Google search suggests a 2-3x slowdown for HashSet iteration versus ArrayList.
If you don't care about uniqueness or the performance of contains, then use ArrayList.
If you don't care about the ordering, and don't delete elements, then it really boils down to whether you need to find elements in this data structure, and how fast you need those lookups to be.
Finding an element by value in a HashSet is O(1). In an ArrayList, it's O(n).
If you are only using the container to store a bunch of unique objects, and iterate over them at the end (in any order), then arguably ArrayList is a better choice since it's simpler and more economical.