Remove duplicate elements from arraylist in java

Example:

package com.w3schools;
 
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;
 
public class Test {
	public static void main(String args[]){
		ArrayList<String> arrayList = new ArrayList<String>();
		arrayList.add("Jai");
		arrayList.add("Mahesh");
		arrayList.add("Vivek");
		arrayList.add("Naren");
		arrayList.add("Hemant");
		arrayList.add("Vishal");
		arrayList.add("Vishal");
		arrayList.add("Naren");
		System.out.println("Actual ArrayList:"+arrayList);
                //Prior to Java 8
		List<String> newList1 = new ArrayList<>(new HashSet<>(arrayList));
		System.out.println("Modified ArrayList:"+newList1);
		//Using Java 8
		List<String> newList2 = arrayList.stream().distinct().collect(Collectors.toList());
		System.out.println("Modified ArrayList:"+newList2);
	}
}

Output

Actual ArrayList:[Jai, Mahesh, Vivek, Naren, Hemant, Vishal, Vishal, Naren]
Modified ArrayList:[Naren, Jai, Vivek, Hemant, Mahesh, Vishal]
Modified ArrayList:[Jai, Mahesh, Vivek, Naren, Hemant, Vishal]