This example shows how to find the minimum or maximum element from LinkedHashSet in Java. This example also shows how to find min or max elements from LinkedHashSet using the Collections class.
How to find the minimum or maximum element from LinkedHashSet in Java?
There are a couple of ways using which we can find min or max values from the LinkedHashSet as given below.
How to find the Maximum element (max element, largest element, biggest element)?
1. Using the max method of the Collections class
The max
method of the Collections class returns the maximum element of the specified collection object.
1 |
public static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll) |
The maximum element is decided based on the element’s natural order.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.util.Collections; import java.util.LinkedHashSet; public class LinkedHashSetFindMinMax { public static void main(String[] args) { LinkedHashSet<Integer> lhSetNumbers = new LinkedHashSet<Integer>(); lhSetNumbers.add(26); lhSetNumbers.add(23); lhSetNumbers.add(24); lhSetNumbers.add(21); lhSetNumbers.add(25); lhSetNumbers.add(22); //using the max method System.out.println("Max element: " + Collections.max(lhSetNumbers)); } } |
Output
1 |
Max element: 26 |
2. By iterating the LinkedHashSet
We can also iterate through elements of LinkedHashSet object using the enhanced for loop or an iterator and 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 |
LinkedHashSet<Integer> lhSetNumbers = new LinkedHashSet<Integer>(); lhSetNumbers.add(26); lhSetNumbers.add(23); lhSetNumbers.add(24); lhSetNumbers.add(21); lhSetNumbers.add(25); lhSetNumbers.add(22); Integer max = null; for(Integer currentElement : lhSetNumbers){ //for the first element if(max == null){ max = currentElement; }else if(max < currentElement ){ max = currentElement; } } System.out.println( "Max element: " + max ); |
Output
1 |
Max element: 26 |
How to find the Minimum element (min element, lowest element, smallest element)?
1. Using the min method of the Collections class
The min
method of the Collections class returns the minimum element of the specified collection object.
1 |
public static <T extends Object & Comparable<? super T>> T min(Collection<? extends T> coll) |
The minimum element is decided based on the element’s natural order.
1 2 3 4 5 6 7 8 9 10 11 |
LinkedHashSet<Integer> lhSetNumbers = new LinkedHashSet<Integer>(); lhSetNumbers.add(26); lhSetNumbers.add(23); lhSetNumbers.add(24); lhSetNumbers.add(21); lhSetNumbers.add(25); lhSetNumbers.add(22); //using the min method System.out.println("Min element: " + Collections.min(lhSetNumbers)); |
Output
1 |
Min element: 21 |
2. By iterating the LinkedHashSet
We can also iterate through elements of LinkedHashSet object using the enhanced for loop or an iterator and find the minimum 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 |
LinkedHashSet<Integer> lhSetNumbers = new LinkedHashSet<Integer>(); lhSetNumbers.add(26); lhSetNumbers.add(23); lhSetNumbers.add(24); lhSetNumbers.add(21); lhSetNumbers.add(25); lhSetNumbers.add(22); Integer min = null; for(Integer currentElement : lhSetNumbers){ //for the first element if(min == null){ min = currentElement; }else if(min > currentElement ){ min = currentElement; } } System.out.println( "Max element: " + min ); |
Output
1 |
Min element: 21 |
How to find min or max element from the LinkedHashSet of custom class objects?
If the LinkedHashSet elements are objects of a custom class, then the custom class either must implement the Comparable interface for the min
and max
methods to work or a custom Comparator must be provided in the overloaded min
or max
method that also accepts a comparator object.
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 will show you how to use the Comparable interface to find min or max element of the custom class.
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 |
import java.util.Collections; import java.util.LinkedHashSet; /* * Step 1: Implement Comparable interface */ class Student implements Comparable<Student>{ private int id; public Student(int id){ this.id = id; } public int getId(){ return this.id; } public String toString(){ return "Student -> " + getId(); } /* * Step 2: Define the compareTo method */ public int compareTo(Student otherStudent) { return this.getId() - otherStudent.getId(); } } public class LinkedHashSetFindMinMax { public static void main(String[] args) { LinkedHashSet<Student> lhSetStudents = new LinkedHashSet<Student>(); lhSetStudents.add(new Student(4)); lhSetStudents.add(new Student(1)); lhSetStudents.add(new Student(2)); lhSetStudents.add(new Student(5)); lhSetStudents.add(new Student(3)); /* * Once we implement the Comparable interface, the min * and max methods work as expected. */ System.out.println("Student with max id: " + Collections.max(lhSetStudents)); System.out.println("Student with min id: " + Collections.min(lhSetStudents)); } } |
Output
1 2 |
Student with max id: Student -> 5 Student with min id: Student -> 1 |
We can also provide a custom comparator object in the overloaded min
and max
methods instead of implementing the Comparable interface in our custom class.
This example is a part of the LinkedHashSet in Java Tutorial with Examples.
Please let me know your views in the comments section below.
References:
Java 8 LinkedHashSet