Skip to content
Related Articles

Related Articles

Finding Minimum Element of Java HashSet
  • Difficulty Level : Medium
  • Last Updated : 28 Dec, 2020

Java HashSet class is used to create collection to be used by the collection that uses a hash table for storage purposes that uses the mechanism known as hashing. The implementation class of Set. It inherits the abstract class and implements et interface. The main trait is it does not allow duplicates and uses a hash table internally.

Illustration: find minimum element in a HashSet.

Example:

Input  : [24, 56, 87, 64, 29, 2, 65]
Output : 2

Input  : [45, 3, 65, 32, 64, 12, 43]
Output : 3

Methods: 

  • Using a For-each loop (Naive approach)
  • Using Collection.min() method of HashSet (Optimal approach)

Method 1: Using for-loop to print maximum element in a HashSet.



Approach:

  1. Create an object of HashSet
  2. Add elements to the object created
  3. Create a variable and assign it with the value MAX_VALUE
  4. Iterate using for each loop

Remember :

  • In computing maximum assign -∞ as initial max
  • In computing minimum assign +∞ as initial min

Example 1

Java




// Java Program to find minimum element on HashSet
  
// Importing all java input output classes
// Importing Collection and HashSet class from
// java.util package
import java.io.*;
import java.util.Collections;
import java.util.HashSet;
  
// Class
class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
  
        // Creating an object of HashSet (of Integer type)
        HashSet<Integer> gfg = new HashSet<Integer>();
  
        // Adding elements in above object of HashSet
        // Custom inputs
        gfg.add(24);
        gfg.add(56);
        gfg.add(87);
        gfg.add(64);
        gfg.add(29);
        gfg.add(2);
        gfg.add(65);
  
        // Print all the elements in the above HashSet
        System.out.println("Elements in HashSet = " + gfg);
  
        // Remember :
        // In computing maximum assign -∞ as initial max
        // In computing minimum assign +∞ as initial min
  
        // Initially assigning +(infinity) as max value
        // so as to get rid with garbage value issues.
        int min = Integer.MAX_VALUE;
  
        // For each loop to iterate over elements of HashSet
        // to find minimum among all elements in Set
        for (int var : gfg) {
  
            // For elements in Set
            if (var < min)
  
                // Update the minimum element
                min = var;
        }
  
        // Display final Minimum element in a HashSet
        // i.e after traversing the complete elements
        System.out.println("Minimum element in HashSet = "
                           + min);
    }
}
Output
Elements in HashSet = [64, 65, 2, 87, 24, 56, 29]
Minimum element in HashSet = 2

Time Complexity: O(n) where n is a number of elements in a HashSet.

Method 2: Using Collection.min() method of HashSet of collection class.

Syntax:

public static <T extends Object & Comparable<? super T>> T 
    min(Collection<? extends T> coll)

Parameters: This method takes the collection coll as a parameter whose minimum element is to be determined

Return Value: This method returns the minimum element of the given collection, according to the natural ordering of its elements.

Exception: This method throws NoSuchElementException if the collection is empty.

Approach:

  1. Create an object of HashSet
  2. Add elements to the object created
  3. Use Collection.min() method to display the smallest string among the non-duplicated list generated after creating. Note there will be no single duplicate element.

Example 2

Java




// Java Program to find minimum element on HashSet
  
// Importing all java input output classes
// Importing Collection and HashSet class from
// java.util package
import java.io.*;
import java.util.Collections;
import java.util.HashSet;
  
// Class
class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
  
        // Creating object of HashSet
        // Declaring String type objects
        HashSet<String> gfg = new HashSet<String>();
  
        // Adding element in above object of HashSet
        // Custom inputs
        gfg.add("Geekss");
        gfg.add("Geeks");
        gfg.add("Geek");
  
        // Print and display all elements inside the object
        // as. inserted above
        System.out.println("Elements in HashSet = " + gfg);
  
        // Using Collection.min() Method to find
        // minimum string in HashSet
        Object obj = Collections.min(gfg);
  
        // Display Maximum element in a HashSet
        System.out.println("Minimum String in a HashSet :"
                           + obj);
    }
}
Output
Elements in HashSet = [Geekss, Geek, Geeks]
Minimum String in a HashSet :Geek

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :