Set (I)
Set (I)
HashSet:
● It is a predefined class present in util package.
● It will inherit the properties from the Set interface.
● It allows only unique elements and Null values.
● It follows the HashMap class to perform operations.
● Here there is no order of elements because it follows a
hashing mechanism, to be exact, it will follow a hashcode
algorithm to insert and get the values.
Constructors:
● HashSet()
● HashSet(collection)
Methods:
● public boolean isEmpty()
● public int size()
● public boolean add(Object)
● public void addAll(Collection)
● public boolean contains(Object)
● public boolean containsAll(Collection)
● public void remove(Object)
● public void removeAll(Object)
● public void clear()
● public Iterator iterator()
● public Object[] toArray()
● public int hashcode()
LinkedHashSet:
● It is a predefined class present in the util package.
● It follows a doubly linked list for adding elements.
● It does not allow duplicate values and it allows null elements.
● Here it follows the insertion order.
Constructors:
● LinkedHashSet()
● LinkedHashSet(Object)
Methods: (same as HashSet)
● public boolean isEmpty()
● public int size()
● public boolean add(Object)
● public void addAll(Collection)
● public boolean contains(Object)
● public boolean containsAll(Collection)
● public void remove(Object)
● public void removeAll(Object)
● public void clear()
● public Iterator iterator()
● public Object[] toArray()
● public int hashcode()