How to Get Min Element in a HashSet in Java


In this example we will show the method to get the min element in a HashSet in Java.

Source Code

package com.beginner.examples;

import java.util.HashSet;
import java.util.Collections;

public class MinElementOfHashSet {

  public static void main(String[] args) {

    HashSet hashs = new HashSet();
    hashs.add(3);
    hashs.add(7);
    hashs.add(20);
    hashs.add(16);
    System.out.println("Min : " + Collections.min(hashs)); // get the min element
 }
}

Output:

Min : 3

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments