Best way to remove duplicate items from ArrayList in Java


Removing duplicate items from ArrayList in Java can be done in some very efficient ways using Java 8 or Guava lib for Java.

Guava lib provides many efficient util classes to perform different operations on collections like creating, sorting, reversing, partitioning, etc.

Removing duplicate using Guava API

Here is the most efficient way to remove duplicate items from ArrayList using the Guava Lib ImmutableSet class.

empList = ImmutableSet.copyOf(empList).asList();
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;

public class GuavaRemoveDuplicateExample {
	public static void main(final String[] args) {

		List<Integer> list = Lists.newArrayList(10, 20, 3, 4, 10, 20, 3, 4);
		list = ImmutableSet.copyOf(list).asList();
		System.out.println(list);

		List<Employee> empList = Lists.newArrayList(new Employee("ssp", 1), new Employee("ssp", 1),
				new Employee("ssp", 2));

		empList = ImmutableSet.copyOf(empList).asList();

		System.out.println(empList);

		final List<Employee> empList1 = new ArrayList<Employee>(new LinkedHashSet<Employee>(empList));
		System.out.println(empList1);
	}
}

class Employee {
	String name;
	int id;

	public Employee(final String name, final int id) {
		super();
		this.name = name;
		this.id = id;
	}

	public String getName() {
		return name;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		return result;
	}

	@Override
	public boolean equals(final Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		final Employee other = (Employee) obj;
		if (id != other.id) {
			return false;
		}
		return true;
	}

	public void setName(final String name) {
		this.name = name;
	}

	public int getId() {
		return id;
	}

	public void setId(final int id) {
		this.id = id;
	}

	@Override
	public String toString() {
		return "Employee [name=" + name + "]";
	}

}

This can be achieved by following using LinkedHashSet also.

final List empList1 = new ArrayList(new LinkedHashSet(empList));

Removing duplicate from ArrayList using Java8 Stream API

Here is an example

import static java.util.Comparator.comparingInt;
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toCollection;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;

import com.google.common.collect.Lists;

public class GuavaRemoveDuplicateExample {
	public static void main(final String[] args) {

		final List<Employee> empList = Lists.newArrayList(new Employee("ssp", 1), new Employee("ssp", 1),
				new Employee("ssp", 2));

		final List<Employee> unique = empList.stream().collect(
				collectingAndThen(toCollection(() -> new TreeSet<>(comparingInt(Employee::getId))), ArrayList::new));
		System.out.println(unique);
	}
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.