How to avoid duplicate elements in ArrayList

How to avoid duplicate elements in ArrayList

As we know List(ArrayList, Vector, and LinkedList) allow duplicate element. Sometime in the interview, you may encounter this question how we will avoid duplicate elements from the list. In this post, we will see How to avoid duplicate elements in ArrayList. Let’s see a couple of ways to avoid duplicate elements in List.

Avoid duplicate into List by converting List into Set.

HashSet class have a constructor which can take the list as an argument.

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class ArraListExample {

	public static void main(String[] args) {

		List<String> listObject = new ArrayList<>();

		listObject.add("ram");
		listObject.add("mohan");
		listObject.add("shyam");
		listObject.add("mohan");
		listObject.add("ram");

		Iterator<String> itObjectForList = listObject.iterator();
		while (itObjectForList.hasNext()) {
			System.out.println(itObjectForList.next());
		}

		System.out.println("After converting into set ----");

		Set<String> set = new HashSet<String>(listObject);
		Iterator<String> itObjectForSet = set.iterator();
		while (itObjectForSet.hasNext()) {
			System.out.println(itObjectForSet.next());
		}
	}
}

Output is –

ram
mohan
shyam
mohan
ram
After converting into set —-
shyam
mohan
ram

Using Set’s addAll() method.

 Set interface has addAll(Collection<? extends E> c) method, we can pass listObject into addAll().

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class ArraListExample {

	public static void main(String[] args) {

		List<String> listObject = new ArrayList<>();

		listObject.add("ram");
		listObject.add("mohan");
		listObject.add("shyam");
		listObject.add("mohan");
		listObject.add("ram");

                Iterator<String> itObjectForList = listObject.iterator();
		while (itObjectForList.hasNext()) {
			System.out.println(itObjectForList.next());
		}

		System.out.println("After converting into set ----");

		Set<String> setObject = new HashSet<>();
		// Set interface has addAll(Collection<? extends E> c) method
		setObject.addAll(listObject);
		Iterator<String> itObjectForSet = setObject.iterator();

		while (itObjectForSet.hasNext()) {
			System.out.println(itObjectForSet.next());
		}
	}
}

Output is –

ram
mohan
shyam
mohan
ram
After converting into set —-
shyam
mohan
ram

Defining custom logic(using for loop).

We can define some custom logic to remove duplicate elements from ArrayList.

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ArraListExample {

	public static void main(String[] args) {

		List<String> listObject = new ArrayList<>();

		listObject.add("ram");
		listObject.add("mohan");
		listObject.add("shyam");
		listObject.add("mohan");
		listObject.add("ram");

		Iterator<String> itObjectForList = listObject.iterator();
		while (itObjectForList.hasNext()) {
			System.out.println(itObjectForList.next());
		}

		System.out.println("After removing duplicates ----");

		for (int i = 0; i < listObject.size(); i++) {

			for (int j = i + 1; j < listObject.size(); j++) {
				if (listObject.get(i) == (listObject.get(j))) {
					listObject.remove(j);
					j--;
				}
			}

		}
		System.out.println(listObject);
	}
}

Output is –

ram
mohan
shyam
mohan
ram
After removing duplicates —-
[ram, mohan, shyam]


Remove duplicate elements for user-defined object list type.

Suppose we have a custom class object in our list. How we will avoid duplicate elements in that case. As we are doing for wrapper class type of list, will it work for the custom type object? Let’s see –

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

class Book {
	String bookName;
	int bookId;

	public Book(String bookName, int bookId) {
		this.bookName = bookName;
		this.bookId = bookId;
	}

}

public class AddingCustomClassExammple {
	public static void main(String[] args) {
		List<Book> listOfBookType = new ArrayList<>();

		Book b1 = new Book("bookname1", 10);
		Book b2 = new Book("bookname2", 13);
		Book b3 = new Book("bookname3", 11);
		Book b4 = new Book("bookname4", 9);
		Book b5 = new Book("bookname2", 13);
		Book b6 = new Book("bookname3", 11);

		listOfBookType.add(b1);
		listOfBookType.add(b2);
		listOfBookType.add(b3);
		listOfBookType.add(b4);
		listOfBookType.add(b5);
		listOfBookType.add(b6);

		System.out.println("before avoiding duplicate elements - -");
		for (Book b : listOfBookType) {
			System.out.println("Book name is: - " + b.bookName + "   "
					+ "Book id is :- " + b.bookId);
		}

		System.out.println("After avoiding duplicate elements - -");

		// Same way we are going to define Set and will pass listOfBookType into
		// the set
		Set<Book> setOfBookType = new HashSet<Book>(listOfBookType);

		for (Book b : setOfBookType) {
			System.out.println("Book name is: - " + b.bookName + "   "
					+ "Book id is :- " + b.bookId);
		}

	}
}

