forked from rootusercop/IMP-Interview-Questions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Diff between Set and List
101 lines (79 loc) · 5.17 KB
/
Diff between Set and List
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
Source-> http://javarevisited.blogspot.com/2012/04/difference-between-list-and-set-in-java.html
ALL three Map,SET and LIST are interfaces in Java.
http://docs.oracle.com/javase/7/docs/api/java/util/Map.html
http://docs.oracle.com/javase/7/docs/api/java/util/Set.html
http://docs.oracle.com/javase/7/docs/api/java/util/List.html
Please Note: This is much better explanation than given in the link above.
List vs Set in Java
Difference between Set and List in Java:
Here are few note worthy differences between List and Set in Java.
Remember that both of them are used to store objects and provides convenient API(that means libraries)
to insert, remove and retrieve elements, along with to support Iteration over collection.
1) Fundamental difference between List and Set in Java is allowing duplicate elements. List in Java allows
duplicates while Set doesn't allow any duplicate. If you insert duplicate in Set it will replace the older
value with the new value. While if a duplicate is inserted in a List, it will be appended as the next
element in the List. In case of duplicates in Map, if the keys are the same(Remember, Map works on KEY-VALUE pairs),
the value of the latest will overwrite the value previously existed at that same key(Remember, Map works on KEY-VALUE pairs).
2) Another significant difference between List and Set in Java is order. List is an Ordered Collection while
Set is an unordered Collection. List maintains insertion order of elements(Example: ArrayList), means any element which is
inserted before will go on lower index than any element which is inserted after. Set in Java doesn't
maintain any order(Example: HashSet). Though Set provide another alternative called SortedSet which can store Set elements
in specific Sorting order defined by Comparable and Comparator methods of Objects stored in Set.
3) Set uses equals() method to check uniqueness of elements stored in Set, while SortedSet uses compareTo()
method to implement natural sorting order of elements. In order for an element to behave properly in Set and
SortedSet, equals and compareTo must be consistent to each other.
4) Popular implementation of List interface in Java includes ArrayList, Vector and LinkedList. While popular
implementation of Set interface includes HashSet, TreeSet and LinkedHashSet.
Read more: http://javarevisited.blogspot.com/2012/04/difference-between-list-and-set-in-java.html#ixzz2qF8yRnUr
Code to test the statements written above:
import java.awt.List;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;
public class Difference {
public static void main(String[] args) {
Set<Integer> sets=new HashSet<Integer>();
sets.add(1);
sets.add(1);
System.out.println(sets.size());
LinkedList<Integer> lists=new LinkedList<Integer>();
lists.add(1);
lists.add(1);
System.out.println(lists.size());
Map<Integer, Integer> maps=new HashMap<Integer, Integer>();
maps.put(1, 1);
maps.put(1, 2); // Just to check whether the pevious VALUE at the same key is over written or not.
System.out.println(maps.size());
System.out.println(maps.get(1)); // Output comes 2, hence the pevious VALUE at the same key is over written
}
}
/*
Output
1 //size of Set
2 //size of List
1 //size of Map
2 // value at key 1 of the Map
*/
Ordered lists of element (unique or not)
Conform to Java's interface named List
Can be accessed by index
LinkedList
ArrayList
Lists of unique elements:
Conform to Java's interface named Set
Can not be accessed by index
HashSet (unordered)
LinkedHashSet (ordered)
TreeSet (sorted by natural order or by provided comparator)
Both interfaces Set and List conform to Java's interface named Collection
╔═══════════════════╦══════════════════════╦═════════════════════════════╗
║ ║ List ║ Set ║
╠═══════════════════╬══════════════════════╬═════════════════════════════╣
║ Duplicates ║ YES ║ NO ║
╠═══════════════════╬══════════════════════╬═════════════════════════════╣
║ Order ║ ORDERED ║ DEPENDS ON IMPLEMENTATION ║
╠═══════════════════╬══════════════════════╬═════════════════════════════╣
║ Positional Access ║ YES ║ NO ║
╚═══════════════════╩══════════════════════╩═════════════════════════════╝