In this example we will show how to find a minimum element of HashSet with min method of Collections class in Java.
Source Code
package com.beginner.examples;
import java.util.Collections;
import java.util.HashSet;
public class FindMinimumOfHashSet {
public static void main(String[] args) {
//create HashSet.
HashSet hashSet = new HashSet();
hashSet.add(new Integer("2547"));
hashSet.add(new Integer("958"));
hashSet.add(new Integer("32587"));
/*
To find minimum element of Java HashSet use static Object min(Collection c) method of Collections.
*/
Integer result = Collections.min(hashSet);
System.out.println("The minimum element is " + result);
}
}
Output:
The minimum element is 958
References
Imported packages in Java documentation: