top of page

Set interface in java - java coding help

What is a set?

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.


The navigable set extends the sorted set interface. Since a set doesn’t retain the insertion order, the navigable set interface provides the implementation to navigate through the Set. The class which implements the navigable set is a TreeSet which is an implementation of a self-balancing tree. Therefore, this interface provides us with a way to navigate through this tree.


Declaration: The Set interface is declared as:

public interface Set extends Collection 

Creating Set Objects:

Since Set is an interface, objects cannot be created of the typeset. We always need a class that extends this list in order to create an object.

Set<Obj> set = new HashSet<Obj> (); 

Methods provided by set interface:


  • add(element): This method is used to add a specific element to the set. The function adds the element only if the specified element is not already present in the set else the function returns False if the element is already present in the Set.

  • addAll(collection): This method is used to append all of the elements from the mentioned collection to the existing set. The elements are added randomly without following any specific order.

  • clear(): This method is used to remove all the elements from the set but not delete the set. The reference for the set still exists.

  • contains(element): This method is used to check whether a specific element is present in the Set or not.

  • containsAll(collection): This method is used to check whether the set contains all the elements present in the given collection or not. This method returns true if the set contains all the elements and returns false if any of the elements are missing.

  • hashCode(): This method is used to get the hashCode value for this instance of the Set. It returns an integer value which is the hashCode value for this instance of the Set.

  • isEmpty(): This method is used to check whether the set is empty or not.

  • iterator(): This method is used to return the iterator of the set. The elements from the set are returned in a random order.

  • remove(element): This method is used to remove the given element from the set. This method returns True if the specified element is present in the Set otherwise it returns False.

  • removeAll(collection): This method is used to remove all the elements from the collection which are present in the set. This method returns true if this set changed as a result of the call.

  • retainAll(collection): This method is used to retain all the elements from the set which are mentioned in the given collection. This method returns true if this set changed as a result of the call.

  • size(): This method is used to get the size of the set. This returns an integer value which signifies the number of elements.

  • toArray(): This method is used to form an array of the same elements as that of the Set.

Program for the demonstration of set:


import java.util.*;
 
public class codersArts{
   
    public static void main(String[] args)
    {
        
        Set<String> hash_Set = new HashSet<String>();
        hash_Set.add("Coders");
        hash_Set.add("Arts");
        hash_Set.add("Arts");
        hash_Set.add("Welcomes");
        hash_Set.add("you");
        
         System.out.println(hash_Set);
    }
}

Output:

[Coders, Arts, Welcomes, you]

Classes that implement the Set interface:

  • HashSet

  • EnumSet

  • LinkedHashSet

  • TreeSet

Class 1: HashSet

HashSet class which is implemented in the collection framework is an inherent implementation of the hash table data structure. The objects that we insert into the HashSet do not guarantee to be inserted in the same order. The objects are inserted based on their hashcode. This class also allows the insertion of NULL elements.


Class 2: EnumSet

EnumSet class which is implemented in the collections framework is one of the specialized implementations of the Set interface for use with the enumeration type. It is a high-performance set implementation, much faster than HashSet. All of the elements in an enum set must come from a single enumeration type that is specified when the set is created either explicitly or implicitly.


Class 3: LinkedHashSet

LinkedHashSet class which is implemented in the collections framework is an ordered version of HashSet that maintains a doubly-linked List across all elements. When the iteration order is needed to be maintained this class is used. When iterating through a HashSet the order is unpredictable, while a LinkedHashSet lets us iterate through the elements in the order in which they were inserted. Let’s see how to create a set object using this class.


Class 4: TreeSet

TreeSet class which is implemented in the collections framework and implementation of the SortedSet Interface and SortedSet extends Set Interface. It behaves like a simple set with the exception that it stores elements in a sorted format. TreeSet uses a tree data structure for storage. Objects are stored in sorted, ascending order. But we can iterate in descending order using the method TreeSet.descendingIterator(). Let’s see how to create a set object using this class.


How does CodersArts help you in Java coding?


CodersArts provide :


  • Java assignment Help

  • Help in Java development Projects

  • Mentorship from Experts Live 1:1 session

  • Course and Project completions

  • CourseWork help


Comments


bottom of page