How to Find the Minimum or Maximum Element from LinkedHashSet in Java? Last Updated : 10 May, 2021 Comments Improve Suggest changes Like Article Like Report 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 Using the min and max method of the Collections classBy Iterating the LinkedHashSetExamples: Input : LinkedHashset : {1, 56, 8, 24, 50} Output: Maximum Element: 56 Minimum Element: 1 Input : LinkedHashset : {2, 34, 100, 29, 30} Output: Maximum Element: 100 Minimum Element: 2Approach 1: The max and min method of the Collections class returns the maximum and minimum element of the specified collection object. Collections.max(Object obj): Accepts the collection object.Collections.min(Object obj): Accepts the collection object.Following Java code finds the minimum and maximum element from LinkedHashSet using predefined min, max method of the Collections class. Java // 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); } } OutputMaximum element: 56 Minimum element: 1Approach 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 // 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); } } OutputMaximum element: 56 Minimum element: 1 Comment More infoAdvertise with us Next Article How to Remove Elements from a LinkedHashMap in Java? S swapnilbuchke Follow Improve Article Tags : Java Java Programs java-LinkedHashSet Practice Tags : Java Similar Reads How to Find the Minimum or Maximum Element from the Vector in Java? The Collection is a framework offered by Java that provides an architecture to store a group of objects. One such collection is Vector(). There are many ways through which we can find the minimum and maximum elements in a Vector. These methods have been discussed below: Methods: Using Collection.min 3 min read How to Get the Last Element from LinkedHashSet in Java? The LinkedHashSet is an ordered version of HashSet that maintains a doubly-linked List across all elements. When the iteration order is needed to be maintained this class is used. When iterating through a HashSet the order is unpredictable, while a LinkedHashSet lets us iterate through the elements 2 min read How to Find the Element Index in LinkedHashSet in Java? LinkedHashSet is used to store distinct items and get the items in which order they were inserted in Java. LinkedHashSet does not store values based on the index. But there are some methods to find the element index in LinkedHashSet in Java. Method 1: (By converting LinkedHashSet to ArrayList) To fi 4 min read How to Remove Elements from a LinkedHashMap in Java? A LinkedHashMap is a part of the Collection Framework from java.util package Java and is similar to a HashMap, except that a LinkedHashMap preserves the insertion order among the keys/entries. In this article, we will look at how to remove the elements from a LinkedHashMap in Java. Program to Remove 2 min read How to Print LinkedHashSet Elements in Java? LinkedHashSet is a child class of HashSet in which duplicates are not allowed but the insertion order is preserved. The elements are printed in the same order in which they were inserted. There are several ways to print LinkedHashSet elements: By simply printing the elementsBy using enhanced for loo 4 min read How to Find User Defined Objects From LinkedHashSet in Java? LinkedHashSet is used to store elements in which order they were inserted. When the iteration order is needed to be maintained this class is used. When iterating through a HashSet the order is unpredictable, while a LinkedHashSet lets us iterate through the elements in the order in which they were i 3 min read Like