This is one of the good interview question. I have faced many times this question in HCL & TCS interview. So frnds, let's see the following answer:
ArrayListDemo1.java
import java.util.*;
public class ArrayListDemo1 {
public static void main(String[] args)
{
ArrayList al = new ArrayList();
al.add("Prajukta");
al.add("Bishnupriya");
al.add("Nitasha");
al.add("Prajukta");
al.add("Prajukta"); //Duplicate
al.add("Prajukta"); //Duplicate
ArrayList al2 = new ArrayList(new HashSet(al));
Iterator it= al2.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
}
Output
Prajukta
Bishnupriya
Nitasha
ArrayListDemo1.java
import java.util.*;
public class ArrayListDemo1 {
public static void main(String[] args)
{
ArrayList al = new ArrayList();
al.add("Prajukta");
al.add("Bishnupriya");
al.add("Nitasha");
al.add("Prajukta");
al.add("Prajukta"); //Duplicate
al.add("Prajukta"); //Duplicate
ArrayList al2 = new ArrayList(new HashSet(al));
Iterator it= al2.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
}
Output
Prajukta
Bishnupriya
Nitasha