Friday 29 October 2021

Java: How to find the minimum 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 minimum element.

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

	T min = null;

	for (T ele : elements) {

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

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

	}

	return min;

}


Find the below working application.

 

FindMinElement1.java

package com.sample.app.collections;

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

public class FindMinElement1 {

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

		T min = null;

		for (T ele : elements) {

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

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

		}

		return min;

	}

	public static void main(String args[]) {

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

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

	}

}

 

Output

minInteger -> 1

 

Approach 2: Using 'Collections.min' method.

 

Collections class provides ‘min' method to get the minimum element from a collection.

 

public static <T extends Object & Comparable<? super T>> T min(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 min(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.

 

FindMinElement2.java

 

package com.sample.app.collections;

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

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

		return Collections.min(elements);

	}

	public static void main(String args[]) {

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

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

	}

}

 

Output

minInteger -> 1

 

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

 

Example

 

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

 

Find the below working application.

 

FindMinElement3.java

package com.sample.app.collections;

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

public class FindMinElement3 {

	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 minPerson = Collections.min(persons, (person1, person2) -> {
			return person1.age.compareTo(person2.age);
		});

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

}

Output

minPerson -> Person [name=Prahlad, age=21, id=4]

 


You may like

Interview Questions

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?

Java: How to find maximum element in a collection?

No comments:

Post a Comment