Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
Find maximum element of HashSet in Java
To get the maximum element of HashSet, use the Collections.max() method.
Declare the HashSet −
Set<Integer> hs = new HashSet<Integer>();
Now let us add the elements −
hs.add(29); hs.add(879); hs.add(88); hs.add(788); hs.add(456);
Let us now get the maximum element −
Object obj = Collections.max(hs);
The following is an example to find maximum element of HashSet −
Example
import java.util.*;
public class Demo {
public static void main(String args[]) {
// create hash set
Set<Integer> hs = new HashSet<Integer>();
hs.add(29);
hs.add(879);
hs.add(88);
hs.add(788);
hs.add(456);
System.out.println("Elements in set = "+hs);
// finding maximum element
Object obj = Collections.max(hs);
System.out.println("Maximum Element = "+obj);
}
}
Output
Elements in set = [788, 88, 456, 29, 879] Maximum Element = 879
Advertisements
