Difference Between Set and List (With Table)

When it comes to JDK 2.0, Hashtable, Vectors, and Arrays were used to group the objects into a single unit. But in JDK 8 framework of the collection came into existence. It generally provides many interfaces to work with data collection.  

Among them, set and the list is used to group the objects. They both help to extend the collection interface. They both are two of the several data structures which are supported by c#. Both these interfaces are quite confusing but this article highlights differences between set and list to clear up the confusion. 

Set vs List 

The main difference between set and list is that set fails to allow duplicates, and if such a thing appears, then the value automatically gets overwritten. On the other hand, a list allows storing duplicate values. The iterator is the only method used by set, while both the ListIterator and iterator method are used by the list.  

Set is mutable, iterable, and an unordered collection data type. The crucial advantage of using it is that for checking a particular element contained in the set it uses highly optimized. 

A list is a collection of items or values of different types. In the list, items are separated with the help of (,) and also enclosed with the square brackets. The elements present in the list can be accessed by index. It can store several various elements.  

Comparison Table Between Set and List 

Parameters of ComparisonSetList
SequenceUnorderedOrdered
Duplicate valuesDoes not storesStores
Null valuesOnly singleHave many
Positional accessYesNo
Legacy classNoOnly vector

What is Set? 

The concept of set is also come across in the subject of mathematics and here less or more means the same. In most cases, it is a collection of similar elements and generally means a collection of elements. It can be cleared with the example:  

Input to a set: 20, 10, 30, 50  

It is stored in a set as 10, 20, 30, 50  

The elements which are sorted here are not in their insertion order. Hence, before storing the elements, it always sorts them, which is a feature. The exception here is the linkedhashSet, as it maintains the elements insertion order.  

When it comes to computer terms, a few additional properties included in this set are inheritance and methods. Functions and methods are quite similar, and they do certain tasks like removing, iterating, or adding through the set of elements.  

The term inheritance is well known to most of us, and it means the same here. It can inherit a Method from its collection and be used with the interface of the set. Set interface nothing but the whole elements set including the Methods. When it comes to ListIterator, it is not possible to use in the set of traverse the items. 

What is List? 

The list is similar to dynamically sized arrays declared in other languages ( ArrayList in Java and vector in C++). It does not need to be always homogeneous, and that’s why it is the most powerful tool in Python. In Python, lists have a definite count and are ordered.  

Lists even after their creation, can be altered because they are mutable. Are single list might contain DataTypes like strings, integers, as well as objects. The list indexing is done with 0, which is the first index. According to a definite sequence, elements are indexed in a list.  

Each element present in the list has its definite place, which allows elements duplicated in the list. Each element has its distinct credibility and place. It is a useful tool to preserve a data sequence and further iterating over it.  

There are multiple ways in Python list to print the whole list with all the present elements. When it comes to printing a particular element range from the list, then the slice operation comes into play. It is generally performed on lists with the help of a colon

Main Differences Between Set and List 

  1. The insertion order is never maintained by set, while the list maintains the insertion order. In the first place, the first inserted remains and so on irrespective related to its value.   
  2. Set fails to define the method of its own, while it restricts the collection methods to add any duplicate elements. On the flip side, some methods of the list are defined by themselves, in addition to collection-defined methods.  
  3. Set is implemented with the help of EnumSet, HashSet, CopyOnWriteArraySet, LinkedHashSet, and TreeSet. But the list is implemented with the help of Vector, CopyOnWriteArrayList, LinkedList, ArrayList, and Stack interfaces.  
  4. ListIterator is not possible to use in the set to traverse the items. On the other hand, ListIterator or Iterator is possible to use in the list to traverse the items.  
  5. Set uses methods such as add (), remove (), clear (), isEmpty (), and size (). But subList (), add (), get (), addAll (), with or without parameter remove (), lastIndexOf (), set (), and remove () are the methods used by list.  

Conclusion 

It can be concluded that both set and list are used to group the objects. The main goal is to extend the collection interface. They both are supported by C# as they are among two of the several data structures. Set contains different elements but in an unordered way, while list contains the same elements in an ordered sequence.  

Set fails to allow duplicates, and if such a thing appears, then the value automatically gets overwritten. On the other hand, a list allows storing duplicate values. In the set, there is only a single null value, whereas there are several null values present in the list. The iterator is the only method used by set, while both the ListIterator and iterator method are used by the list. In a set, elements themselves are indices, and each loop is used to access its elements. On the flip side, elements of the list are accessed using an index. 

References 

  1. https://link.springer.com/article/10.1007/s10822-015-9873-0
  2. https://aip.scitation.org/doi/pdf/10.1063/1.4822400
x
2D vs 3D