Home > Java > javaTutorial > body text

How to find the smallest or largest element from a LinkedHashSet in Java?

王林
Release: 2023-09-12 23:37:02
forward
1200 people have browsed it

How to find the smallest or largest element from a 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 collection class. It stores and returns objects in the order in which they were inserted. Finding the maximum and minimum elements from a LinkedHashSet collection is one of the common tasks that are often asked in exams and even interviews. In this article, we will explore several ways to perform a given task.

Procedure to get the minimum and maximum elements from LinkedHashSet

To find the largest and smallest element from a LinkedHashSet collection, we will use the following method:

  • Iterate through LinkedHashSet

  • By converting LinkedHashSet to TreeSet

  • By using the min() and max() methods

Let’s discuss them one by one.

Use iteration

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

Example 1

The following example demonstrates the actual implementation of a for-each loop to find the largest and smallest elements from a LinkedHashSet.

method

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

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

  • Initialize two Integer type variables to store the largest and smallest elements.

  • Now, create a for-each and define two if blocks in it. The first if block will check for the smallest element and the second if block will check for the largest element.

  • Finally print the results 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);
   }
}
Copy after login

Output

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

Use tree set

TreeSet is also a class of the Java collection framework, which implements the NavigableSet interface. It stores the elements of the set in a tree structure and in a sorted manner, which makes it the best choice for retrieving the largest and smallest elements from a LinkedHashSet.

Example 2

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

method

  • Follow the first two steps from the previous example.

  • Then, convert the LinkedHashSet set to a TreeSet so that the first element becomes the smallest and the last element becomes the largest.

  • Now, use the built-in method "first()" to get the smallest element and "last()" to get the largest 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());
   }
}
Copy after login

Output

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

Use min() and max() methods

A simple way to get the minimum and maximum elements from a LinkedHashSet is to use the built-in methods "min()" and "max()". The "min()" method returns the smallest element in the LinkedHashSet, and "max()" returns the largest element. Note that both methods work with "sets".

Example 3

In the following example, instead of iterating and transforming the LinkedHashSet collection, we will use the "min()" and "max()" methods to find its minimum and maximum elements.

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));
   }
}
Copy after login

Output

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

in conclusion

This article first introduces LinkedHashSet. In the next section, we discuss three methods to find the smallest and largest elements from LinkedHashSet. Additionally, we learned about the basics of TreeSet and some of the built-in methods of the Java Collections Framework, including “min()”, “max()”, “first()” and “last()”.

The above is the detailed content of How to find the smallest or largest element from a LinkedHashSet in Java?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!