Get the max and min value from a Collection

static<T extends Object & Comparable<? super T>>T max(Collection<? extends T> coll)
Returns the maximum element of the given collection, according to the natural ordering of its elements.
static<T> T max(Collection<? extends T> coll, Comparator<? super T> comp)
Returns the maximum element of the given collection, according to the order induced by the specified comparator.
static<T extends Object & Comparable<? super T>>T min(Collection<? extends T> coll)
Returns the minimum element of the given collection, according to the natural ordering of its elements.
static<T> T min(Collection<? extends T> coll, Comparator<? super T> comp)
Returns the minimum element of the given collection, according to the order induced by the specified comparator.

  import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;

public class Main {
  public static void main(String args[]) {
    LinkedList<Integer> list = new LinkedList<Integer>();
    list.add(-8);
    list.add(20);
    list.add(-20);
    list.add(8);

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

    Collections.sort(list, r);

    System.out.print("List sorted in reverse: ");
    System.out.print(list);

    System.out.println();

    Collections.shuffle(list);

    System.out.print("List shuffled: ");

    System.out.print(list);

    System.out.println();

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

The output:


List sorted in reverse: [20, 8, -8, -20]
List shuffled: [-20, 20, -8, 8]
Minimum: -20
Maximum: 20
Home 
  Java Book 
    Collection  

Collections:
  1. Collections
  2. Get empty collection from Collections
  3. Do binary search
  4. Copy value to a List
  5. Get Enumeration from collection, create list from Enumeration
  6. Fill a list
  7. Get the max and min value from a Collection
  8. Fill n Copy object to a list
  9. Replace value in a list
  10. Reverse a list
  11. Get comparator in reverse order
  12. Rotate and shuffle a list
  13. Create singleton
  14. Sort a list
  15. Swap element in a list
  16. Get a synchronized collection
  17. Return an unmodifiable view of collections