Difference Between List vs. Set in Java

Advertisement

Main Difference

The difference between list and set in java is that list is a collection of ordered element and elements are added, removed and accessed with the help of an index variable whereas a set is a collection of objects in which there is no duplicate element.

Advertisement

List vs. Set in Java

Java is an object-oriented programming language that uses both compiler and interpreter. Mostly all software’s are made in Java programming language. Java code can be written on Windows, Linux, and Mac OS. The syntax of C and C++ programming language is quite the same. Java produces browser to run programs that help in making a graphical user interface. Java programming language is in used and trend these days. To write Java code, a programmer needs software development kit (SDK) that includes a compiler, interpreter that is not required in C++. List and set are important terms in Java if we talk about the main difference then the main difference between list and set in java is that list is a collection of ordered element and elements are added, removed and accessed with the help of an index variable whereas set is a collection of objects in which there is no duplicate element. The list is an interface that extends collection interface. List is a collection of ordered element and elements are added, removed and accessed with the help of an index variable. There cannot be any duplicate elements in sets but there can be duplicate elements in list. There are some common methods in list that are index-based get() and set() method. Set is a collection of objects in which there is no duplicate element. Two references cannot refer to one single object and vice versa. Reference cannot be a null reference. There is no method to add elements in set it has add() method and addall() method. Duplicates are not allowed in sets. HashSet, LinkedHashset, EnumSet, TreeSet, CopyOnWriteArraySET. There are also add() method in list that is used to add elements in list and other method is remove() that is used to remove elements in the list. We can also say that list is a kind of array that can grow size as we add elements in the list. There is no method to set the range of indexed in a list. There is just a sub list() method that returns sub list from the original list of a specific range.

Advertisement

Comparison Chart

BasisList in JavaSet in Java
MeaningThe list is a collection of ordered element and elements are added, removed and accessed with the help of an index variableSet is a collection of objects in which there is no duplicate element.
DuplicateList in java can have duplicateSet in java cannot have duplicate
MethodsThe list can have additional methodsSet can have additional methods
ExampleExample of list in java is Vector and StackExample of set in java is TreeSet

What is List in Java?

List is an interface that extends collection interface. List is a collection of ordered element and elements are added, removed and accessed with the help of an index variable. There cannot be any duplicate elements in sets but there can be duplicate elements in list. There are some common methods in list that are index-based get() and set() method. There are also add() method that is used to add elements in the list, and another method removes () that is used to remove elements in the list. We can also say that list is a kind of array that can grow size as we add elements in the list. There is no method to set the range of indexed in a list. There is just a sub list() method that returns sub list from the original list of a specific range.

Advertisement
  1. What is set in Java?

Set is a collection of objects in which there is no duplicate element. Two references cannot refer to a one single object and vice versa. Reference cannot be a null reference. There is no method to add elements in set it have add() method and addall() method. Duplicates are not allowed in sets. HashSet, LinkedHashset, EnumSet, TreeSet, CopyOnWriteArraySET.

Key Differences

  1. The list is a collection of ordered element and elements are added, removed and accessed with the help of an index variable whereas Set is a collection of objects in which there is no duplicate element.
  2. List in java can have duplicate whereas Set in java cannot have a duplicate.
  3. The list can have additional methods whereas Set can have additional methods.
  4. Example of the list in java is Vector and Stack whereas Example of set in java is TreeSet

Video Explanation