Output is –


before avoiding duplicate elements – –
Book name is: – bookname1 Book id is :- 10
Book name is: – bookname2 Book id is :- 13
Book name is: – bookname3 Book id is :- 11
Book name is: – bookname4 Book id is :- 9
Book name is: – bookname2 Book id is :- 13
Book name is: – bookname3 Book id is :- 11
After avoiding duplicate elements – –
Book name is: – bookname1 Book id is :- 10
Book name is: – bookname2 Book id is :- 13
Book name is: – bookname3 Book id is :- 11
Book name is: – bookname4 Book id is :- 9
Book name is: – bookname2 Book id is :- 13
Book name is: – bookname3 Book id is :- 11

oops! Still, we have duplicate elements. Why the set is allowing duplicate elements in case of custom classes. Actually, all wrapper classes override equals() and hashCode() method because of that set doesn’t allow duplicate elements. So what will happen if we override equals() and hashCode() in class Book? Here we are going to override these methods with default eclipse logic(go to source/generate equals() and hashCode()). Let’s see in below example –

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

class Book {
	String bookName;
	int bookId;

	public Book(String bookName, int bookId) {
		this.bookName = bookName;
		this.bookId = bookId;
	}

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

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		Book other = (Book) obj;
		if (bookId != other.bookId) {
			return false;
		}
		if (bookName == null) {
			if (other.bookName != null) {
				return false;
			}
		} else if (!bookName.equals(other.bookName)) {
			return false;
		}
		return true;
	}

}

public class AddingCustomClassExammple {
	public static void main(String[] args) {
		List<Book> listOfBookType = new ArrayList<>();

		Book b1 = new Book("bookname1", 10);
		Book b2 = new Book("bookname2", 13);
		Book b3 = new Book("bookname3", 11);
		Book b4 = new Book("bookname4", 9);
		Book b5 = new Book("bookname2", 13);
		Book b6 = new Book("bookname3", 11);

		listOfBookType.add(b1);
		listOfBookType.add(b2);
		listOfBookType.add(b3);
		listOfBookType.add(b4);
		listOfBookType.add(b5);
		listOfBookType.add(b6);

		System.out.println("before avoiding duplicate elements - -");
		for (Book b : listOfBookType) {
			System.out.println("Book name is: - " + b.bookName + "   "
					+ "Book id is :- " + b.bookId);
		}

		System.out.println("After avoiding duplicate elements - -");

		// Same way we are going to define Set and will pass listOfBookType into
		// the set
		Set<Book> setOfBookType = new HashSet<Book>(listOfBookType);

		for (Book b : setOfBookType) {
			System.out.println("Book name is: - " + b.bookName + "   "
					+ "Book id is :- " + b.bookId);
		}

	}
}

Output is –

before avoiding duplicate elements – –
Book name is: – bookname1 Book id is :- 10
Book name is: – bookname2 Book id is :- 13
Book name is: – bookname3 Book id is :- 11
Book name is: – bookname4 Book id is :- 9
Book name is: – bookname2 Book id is :- 13
Book name is: – bookname3 Book id is :- 11
After avoiding duplicate elements – –
Book name is: – bookname4 Book id is :- 9
Book name is: – bookname2 Book id is :- 13
Book name is: – bookname3 Book id is :- 11
Book name is: – bookname1 Book id is :- 10

Now we don’t have duplicate elements.

Remove duplicates elements from list Using Java 8.

Using java8 stream().distinct().

package collectionint1;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class AvoidDulpicateJava8 {
	public static void main(String[] args) {
		List<String> list = new ArrayList<>();
		list.add("john");
		list.add("rohan");
		list.add("john");
		list.add("rohan");
		list.add("rohan");

		// Using java8 stream().distinct()
		List<String> newListUsingSteram = list.stream().distinct().collect(Collectors.toList());
		newListUsingSteram.forEach(System.out::println);

	}
}

The output of the above program is –

john
rohan

That’s all about How to avoid duplicate elements in ArrayList. You might want to check ArrayList docs.