Java Collections get max/min using Comparator

Description

Java Collections get max/min using Comparator


import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Main {
  public static void main(String args[]) {
    String str[] = { "Java", "CSS", "Javascript", "HTML", "SQL" };
    List<String> list = Arrays.asList(str);
    System.out.println("min:"+Collections.min(list));
    System.out.println("max:"+Collections.max(list));
    /*w ww . ja  v a  2 s.  c  om*/
    System.out.println();
    
    Comparator<String> comp = Collections.reverseOrder();
    System.out.println("min:"+Collections.min(list, comp));
    System.out.println("max:"+Collections.max(list, comp));
  }
}



PreviousNext

Related