Skip to content
Related Articles

Related Articles

How to Find the Minimum or Maximum Element from LinkedHashSet in Java?
  • Last Updated : 11 Dec, 2020

Given a LinkedHashSet the task is to find the minimum and maximum element from this LinkedHashSet in Java. There are two ways to find the minimum or maximum element from LinkedHashSet in Java as follows

  1. Using the min and max method of the Collections class
  2. By Iterating the LinkedHashSet

Examples:

Input : LinkedHashset : {1, 56, 8, 24, 50}
Ouput: Maximum Element: 56
       Minimum Element: 1

Input : LinkedHashset : {2, 34, 100, 29, 30}
Ouput: Maximum Element: 100
       Minimum Element: 2

Approach 1:

The max and min method of the Collections class returns the maximum and minimum element of the specified collection object.

Following Java code finds the minimum and maximum element from LinkedHashSet using predefined min, max method of the Collections class.



Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to find the minimum or 
// maximum element from LinkedHashSe
import java.io.*;
import java.util.*;
  
class Main {
    public static void main(String[] args)
    {
  
        // create a new LinkedHashSet
        LinkedHashSet<Integer> lhs
            = new LinkedHashSet<Integer>();
  
        // add elements to LinkedHashSet
        lhs.add(1);
        lhs.add(56);
        lhs.add(8);
        lhs.add(24);
        lhs.add(50);
  
        // finding maximum and minimum using inbuilt
        // functions
        int maximum = Collections.max(lhs);
        int minimum = Collections.min(lhs);
  
        System.out.println("Maximum element: " + maximum);
        System.out.println("Minimum element: " + minimum);
    }
}

chevron_right


Output

Maximum element: 56
Minimum element: 1

Approach 2:

Iterate over the LinkedHashSet using enhanced for loop or iterator and find the maximum and minimum element.

Consider the following Java code which finds the maximum and minimum element by iterating over each element in the LinkedHashSet.

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to find the minimum or 
// maximum element from LinkedHashSet
import java.io.*;
import java.util.*;
  
class Main {
    public static void main(String[] args)
    {
  
        // create a new LinkedHashSet
        LinkedHashSet<Integer> lhs
            = new LinkedHashSet<Integer>();
  
        // add elements to LinkedHashSet
        lhs.add(1);
        lhs.add(56);
        lhs.add(8);
        lhs.add(24);
        lhs.add(50);
  
        // Iterate over the LinkedHashSet
        int maximum = Integer.MIN_VALUE;
        int minimum = Integer.MAX_VALUE;
  
        for (Integer num : lhs) {
            if (maximum < num)
                maximum = num;
        }
  
        for (Integer num : lhs) {
            if (minimum > num)
                minimum = num;
        }
  
        System.out.println("Maximum element: " + maximum);
        System.out.println("Minimum element: " + minimum);
    }
}

chevron_right


Output

Maximum element: 56
Minimum element: 1

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :