Find Max and Min elements of HashSet in Java
HashSet implements the Set interface, backed by a hash table (actually a HashMap instance).
We can add null elements in HashSet.
Table of Content :
- Introduction
- Java program to find Max and Min elements of HashSet
- Questions Related to find Max and Min Elements of HashSet in Java
- Summary
To get the maximum element of HashSet, use the Collections.max() method.
To get the min element of HashSet, use the Collections.max() method.
import java.util.*;
public class HashSetExample {
public static void main(String[] args) {
// Create HashSet Objects
HashSet<Integer> set = new HashSet<Integer>();
set.add(123);
set.add(45);
set.add(74);
set.add(67);
set.add(54);
set.add(98);
set.add(78);
System.out.println("the Min element in the HasSet is : " + Collections.max(set));
// Print Maximum value using max method of Collections class
//
System.out.println("the Min element in the HashSet is : " + Collections.min(set));
}
output:
the Min element in the HashSet is : 123
the Min element in the HashSet is : 45
If we have HashSet of DataType Long, still we can use the same methods to find out max and Min elements.
Questions Related Find Max and Min Elements of HashSet in Java
What is the difference between List and Set in Java?
Three Cursors in Java Collections
Iterable interface in java
In this article, we have seen the How to find Max and Min elements of HashSet in Java.
0 Comments
Post a Comment