Open In App

Finding Maximum Element of Java HashSet

Improve
Improve
Like Article
Like
Save
Share
Report

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 maximum element in a HashSet.

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

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

Methods: 

  • Using a For-each loop (Naive approach)
  • Using Collection.max() 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  MIN_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 maximum 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 deal with garbage value issues.
        int max = Integer.MIN_VALUE;
  
        // For each loop to iterate over elements of HashSet
        // to find maximum among all elements in Set
        for (int var : gfg) {
  
            // For elements in Set
            if (var > max)
  
                // Update the current maximum element
                max = var;
        }
  
        // Display and print the
        // maximum element in a HashSet
        System.out.println("Maximum element in HashSet = "
                           + max);
    }
}


Output

Elements in HashSet = [64, 65, 2, 87, 24, 56, 29]
Maximum element in HashSet = 87

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

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

Syntax:

public static <T extends Object & Comparable> T max(Collection coll)

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

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

Exception: This method throws the following Exception:

Approach:

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

Example 2

Java




// Java Program to find maximum 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
        System.out.println("Elements in HashSet = " + gfg);
  
        // Using Collection.max() Method to find
        // max string in HashSet
        Object obj = Collections.max(gfg);
  
        // Display Maximum element in a HashSet
        System.out.println("Maximum element :" + obj);
    }
}


Output

Elements in HashSet = [Geekss, Geek, Geeks]
Maximum element :Geekss


Last Updated : 13 Jan, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads