Get the min value from a string array with Collections.min and Comparator in Java

Description

The following code shows how to get the min value from a string array with Collections.min and Comparator.

Example


//  w  w w. j a  va 2  s  .  c o  m
import java.util.Collections;
import java.util.Set;
import java.util.TreeSet;

class Main {
  public static void main(String[] args) {
    String[] coins = { "Penny", "nickel", "dime", "Quarter", "dollar" };

    Set set = new TreeSet();
    for (int i = 0; i < coins.length; i++)
      set.add(coins[i]);

    System.out.println(Collections.min(set));
    System.out.println(Collections.min(set, String.CASE_INSENSITIVE_ORDER));

    System.out.println("");

    System.out.println(Collections.max(set));
    System.out.println(Collections.max(set, String.CASE_INSENSITIVE_ORDER));
  }
}




















Home »
  Java Tutorial »
    Java Collection »




Java ArrayList
Java Collection
Java Comparable
Java Comparator
Java HashMap
Java HashSet
Java Iterator
Java LinkedHashMap
Java LinkedHashSet
Java LinkedList
Java List
Java ListIterator
Java Map
Queue
Java Set
Stack
Java TreeMap
TreeSet