Java Collections find the Max/Min value in a collection

Description

Java Collections find the Max/Min value in a collection

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

public class Main {

  public static void main(String[] args) {
    HashSet<Integer> hashSet = new HashSet<>();
    hashSet.add(1);//ww  w  .ja  v  a 2 s.c  o m
    hashSet.add(2);
    hashSet.add(3);
    hashSet.add(4);
    hashSet.add(5);

    Integer obj = Collections.max(hashSet);
    System.out.println(obj);
    
    obj = Collections.min(hashSet);
    System.out.println(obj);
  }
}



PreviousNext

Related