For being familiar with an ArrayList and HashSet in Java collection framework, I suggest reading Java Collection Framework and ArrayList and HashSet Example in Java.
Popular Tutorials
Example for Removing Duplicate Items from ArrayList
It very simple example for removing duplicate items from ArrayList. Here I have an ArrayList with 5 items String type. This ArrayList has some duplicate items. Let’s see in the below java code.
import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Set; /** * */ /** * @author Dinesh.Rajput * */ public class DuplicateItemsRemove { /** * @param args */ public static void main(String[] args) { //ArrayList with duplicates items List<String> listOfDuplicateItems = (List<String>) Arrays.asList("DOJ" , "DOJ", "MB", "DR", "AM"); //should print 5 with duplicates items System.out.println("size of Arraylist with duplicates: " + listOfDuplicateItems.size()); System.out.println("ArrayList with duplicates: " + listOfDuplicateItems); //Converting ArrayList to HashSet to remove duplicates Set<String> listToSet = new HashSet<String>(listOfDuplicateItems); //Creating Arraylist without duplicate items List<String> listOfItems = new ArrayList<String>(listToSet); //should print 4 because of duplicates items are removed System.out.println("size of ArrayList without duplicates: " + listToSet.size()); System.out.println("ArrayList after removing duplicates in same order: " + listOfItems); } }
Output:
size of Arraylist with duplicates: 5
ArrayList with duplicates: [DOJ, DOJ, MB, DR, AM]
size of ArrayList without duplicates: 4
ArrayList after removing duplicates in same order: [DR, DOJ, AM, MB]
According above output of the program we have removed duplicate items but also we have lost our insertion order of this list because I have converted ArrayList to HashSet. We can maintain our insertion order as well if we use LinkedHashSet instead of HashSet, Which guarantees insertion order. Let’s see below example of java code.
import java.util.ArrayList; import java.util.Arrays; import java.util.LinkedHashSet; import java.util.List; import java.util.Set; /** * */ /** * @author Dinesh.Rajput * */ public class DuplicateItemsRemove { /** * @param args */ public static void main(String[] args) { //ArrayList with duplicates items List<String> listOfDuplicateItems = (List<String>) Arrays.asList("DOJ" , "DOJ", "MB", "DR", "AM"); //should print 5 with duplicates items System.out.println("size of Arraylist with duplicates: " + listOfDuplicateItems.size()); System.out.println("ArrayList with duplicates: " + listOfDuplicateItems); //Converting ArrayList to LinkedHashSet to remove duplicates Set<String> listToSet = new LinkedHashSet<String>(listOfDuplicateItems); //Creating Arraylist without duplicate items List<String> listOfItems = new ArrayList<String>(listToSet); //should print 4 because of duplicates items are removed System.out.println("size of ArrayList without duplicates: " + listToSet.size()); System.out.println("ArrayList after removing duplicates in same order: " + listOfItems); } }
Output
size of Arraylist with duplicates: 5
ArrayList with duplicates: [DOJ, DOJ, MB, DR, AM]
size of ArrayList without duplicates: 4
ArrayList after removing duplicates in same order: [DOJ, MB, DR, AM]
Now according above output of the program we have removed duplicate items and also maintain our insertion order of this list.
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…