12:27 am - Monday May 1, 2017

This Collection Program is used to get the max elements using max() Method

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

This Collection Program is used to get the max elements using max() Method

Example Program


import java.util.*;

public class CollectionsDemo {

	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 :40

Declaration:

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

Parameters:

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

Return Value:

  •       The method call returns the maximum element of the given collection, according to the specified comparator.

Exception:

  •       ClassCastException–This is thrown if the collection contains elements that are not mutually comparable using the specified comparator.

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

 

BACK

import java.util.*;

public class CollectionsDemo {

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));

}
}

Filed in: Z-A All Java Codes

Comments are closed.