Set,hashset,linkedhashset,treeset

Set:

The set interface is present in java.util package and extends the Collection interface. It is an unordered collection of objects in which duplicate values cannot be stored. It is an interface that implements the mathematical set. This interface contains the methods inherited from the Collection interface and adds a feature that restricts the insertion of the duplicate elements. There are two interfaces that extend the set implementation namely SortedSet and NavigableSet.

SortedSet:[TBD]

The SortedSet interface present in java.util package extends the Set interface present in the collection framework. It is an interface that implements the mathematical set. This interface contains the methods inherited from the Set interface and adds a feature that stores all the elements in this interface to be stored in a sorted manner.

Methods of SortedSet:[TBD]

The SortedSet interface includes all the methods of the Set interface. It’s because Set is a super interface of SortedSet.

Besides methods included in the Set interface, the SortedSet interface also includes these methods:

  • comparator() – returns a comparator that can be used to order elements in the set
  • first() – returns the first element of the set
  • last() – returns the last element of the set
  • headSet(element) – returns all the elements of the set before the specified element
  • tailSet(element) – returns all the elements of the set after the specified element including the specified element
  • subSet(element1, element2) – returns all the elements between the element1 and element2 including element1

NavigableSet in Java:[TBD]

NavigableSet is a sub interface of the SortedSet interface, so it inherits all SortedSet’s behaviors like range view, endpoints and comparator access. In addition, the NavigableSet interface provides navigation methods and descending iterator that allows the elements in the set can be traversed in descending order.Let’s look at each new method defined by this interface in details.

  • lower(E): returns the greatest element which is less than the specified element E, or null if there is no such element.
  • floor(E): returns the greatest element which is less than or equal to the given element E.
  • ceiling(E): returns the least element which is greater than or equal to the given element E.
  • higher(E): returns the least element which is strictly greater than the specified element E.
  • descendingSet(): returns a reverse order view of the elements contained in the set.
  • descendingIterator(): returns an iterator that allows traversing over elements in the set in descending order.
  • pollFirst(): retrieves and removes the first (lowest) element, or returns null if the set is empty.
  • pollLast(): retrieves and removes the last (highest) element, or returns null if the set is empty.

Furthermore, the NavigableSet interface overloads the methods headSet(), subSet() and tailSet() from the SortedSet interface, which accepts additional arguments describing whether lower or upper bounds are inclusive versus exclusive:

  • headSet(E toElement, boolean inclusive)
  • subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive)
  • tailSet(E fromElement, boolean inclusive)

HashSet:

Java HashSet class is used to create a collection that uses a hash table for storage. It inherits the AbstractSet class and implements Set interface.

The important points about Java HashSet class are:

  • HashSet stores the elements by using a mechanism called hashing.
  • HashSet contains unique elements only.
  • HashSet allows null value.
  • HashSet class is non synchronized.
  • HashSet doesn’t maintain the insertion order. Here, elements are inserted on the basis of their hashcode.
  • HashSet is the best approach for search operations.
  • The initial default capacity of HashSet is 16, and the load factor is 0.75.

Constructors of Java HashSet class:

SNConstructorDescription
1)HashSet()It is used to construct a default HashSet.
2)HashSet(int capacity)It is used to initialize the capacity of the hash set to the given integer value capacity. The capacity grows automatically as elements are added to the HashSet.
3)HashSet(int capacity, float loadFactor)It is used to initialize the capacity of the hash set to the given integer value capacity and the specified load factor.
4)HashSet(Collection<? extends E> c)It is used to initialize the hash set by using the elements of the collection c.

Methods of Java HashSet class:[TBD-2,3,5,6,8,9]

Various methods of Java HashSet class are as follows:

SNModifier & TypeMethodDescription
1)booleanadd(E e)It is used to add the specified element to this set if it is not already present.
2)voidclear()It is used to remove all of the elements from the set.
3)objectclone()It is used to return a shallow copy of this HashSet instance: the elements themselves are not cloned.
4)booleancontains(Object o)It is used to return true if this set contains the specified element.
5)booleanisEmpty()It is used to return true if this set contains no elements.
6)Iterator<E>iterator()It is used to return an iterator over the elements in this set.
7)booleanremove(Object o)It is used to remove the specified element from this set if it is present.
8)intsize()It is used to return the number of elements in the set.
9)Spliterator<E>spliterator()It is used to create a late-binding and fail-fast Spliterator over the elements in the set.

LinkedHashSet In Java:

LinkedHashSet extends HashSet and implements the Set interface. It maintains insertion order for its elements. There are certain important points about the  LinkedHashSet class which is required to understand while using the LinkedHashSet class.

  • Similar to the HashSet class, the LinkedHashSet class can also hold the unique elements only.
  • It facilitates all optional set operations.
  • It is non-synchronized.
  • It allows null elements.
  • The insertion order is maintained by the LinkedHashSet class.

Constructors of Java LinkedHashSet class:

Sl.No.ConstructorDescription
1HashSet()To construct a default HashSet.
2HashSet(Collection c)To initialize the hash set by using the elements of the collection c.
3LinkedHashSet(int capacity)To initialize the capacity of the linked hash set to the given integer value capacity.
4LinkedHashSet(int capacity, float fillRatio)To initialize both the capacity and the fill ratio (also called load capacity) of the hash set from its argument.

TreeSet In Java:

TreeSet extends AbstractSet and implements the NavigableSet interface. It maintains ascending order for its elements i.e. elements will be in sorted form.

  • Just like HashSet, the Java TreeSet class contains unique elements only.
  • The access and retrieval times of the TreeSet class are very fast.
  • It does not give access to the null element.
  • It maintains the ascending order.
  • It is non-synchronized.

TreeSet class Constructors:

ConstructorDescription
TreeSet()It will create an empty tree set that will be sorted in ascending order according to the natural order of the tree set.
TreeSet(Collection<? extends E> c)It will create a new tree set that contains the elements of the collection c.
TreeSet(Comparator<? super E> comparator)It will create an empty tree set that will be sorted according to the given comparator.
TreeSet(SortedSet<E> s)It will create a TreeSet that contains the elements of the given SortedSet.

Reference:

https://www.geeksforgeeks.org/set-in-java/

https://www.codejava.net/java-core/collections/java-navigableset-and-treeset-tutorial-and-examples

https://www.programiz.com/java-programming/sortedset

https://www.w3schools.blog/treeset-in-java

https://www.w3schools.blog/linkedhashset-in-java

https://www.javatpoint.com/java-hashset

Leave a comment

Design a site like this with WordPress.com
Get started