We are moved to new domain
Click -> www.ehowtonow.com
Sunday, 31 May 2015

How to find min and max value from Collection

Following example shows how to find minimum and maximum of Collection using Collections.min(Collection<T> collection) and Collections.max(Collection<T> collection) method in Java.

<String> String java.util.Collections.min(Collection<? extends String> coll)
min

public 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. All elements in the collection must implement the Comparable interface. Furthermore, all elements in the collection must be mutually comparable (that is, e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the collection). This method iterates over the entire collection, hence it requires time proportional to the size of the collection.
Parameters:
    coll - the collection whose minimum element is to be determined.
Returns:
    the minimum element of the given collection, according to the natural ordering of its elements.
Throws: 
ClassCastException - if the collection contains elements that are not mutually comparable (for example, strings and integers). 
NoSuchElementException - if the collection is empty.

<String> String java.util.Collections.max(Collection<? extends String> coll, Comparator<? super String> comp)
max

public 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. All elements in the collection must be mutually comparable by the specified comparator (that is, comp.compare(e1, e2) must not throw a ClassCastException for any elements e1 and e2 in the collection). This method iterates over the entire collection, hence it requires time proportional to the size of the collection.
Parameters:
    coll - the collection whose maximum element is to be determined.
    comp - the comparator with which to determine the maximum element. A null value indicates that the elements' natural ordering should be used.
Returns:
    the maximum element of the given collection, according to the specified comparator.
Throws: 
ClassCastException - if the collection contains elements that are not mutually comparable using the specified comparator.
NoSuchElementException - if the collection is empty.

Sample Program
MinMaxCollection.java
package com.javatutorialcorner.collection;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

public class MinMaxCollection {

 public static void main(String[] args) {
  String[] nameArray = new String[] { "Spring", "Struts", "Hibernate",
    "Webservice", "SOAP Webservice", "RESTful Webservice" };
  Set<String> set = new TreeSet<String>();

  for (String name : nameArray) {
   set.add(name);
  }

  System.out.println("******** Min and Max of Set ********");

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

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

  System.out.println("******** Min and Max of List ********");
  List<String> nameList = Arrays.asList(nameArray);
  System.out.println(Collections.min(nameList));
  System.out.println(Collections.min(nameList,
    String.CASE_INSENSITIVE_ORDER));

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

}


Result
The above code will produce the following output.
******** Min and Max of Set ********
Hibernate
Hibernate
Webservice
Webservice
******** Min and Max of List ********
Hibernate
Hibernate
Webservice
Webservice

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: How to find min and max value from Collection Rating: 5 Reviewed By: eHowToNow