How to Get Max Element in a HashSet in Java


In this example, we will learn the method to get the max element in a HashSet in Java.

Source Code

package com.beginner.examples;

import java.util.HashSet;
import java.util.Collections;

public class MaxElementOfHashSet {

  public static void main(String[] args) {

    HashSet hashs = new HashSet();
    hashs.add(3);
    hashs.add(7);
    hashs.add(20);
    hashs.add(16);
    System.out.println("Max : " + Collections.max(hashs)); // get the max element
 }
}

Output:

Max : 20

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments