Sunday 13, Apr 2025
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

Related Posts:

  • First Java Application in Eclipse In this tutorial we are going to see about "how to create Java Project in Eclipse.Step by step procedure to create Java Project using Eclipse given below. Read detail explanation of  First Java Program Follow the … Read More
  • Java 8 convert List to Map with Duplicate Keys example In this article we are going to see about how to convert a List with duplicate key into a Map using Java 8 with example program. StockMarket.java package com.javatutorialcorner.java8; public class StockMarket { private i… Read More
  • Introduction to Java Virtual Machine (JVM) What is JVM A Java virtual machine (JVM) is an abstract computing machine that enables a computer to run a Java program. There are three notions of the JVM: 1. Specification 2. Implementation 3. Instance. The specification… Read More
  • First Java Program In this tutorial we are going to see about details of "First Java Program. To read how to create and run First Java Application in Eclipse HelloWorld.java package com.javatutorialcorner.java; public class HelloWorld … Read More
  • Java Data type and Identifier In this tutorial we are going to see about Data types and Identifiers in Java. Java language has a rich implementation of data types. Data types specify size and the type of values that can be stored in an identifier. Java… Read More
  • 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