How to Find the Minimum or Maximum Element from LinkedHashSet in Java?


LinkedHashSet is a class of Java Collection Framework that implements the Set interface and extends the HashSet class. It is a linked list type of collection class. It stores and returns the objects in the order in which they were inserted. Finding maximum and minimum elements from a LinkedHashSet collection is one of the common tasks that are frequently asked in exams and even in job interviews. In this article, we are going to explore a few approaches for performing the given task.

Program to get Minimum and Maximum elements from LinkedHashSet

To find the maximum and minimum element from LinkedHashSet collection, we are going to use the following ways:

  • By iterating over the LinkedHashSet

  • By converting LinkedHashSet to TreeSet

  • By using min() and max() methods

Let's discuss them one by one.

Using iteration

With the help of for-each loop, we can iterate over the elements of LinkedHashSet collection to get the maximum and minimum elements.

Example 1

The following example demonstrates the practical implementation of for-each loop in finding maximum and minimum elements from a LinkedHashSet.

Approach

  • Our first step is to import the 'java.util' package so that we can use the features of LinkedHashSet class.

  • Then, create a LinkedHashSet collection and store a few elements using the built-in method 'add()'.

  • Initialize two variables of Integer type to store the maximum and minimum element.

  • Now, create a for-each and define two if block inside it. The first if block will check the minimum element and the second one will check maximum element.

  • In the end, print the result and exit.

import java.util.*;
public class MinMax {
   public static void main(String[] args) {
      // Creating a LinkedHashSet
      LinkedHashSet<Integer> lHset = new LinkedHashSet<>();
      // Adding elements to the set
      lHset.add(57);
      lHset.add(68);
      lHset.add(79);
      lHset.add(88);
      lHset.add(95);
      // to store the maximum and minimum element
      Integer minElem = null;
      Integer maxElem = null;
      // using for-each loop to find minimum and maximum elements
      for (Integer element : lHset) { 
         // checking minimum element
         if (minElem == null || element < minElem) {
            minElem = element;
         }
         // checking maximum element
         if (maxElem == null || element> maxElem) {
            maxElem = element;
         }
      }
      System.out.println("List of elements from LinkedHashSet: " + lHset);
      System.out.println("The Minimum element from LinkedHashSet: " + minElem);
      System.out.println("The Maximum element from LinkedHashSet: " + maxElem);
   }
}

Output

List of elements from LinkedHashSet: [57, 68, 79, 88, 95]
The Minimum element from LinkedHashSet: 57
The Maximum element from LinkedHashSet: 95

Using TreeSet

TreeSet is also a class of Java Collection Framework that implements NavigableSet Interface. It stores the elements of the set in a tree structure and also in a sorted manner that makes it an optimal choice for retrieving maximum and minimum elements from LinkedHashSet.

Example 2

In the following example, we will use the TreeSet for getting minimum and maximum elements from LinkedHashSet.

Approach

  • Follow the first two steps of the previous example.

  • Then, convert the LinkedHashSet collection into TreeSet so that the first element becomes minimum and the last becomes maximum.

  • Now, use the in-built method 'first()' to get the minimum element and 'last()' to get the maximum element.

import java.util.*;
public class MinMax {
   public static void main(String[] args) {
      // Creating a LinkedHashSet
      LinkedHashSet<Integer> lHset = new LinkedHashSet<>();
      // Adding elements to the set
      lHset.add(57);
      lHset.add(68);
      lHset.add(79);
      lHset.add(88);
      lHset.add(95);
      System.out.println("List of elements from LinkedHashSet: " + lHset);
      // converting LinkedHashSet to TreeSet 
      TreeSet<Integer> newTree = new TreeSet<>(lHset);
      // getting the minimum element
      System.out.println("The Minimum element from LinkedHashSet: " + newTree.first());
      // getting the maximum element
        System.out.println("The Maximum element from LinkedHashSet: " + newTree.last());
   }
}

Output

List of elements from LinkedHashSet: [57, 68, 79, 88, 95]
The Minimum element from LinkedHashSet: 57
The Maximum element from LinkedHashSet: 95

Using min() and max() methods

One naive approach to get the minimum and maximum element from LinkedHashSet is to use the built-in methods 'min()' and 'max()'. The 'min()' method returns the minimum element from LinkedHashSet and 'max()' returns the maximum element. Note that both methods are used with 'Collections'.

Example 3

In the following example, instead of iterating and converting the LinkedHashSet collection, we will use 'min()' and 'max()' methods for finding its minimum and maximum element.

import java.util.*;
public class MinMax {
   public static void main(String[] args) {
      // Creating a LinkedHashSet
      LinkedHashSet<Integer> lHset = new LinkedHashSet<>();
      // Adding elements to the set
      lHset.add(57);
      lHset.add(68);
      lHset.add(79);
      lHset.add(88);
      lHset.add(95);
      System.out.println("List of elements from LinkedHashSet: " + 
lHset);
      // getting the minimum element
      System.out.println("The Minimum element from LinkedHashSet: " + 
Collections.min(lHset));
      // getting the maximum element
      System.out.println("The Maximum element from LinkedHashSet: " + 
Collections.max(lHset));
   }
}

Output

List of elements from LinkedHashSet: [57, 68, 79, 88, 95]
The Minimum element from LinkedHashSet: 57
The Maximum element from LinkedHashSet: 95

Conclusion

We started this article by introducing LinkedHashSet and in the next section, we discussed three approaches to find the minimum and maximum element from LinkedHashSet. Also, we discovered the basics of TreeSet and a few built-in methods of the Java collection framework including 'min()', 'max()', 'first()' and 'last()'.

Updated on: 20-Jul-2023

51 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements