Print the ArrayList and LinkedHashSet
After converting the ArrayList to LinkedHashSet, let's print both of them to verify the unique elements.
import java.util.ArrayList;
import java.util.LinkedHashSet;
public class ArrayListToLinkedHashSet {
public static void main(String[] args) {
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("Apple");
arrayList.add("Mango");
arrayList.add("Orange");
arrayList.add("Mango");
//ArrayList to LinkedHashSet using constructor
LinkedHashSet<String> linkedHashSet = new LinkedHashSet<String>(arrayList);
//print ArrayList
System.out.println("ArrayList: " + arrayList);
//print LinkedHashSet
System.out.println("LinkedHashSet: " + linkedHashSet);
}
}