javacodex.com
Java Examples
Java Examples
memu home questions

Find Minimum and Maximum Element in a Set

This java example shows how to find the minimum and maximum of a Set using the Collections class.

Source: (MinMax.java)

import java.util.*;
 
public class MinMax {
   public static void main(String args[]){
      HashSet<Integer> set = new HashSet<Integer>();
      set.add(10);
      set.add(5);
      set.add(7);
      set.add(100);
 
      System.out.println("Set: " + set);
      System.out.println("Min: " + Collections.min(set));
      System.out.println("Max: " + Collections.max(set));
   }
}
 

Output:

# java MinMax 
Set: [100, 5, 7, 10]
Min: 5
Max: 100

Contact: javacodex@yahoo.com