When you first learn about programming, you are introduced to two data structures: sets and lists. Both data structures have their own strengths and weaknesses, but which one is the best for your needs? In this blog post, we will discuss the difference between set and list and help you decide which one is best for your project. Stay tuned!
What is Set?
Set is an interface which extends Collection. It is an unordered collection of objects in which duplicate values cannot be stored. Basically, Set is implemented by HashSet, LinkedHashSet or TreeSet. Set also provides constructors to convert from other Collection objects to Set.
- Set does not maintain insertion order as by List interface. Set keeps only one copy of duplicate values. Set main purpose is to remove duplicates from a Collection.
- Set also doesn’t allow random access to elements as List provides via get() method. Set operates with Iterator, but you can not access elements by index number like you do it with ListIterator. Java Set also provides added advantage of fail-safe i.e any changes made in Set collection won’t be reflected back in original Set object.
- You have to explicitly make copy of Set if you want original set to be unaffected by any changes made on copy set object. Since Set contains unique elements, we can use Set methods like union(), intersect() and difference() to perform mathematical set operations on two different Set objects containing unique elements only.
What is List?
List is an ordered collection of elements, in which duplicate values are allowed. List maintains the insertion order, i.e., the order in which the elements were added to the List. List index starts from 0. List allows random access because it supports index-based element retrieval.
- ArrayList and LinkedList are two most commonly used List implementations in java. ArrayList creates a dynamic array of objects that means it can grow or shrink as per our need.
- In ArrayList, manipulation is slow because a lot of shifting needs to occur if any element is removed from the arraylist. ArrayList class implements List interface and it is based on an Array data structure. LinkedList creates a doubly linked list of objects.
- In LinkedList, manipulation is fast because no shifting needs to take place if any element is removed from the list. LinkedList class extends AbstractSequentialList class and implements List & Deque interfaces.
- There are several differences between List and Set which are given below: List preserves the insertion order while Set doesn’t maintain any order (the elements will be retrieved by hashedCode).
In List duplicates are allowed but in Set objects only one object is allowed for a specific value (has to be unique). List allows random access to fetch any element while Set doesn’t support it because Set uses hashing to retrieve values while List uses get(index) method – numeric indexing to retrieve an element from List .
Difference between Set and List
Set and List are two data structures available in java. Set is an unordered collection of objects in which duplicate values cannot be stored. Remember, Set doesn’t allow duplicates. List is an ordered collection of objects in which duplicates values can be stored.
- ArrayList and LinkedList are the two classes that implement List interface. TreeSet and HashSet are the classes that implement Set interface.
Now let’s see some important differences between Set and List with the help of points given below:
- – Set is an unordered collection while List is an ordered one. Set stores its elements by calculating the hash code while on the other hand; List uses doubly linked list to store its element which makes it possible to store duplicate values also.
– Set doesn’t maintain any order for storing elements while List does. However, if we want to print Set content, it gets printed in some sorted order(for ex: alphabetically for Strings).
- – In Set, we cannot access elements by their index, but in List, we can do so because it uses dynamic array (which keeps track of indexes).
– Set is implemented by HashSet, LinkedHashSet, and TreeSet while List is implemented by ArrayList and LinkedList classes.
We’ve looked at the difference between a set and a list. In essence, a set is an unordered collection of unique items while a list is an ordered collection of unique items. Sets are useful when you want to find something specific or test for membership in the set. Lists are useful when you need to keep track of things or iterate over them.