Example usage for java.util Collections min

List of usage examples for java.util Collections min

Introduction

In this page you can find the example usage for java.util Collections min.

Prototype

public static <T extends Object & Comparable<? super T>> T min(Collection<? extends T> coll) 

Source Link

Document

Returns the minimum element of the given collection, according to the natural ordering of its elements.

Usage

From source file:Main.java

public static void main(String[] args) {
    Integer[] numbers = { 8, 2, 6, 7, 0, 1, 4, 9, 5, 3 };

    int min = (int) Collections.min(Arrays.asList(numbers));
    int max = (int) Collections.max(Arrays.asList(numbers));

    System.out.println("Min number: " + min);
    System.out.println("Max number: " + max);
}

From source file:Main.java

public static void main(String args[]) {
    String str[] = { "B", "H", "L", "M", "R" };
    List list = Arrays.asList(str);
    System.out.println(Collections.min(list));
    System.out.println(Collections.max(list));
    Comparator comp = Collections.reverseOrder();
    System.out.println(Collections.min(list, comp));
    System.out.println(Collections.max(list, comp));
}

From source file:Main.java

public static void main(String[] args) {
    Vector<Double> v = new Vector<Double>();
    v.add(new Double("3.4"));
    v.add(new Double("3.3"));
    v.add(new Double("4.4"));
    v.add(new Double("5.3"));
    v.add(new Double("3.3"));
    Object obj = Collections.min(v);
    System.out.println(obj);/*from w  ww  .  ja v  a  2 s. c  o m*/
}

From source file:MainClass.java

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]);/*from   ww  w  .  j av  a2s  .  co m*/

    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));
}

From source file:Main.java

public static void main(String[] args) {
    HashSet<Long> hashSet = new HashSet<Long>();

    hashSet.add(new Long("9"));
    hashSet.add(new Long("4"));
    hashSet.add(new Long("2"));
    hashSet.add(new Long("2"));
    hashSet.add(new Long("3"));

    Object obj = Collections.min(hashSet);
    System.out.println("Minimum Element of Java HashSet is : " + obj);
}

From source file:Main.java

public static void main(String[] args) {
    HashSet<Long> hashSet = new HashSet<Long>();

    hashSet.add(new Long("9"));
    hashSet.add(new Long("4"));
    hashSet.add(new Long("2"));
    hashSet.add(new Long("2"));
    hashSet.add(new Long("3"));

    Object obj = Collections.min(hashSet);
    System.out.println("Minimum Element of HashSet is : " + obj);
}

From source file:Main.java

public static void main(String args[]) {
    // create link list object 
    List<Integer> list = new LinkedList<Integer>();

    // populate the list  
    list.add(-8);// w ww .  ja v  a2  s.c o  m
    list.add(4);
    list.add(-5);
    list.add(1);

    System.out.println("Min value is: " + Collections.min(list));
}

From source file:Main.java

public static void main(String[] args) {
    ArrayList<Integer> arrayList = new ArrayList<Integer>();

    arrayList.add(new Integer("1"));
    arrayList.add(new Integer("2"));
    arrayList.add(new Integer("3"));
    arrayList.add(new Integer("4"));
    arrayList.add(new Integer("5"));

    Object obj = Collections.min(arrayList);
    System.out.println(obj);/*from   w ww  .j  av a 2  s. c om*/
}

From source file:Main.java

public static void main(String args[]) {
    // create link list object 
    List<Integer> list = new LinkedList<Integer>();

    // populate the list  
    list.add(-8);/*from w ww.  ja  v a2 s . co m*/
    list.add(4);
    list.add(-5);
    list.add(2);

    // comparing using natural ordering
    System.out.println("Min val: " + Collections.min(list));
    System.out.println("Min val: " + Collections.min(list, Collections.reverseOrder()));
}

From source file:AlgorithmsDemo.java

public static void main(String args[]) {

    LinkedList<Integer> ll = new LinkedList<Integer>();
    ll.add(-8);/*  w ww  . jav  a 2 s.co  m*/
    ll.add(20);
    ll.add(-20);
    ll.add(8);

    Comparator<Integer> r = Collections.reverseOrder();

    Collections.sort(ll, r);

    for (int i : ll)
        System.out.print(i + " ");

    Collections.shuffle(ll);

    System.out.print("List shuffled: ");
    for (int i : ll)
        System.out.print(i + " ");

    System.out.println("Minimum: " + Collections.min(ll));
    System.out.println("Maximum: " + Collections.max(ll));
}