Friday 29 October 2021

Java: How to find maximum element in a collection?

In this post, I am going to explain different approaches to find the maximum element from a collection.

 


Approach 1: Iterate over a collection and find the maximum element.

private static <T extends Comparable<T>> T findMaxElement(Collection<T> elements) {
	if (elements == null || elements.isEmpty()) {
		return null;
	}

	T max = null;

	for (T ele : elements) {

		if (max == null) {
			max = ele;
			continue;
		}

		if (max.compareTo(ele) < 0) {
			max = ele;
		}

	}

	return max;

}

 

Find the below working application.

 

FindMaxElement1.java

package com.sample.app.collections;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;

public class FindMaxElement1 {

	private static <T extends Comparable<T>> T findMaxElement(Collection<T> elements) {
		if (elements == null || elements.isEmpty()) {
			return null;
		}

		T max = null;

		for (T ele : elements) {

			if (max == null) {
				max = ele;
				continue;
			}

			if (max.compareTo(ele) < 0) {
				max = ele;
			}

		}

		return max;

	}

	public static void main(String args[]) {

		List<Integer> integers = Arrays.asList(1, 3, 5, 2, 31, 87, 6, 5);
		Integer maxInteger = findMaxElement(integers);

		System.out.println("maxInteger -> " + maxInteger);

	}

}

Output

maxInteger -> 87


Approach 2: Using 'Collections.max' method.

Collections class provides ‘max' method to get the maximum element from a collection.

 

public static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll)

Returns the maximum element of the given collection, according to the natural ordering of its elements. To use this method, elements in the collection must implement Comparable interface.

 

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

Returns the maximum element of the given collection, according to the logic supplied by the custom comparator.

 

FindMaxElement2.java

package com.sample.app.collections;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

public class FindMaxElement2 {
	private static <T extends Comparable<T>> T findMaxElement(Collection<T> elements) {
		if (elements == null || elements.isEmpty()) {
			return null;
		}

		return Collections.max(elements);

	}

	public static void main(String args[]) {

		List<Integer> integers = Arrays.asList(1, 3, 5, 2, 31, 87, 6, 5);
		Integer maxInteger = findMaxElement(integers);

		System.out.println("maxInteger -> " + maxInteger);

	}

}


Output

maxInteger -> 87


Let’s supply a custom comparator and find out the maximum person object from the collection of persons. A person p1 is greater than person p2, if p1’s age is greater than p2.

 

Example

Person maxPerson = Collections.max(persons, (person1, person2) -> {
	return person1.age.compareTo(person2.age);
});


FindMaxElement3.java

package com.sample.app.collections;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class FindMaxElement3 {

	private static class Person {
		private String name;
		private Integer age;
		private Integer id;

		public Person(String name, Integer age, Integer id) {
			super();
			this.name = name;
			this.age = age;
			this.id = id;
		}

		@Override
		public String toString() {
			return "Person [name=" + name + ", age=" + age + ", id=" + id + "]";
		}

	}

	public static void main(String args[]) {

		Person p1 = new Person("Krishna", 33, 1);
		Person p2 = new Person("Ram", 34, 2);
		Person p3 = new Person("Soundarya", 53, 3);
		Person p4 = new Person("Prahlad", 21, 4);

		List<Person> persons = Arrays.asList(p1, p2, p3, p4);

		Person maxPerson = Collections.max(persons, (person1, person2) -> {
			return person1.age.compareTo(person2.age);
		});

		System.out.println("maxPerson -> " + maxPerson);
	}

}


Output

maxPerson -> Person [name=Soundarya, age=53, id=3]

 

 

You may like

Interview Questions

Synthetic constructs in Java

What is bridge method in Java?

How HashSet behave while adding a duplicate value?

How to call a method asynchronously in Java?

How method overloading works with null values in Java?

No comments:

Post a Comment