This example shows how to find the minimum and maximum values from HashSet in Java. The example also shows how to get min or max element from HashSet using the Collections class.
How to find the minimum or maximum value from HashSet in Java?
There are a couple of ways using which we can find the minimum or maximum element from the HashSet in Java.
How to find the maximum value (biggest or highest value)?
1. Using the Collections class
The max
method of the Collections class returns the maximum element of the HashSet object according to the natural ordering.
1 |
public static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll) |
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.util.Collections; import java.util.HashSet; import java.util.Set; public class JavaHashSetFindMinMaxExample { public static void main(String[] args) { Set<Integer> hSetNumbers = new HashSet<Integer>(); hSetNumbers.add(34); hSetNumbers.add(13); hSetNumbers.add(42); hSetNumbers.add(45); hSetNumbers.add(12); /* * Use the max method of Collections class to * find max element */ System.out.println("Max value: " + Collections.max(hSetNumbers) ); } } |
Output
1 |
Max value: 45 |
2. By iterating the HashSet
We can also iterate the HashSet object to find the maximum element as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
Set<Integer> hSetNumbers = new HashSet<Integer>(); hSetNumbers.add(34); hSetNumbers.add(13); hSetNumbers.add(42); hSetNumbers.add(45); hSetNumbers.add(12); Integer max = null; for(Integer element : hSetNumbers){ //this will get first element in the max if(max == null){ max = element; //this will compare subsequent elements with max }else if(max < element){ max = element; } } System.out.println("Max value: " + max ); |
Output
1 |
Max value: 45 |
How to find a minimum value (smallest or lowest value)?
1. Using the Collections class
The min
method of the Collections class returns the minimum element of the HashSet object according to the natural ordering.
1 |
public static <T extends Object & Comparable<? super T>> T min(Collection<? extends T> collection) |
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Set<Integer> hSetNumbers = new HashSet<Integer>(); hSetNumbers.add(34); hSetNumbers.add(13); hSetNumbers.add(42); hSetNumbers.add(45); hSetNumbers.add(12); /* * Use the min method of Collections class to * find minimum element */ System.out.println("Min value: " + Collections.min(hSetNumbers) ); |
Output
1 |
Min value: 12 |
2. By iterating
We can also iterate through the HashSet to find the min element as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
Integer min = null; for(Integer element : hSetNumbers){ //this will get first element in the min if(min == null){ min = element; //this will compare subsequent elements with min }else if(min > element){ min = element; } } System.out.println("Min value: " + min ); |
Output
1 |
Min value: 12 |
Collections class vs Iteration Performance
The min
method and max
method of the Collections class iterates the specified collection in order to find min and max elements respectively. So, there should not be any substantial difference between using these methods and iterating the HashSet to find the min or max values as far as performance is concerned.
How to find the min or max element from the HashSet of custom class objects?
If the HashSet elements are objects of a custom class, then the custom class must implement the Comparable interface or a custom Comparator object must be specified for the min
or max
methods to work.
If the custom class has implemented the Comparable interface, you can use the same min
or max
methods. If you want to provide a custom comparator object. the overloaded min
or max
methods should be used.
1 |
public static <T> T min(Collection<? extends T> collection, Comparator<? super T> comp) |
1 |
public static <T> T max(Collection<? extends T> collection, Comparator<? super T> comp) |
I am going to show how to find min or max custom class objects using the comparator.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
import java.util.Collections; import java.util.Comparator; import java.util.HashSet; import java.util.Set; class Person{ private int age; private String name; public Person(String name, int age){ this.age = age; this.name = name; } public int getAge(){ return this.age; } public String toString(){ return "[" + this.name + "->" + this.age + "]"; } } /* * This comparator compares Person objects by age */ class PersonComparator implements Comparator<Person>{ public int compare(Person p1, Person p2) { return p1.getAge() - p2.getAge(); } } public class JavaHashSetFindMinMaxExample { public static void main(String[] args) { Set<Person> hSetPerson = new HashSet<Person>(); hSetPerson.add(new Person("Jack", 43)); hSetPerson.add(new Person("Emily", 21)); hSetPerson.add(new Person("John", 47)); hSetPerson.add(new Person("Maria", 62)); hSetPerson.add(new Person("Sheila", 51)); /* * To find person with maximum age, use * the max method and provide the comparator */ Person p = Collections.max(hSetPerson, new PersonComparator()); System.out.println("Person with max age: " + p); /* * To find person with minimum age, use the * min method and provide the comparator */ p = Collections.min(hSetPerson, new PersonComparator()); System.out.println("Person with min age: " + p); } } |
Output
1 2 |
Person with max age: [Maria->62] Person with min age: [Emily->21] |
This example is a part of the HashSet in Java Tutorial with Examples.
Please let me know your views in the comments section below.
References:
Java 8 HashSet