How to find distinct elements in a list in Java


Java 8 introduced distinct() method in Stream interface to find the distinct element in a list. distinct() method returns a stream consisting of the distinct elements of this stream.

1- Stream.distinct()

In this example, we have a list of the company where duplicate elements present in the list.

DistinctExample.java
package org.websparrow;

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

public class DistinctExample {

	public static void main(String[] args) {

		List<String> companyList = Arrays.asList(
				"Websparrow", "Google", "Microsoft", 
				"Websparrow", "Adobe", "Google", "Websparrow");

		// count distinct element in list
		long count = companyList.stream().distinct().count();
		System.out.println("Total distinct element in list: " + count);

		// looping distinct element
		companyList.stream().distinct().forEach(c -> System.out.println(c));

	}
}

Output- You will get the number of distinct company and thier name as well.

Total distinct element in list: 4

Websparrow
Google
Microsoft
Adobe

count() method returns the count of elements in this stream.

2- Stream.distinct() with custom Object

Stream.distinct() method can be also used with custom object list. Make sure your class will override hashCode() and equals() in order to get distinct elements. Learn more about hashCode() and equals() method.

POJO class.

Car.java
package org.websparrow;

public class Car {

	private String model;
	private int modelYear;
	private String manufacturer;

	public Car(String model, int modelYear, String manufacturer) {
		this.model = model;
		this.modelYear = modelYear;
		this.manufacturer = manufacturer;
	}

	public String getModel() {
		return model;
	}

	public int getModelYear() {
		return modelYear;
	}

	public String getManufacturer() {
		return manufacturer;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((manufacturer == null) ? 0 : manufacturer.hashCode());
		result = prime * result + ((model == null) ? 0 : model.hashCode());
		result = prime * result + modelYear;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Car other = (Car) obj;
		if (manufacturer == null) {
			if (other.manufacturer != null)
				return false;
		} else if (!manufacturer.equals(other.manufacturer))
			return false;
		if (model == null) {
			if (other.model != null)
				return false;
		} else if (!model.equals(other.model))
			return false;
		if (modelYear != other.modelYear)
			return false;
		return true;
	}

}

Implementation class where we find distinct cars.

CustomDistinctExample.java
package org.websparrow;

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

public class CustomDistinctExample {
	public static void main(String[] args) {
		List<Car> carList = Arrays.asList(
				new Car("Face", 2019, "Jaguar"),
				new Car("X1", 2012, "BMW"),
				new Car("C-Class", 2010, "Mercedes"),
				new Car("Face", 2019, "Jaguar"),
				new Car("X1", 2012, "BMW"));

		// count distinct element in list
		long count = carList.stream().distinct().count();
		System.out.println("Distinct cars in list: " + count);

		// looping all distinct car
		carList.stream().distinct().forEach(car -> System.out.println(
				"Model:" + car.getModel() + ", Model Year:"
				+ car.getModelYear() + ", Manufacturer:" + car.getManufacturer()));

	}

}

Output- You will get the number of distinct cars and their name as well.

Distinct cars in list: 3

Model:Face, Model Year:2019, Manufacturer:Jaguar
Model:X1, Model Year:2012, Manufacturer:BMW
Model:C-Class, Model Year:2010, Manufacturer:Mercedes

References

  1. Interface Stream<T>
  2. Stream.distinct()
  3. Stream.count()

Similar Posts

About the Author

Atul Rai
I love sharing my experiments and ideas with everyone by writing articles on the latest technological trends. Read all published posts by Atul Rai.