3:06 pm - Monday January 16, 2017

HashSet to using find Max and Min Number Max() and Min() 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 Program used to findĀ  Max and Min from hashset by using Max() and Max() method.

Example Program




import java.util.HashSet;
import java.util.Collections;

class FindMaxHashSet {
	public static void main(String[] args) { // creating Hashset Object
		HashSet hashSet = new HashSet();
		hashSet.add(new Long("00000000005"));
		hashSet.add(new Long("00000000006"));
		hashSet.add(new Long("000000000010"));
		hashSet.add(new Long("00000000008"));
		hashSet.add(new Long("00000000007"));// using collections.max() method
		// the maximum of element is
		// find
		Object obj = Collections.max(hashSet);
		System.out.println("Maximum Element of Java HashSet is : " + obj);
		Object obj1 = Collections.min(hashSet);
		System.out.println("Minmum Element of Java HashSet is : " + obj1);
	}
}

 

Output:

Maximum Element of Java HashSet is : 10

Minmum Element of Java HashSet is : 5

Declaration 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.

Declaration Min()

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

Returns the minimum 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 minimum element is to be determined.

comp – the comparator with which to determine the minimum element. A null value indicates that the elements’ natural ordering should be used.

Returns: the minimum 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.

BACK

Filed in: Z-A All Java Codes

Comments are closed.