5:37 pm - Thursday August 17, 2017

Program for creating binary search list of data find max values. max(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

Program for creating binary search list of data find max values

Example Program


import java.util.*;

public class Collections4 {

	public static void main(String args[]) {

		// create link list object

		LinkedList<Integer> list = new LinkedList<Integer>();

		// populate the list

		list.add(-18);

		list.add(40);

		list.add(-45);

		list.add(12);

		// comparing using natural ordering

		System.out.println("Max val: " + Collections.max(list, null));

	}

}

Output

Max val: -45

 

Explanation

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.

See Also:

Comparable

BACK

Filed in: Z-A All Java Codes

Comments are closed.