How to declare unique ArrayList ?

Answer Posted / indresh_ips

Hey, Here is the program of Unique ArrayList.........
Just Run It........

import java.util.*;
class ArrayListUnique {

static public ArrayList uniqueArrayList(ArrayList arrl) {
int i;
for(int k=0;k<arrl.size();k++) {
Object s=arrl.get(k);
i=Collections.frequency(arrl,s);

for(int j=1;j<i;j++) {

arrl.remove(s);
}
}
return arrl;
}
public static void main (String[] args) {
ArrayList al= new ArrayList();

al.add("A");
al.add("A");
al.add("B");
al.add("C");
al.add("B");
al.add("A");
al.add("D");
al.add("D");
al.add("D");
al.add("D");
al.add("A");
al.add("B");
al.add("F");
al.add("G");
al.add("G");
al.add(10);
al.add(5);
al.add(10);
al.add(5);
System.out.println ("Before declaring unique:"+al);

ArrayList a=ArrayListUnique.uniqueArrayList(al);

System.out.println ("after declaring unique: "+a);
}
}

Is This Answer Correct ?    7 Yes 2 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is multithreading in java?

761


What is procedure overloading?

2005


Is java a security risk?

786


What is methods in java?

753


Explain the different forms of polymorphism?

761


What does the three dot emoji mean?

798


What is the purpose of encapsulation?

740


What is substring in java?

811


How to invoke external process in java.

768


What is the replace tool?

756


Which non-unicode letter characters may be used as the first character of an identifier?

787


Can a singleton class be inherited?

702


What does the ‘static’ keyword mean? Is it possible to override private or static method in java?

755


What is the difference between jfc & wfc?

787


Can we override constructor in java?

796