12:20 am - Monday June 26, 2017

Collections.min(Collection coll, Comparator comp)

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

BACK

Collections.min(Collection <? extends T> coll, Comparator <? super T> comp)

Example Program


import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

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

		// populate the list
		list.add(-8);
		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()));
	}
}

 

Output:

  • min val :-8

  • min val :4

Declaration:

  • public static <T> T min(Collection <? extends T> coll,  Comparator <? super T> comp)

Parameters:

  • coll– The collection whose minimum element is to be determined.

Return Value:

  •     The method call returns the minimum element of the given collection, according to the natural ordering of its elements.

Exception:

  •   ClassCastException–This is thrown if the collection contains elements that are not mutually comparable (for example, strings and integers).

  • NoSuchElementException–This is thrown if the collection is empty.

 

BACK

Filed in: Z-A All Java Codes

Comments are closed.