Set vs List vs Map - Yash-777/LearnJava GitHub Wiki
Collection: Group of objects get stored into some specific location
medium.com | |
---|---|
Interface | Hash Table | Resizable Array | Balanced Tree | Linked List | Hash Table + Linked List |
---|---|---|---|---|---|
Set | HashSet | TreeSet | LinkedHashSet | ||
List | ArrayList | LinkedList | |||
Deque | ArrayDeque | LinkedList | |||
Map | HashMap | TreeMap | LinkedHashMap |
Collection framework mainly consists of four interfaces: h2kinfosys.com
-
List: It contains elements arranged in sequential order, and these elements can be accessed by using their index position (starting from 0). It implements ArrayList, Vector, and VectorList. Elements can be inserted in the list at any position but can be retrieved in the same order in which they are added. It can also store duplicate elements.
-
Queue: Queue contains similar elements in which new elements are added from one end, whereas removed from the other end (FIFO – First In First Out). It implements LinkedList and PriorityQueue.
-
Set: Set is used to store the collection of unique elements. Duplicacy is not allowed in the Set. It does not store the elements in sequential order. While accessing the elements, we may not get the elements in the same order in which they are stored. HashSet, LinkedHashSet, and TreeSet implements the Set interface. Elements in the Set can only be iterated using Iterator.
-
Map: Map stores the elements in the form of Key/Value pairs. It is extended by SortedMap and implemented by HashMap and HashTable. It does not contain duplicate values, and each key can, at most, store one value.
A List is an ordered Collection (sometimes called a sequence). Lists may contain duplicate elements.
The Java platform contains two general-purpose List implementations. ArrayList, which is usually the better-performing implementation, and LinkedList which offers better performance under certain circumstances.
A Set is a Collection that cannot contain duplicate elements.
The Java platform contains three general-purpose Set implementations: HashSet, TreeSet, and LinkedHashSet.
- HashSet, which stores its elements in a hash table, is the best-performing implementation; however it makes no guarantees concerning the order of iteration.
- TreeSet, which stores its elements in a red-black tree, orders its elements based on their values; it is substantially slower than HashSet.
- LinkedHashSet, which is implemented as a hash table with a linked list running through it, orders its elements based on the order in which they were inserted into the set (insertion-order). LinkedHashSet spares its clients from the unspecified, generally chaotic ordering provided by HashSet at a cost that is only slightly higher.
public class HashSet<E>
extends AbstractSet<E>
implements Set<E>, Cloneable, java.io.Serializable
{
private transient HashMap<E,Object> map;
// Dummy value to associate with an Object in the backing Map
private static final Object PRESENT = new Object();
// Constructs a new, empty set; the backing {@code HashMap} instance has default initial capacity (16) and load factor (0.75).
public HashSet() {
map = new HashMap<>();
}
// * Constructs a new set containing the elements in the specified collection.
// * The HashMap is created with default load factor (0.75) and an initial capacity sufficient to contain the elements in the specified collection.
public HashSet(Collection<? extends E> c) {
map = new HashMap<>(Math.max((int) (c.size()/.75f) + 1, 16));
addAll(c);
}
public boolean add(E e) {
return map.put(e, PRESENT)==null; // key, PRESENT(Dummy Object)
}
public boolean remove(Object o) {
return map.remove(o)==PRESENT;
}
public class LinkedHashSet<E>
extends HashSet<E>
implements Set<E>, Cloneable, java.io.Serializable {
public LinkedHashSet() {
super(16, .75f, true);
}
public interface NavigableSet<E> extends SortedSet<E> extends Set<E>
public class TreeSet<E> extends AbstractSet<E>
implements NavigableSet<E>, Cloneable, java.io.Serializable
{
// The backing map.
private transient NavigableMap<E,Object> m;
// Dummy value to associate with an Object in the backing Map
private static final Object PRESENT = new Object();
// Constructs a set backed by the specified navigable map.
TreeSet(NavigableMap<E,Object> m) {
this.m = m;
}
public boolean add(E e) {
return m.put(e, PRESENT)==null;
}
Setwiki
In mathematics, a set is a collection of distinct objects, considered as an object in its own right. For example, the numbers 2, 4, and 6 are distinct objects when considered separately, but when they are considered collectively they form a single set of size three, written {2,4,6}.
Set | List |
---|---|
Set is stored in unordered way and does not allow duplicate values | List is used to store elements in ordered way and it does allow duplicate values. |
Set elements cannot be accessed by an index position | List elements can be accessed with an index position. |
Real world example: A school contains different classes, if the class strength is more then they make sessions like VII(A), VII(B)
|
But in a class their may a persons with same name but they where diff from their roll numbers |
[I, II, V, VII(A), VII(B)] |
Class room seating capacity 50, if exceeds then make new session. [4, 1, 4, 0, 1, 2, 4, 0, 0],[3, 4, 3, 2, 0, 4, 0, 1, 3, 1, 3]
|
Map: entery
Student [Roll No, Name], roll number is unique and Names may be same.