Remove duplicates from an Array List:
==============================
package tesngdemos;
import java.util.ArrayList;
import java.util.HashSet;
public class ArrayListDemo {
public static void main(String[] args) {
ArrayList arrayList = new ArrayList();
arrayList.add(10);
arrayList.add(10);
arrayList.add(20);
arrayList.add(20);
arrayList.add(30);
arrayList.add(30);
arrayList.add("selenium by ramesh");
arrayList.add("selenium by ramesh");
arrayList.add("selenium by ramesh");
arrayList.add("selenium by ramesh");
System.out.println(arrayList);
HashSet hashSet = new HashSet();
for (int i = 0; i < arrayList.size(); i++) {
hashSet.add(arrayList.get(i));
}
System.out.println(hashSet);
}
}
==============================
package tesngdemos;
import java.util.ArrayList;
import java.util.HashSet;
public class ArrayListDemo {
public static void main(String[] args) {
ArrayList arrayList = new ArrayList();
arrayList.add(10);
arrayList.add(10);
arrayList.add(20);
arrayList.add(20);
arrayList.add(30);
arrayList.add(30);
arrayList.add("selenium by ramesh");
arrayList.add("selenium by ramesh");
arrayList.add("selenium by ramesh");
arrayList.add("selenium by ramesh");
System.out.println(arrayList);
HashSet hashSet = new HashSet();
for (int i = 0; i < arrayList.size(); i++) {
hashSet.add(arrayList.get(i));
}
System.out.println(hashSet);
}
}
No comments:
Post a Comment