How to declare unique ArrayList ?

Answers were Sorted based on User's Feedback



How to declare unique ArrayList ?..

Answer / kiran

you have SET INTERFACE in COLLECTION in java . where u can
store unique elements and if u try to add duplicate elements
it will raise an error

Is This Answer Correct ?    10 Yes 3 No

How to declare unique ArrayList ?..

Answer / 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

How to declare unique ArrayList ?..

Answer / sudhakar

I have modified the above program. And made esay the
process.


import java.util.ArrayList;

public class UniqueArrayListValues {

static ArrayList assignValues() {
ArrayList a1 = new ArrayList();
a1.add("A");
a1.add("B");
a1.add("C");
a1.add("27");
a1.add("A");
a1.add("B");
a1.add("C");
a1.add("27");
a1.add("A");
a1.add("B");
a1.add("C");
a1.add("27");
return a1;
}

static ArrayList applyUniqueKey(ArrayList a1) {
ArrayList a2 = new ArrayList();
for (int i = 0; i < a1.size(); i++) {
if (!a2.contains(a1.get(i))) {
a2.add(a1.get(i));
}
}
return a2;
}

public static void main(String args[]) {
ArrayList beforeUniqueKey = assignValues();
System.out.println("Before applying Unique
Key:" + beforeUniqueKey);
ArrayList afterUniqueKey = applyUniqueKey
(beforeUniqueKey);
System.out.println("Afrer applying Unique
Key:" + afterUniqueKey);
}

}

Is This Answer Correct ?    5 Yes 2 No

How to declare unique ArrayList ?..

Answer / surya chandra mohan , visakhap

small modification for first program. But that one is really excellent.i really proud Indresh_ips.


import java.util.*;
class ArrayListUnique2 {

static public ArrayList uniqueArrayList(ArrayList arrl) {
int i;
String check="";
for(int k=0;k<arrl.size();k++) {
String s=arrl.get(k).toString();


if(!check.contains(s)){
check = check+s+", ";
}else{
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 ?    1 Yes 0 No

How to declare unique ArrayList ?..

Answer / amar

Excalent work friend

Is This Answer Correct ?    0 Yes 0 No

How to declare unique ArrayList ?..

Answer / ballu

i think java does not this in built facility ... you would
need to make sure that objects inserted in array are unique
( by overriding equals() and hash() method of Object class

Is This Answer Correct ?    3 Yes 6 No

Post New Answer

More Core Java Interview Questions

Justify your answer that you can't define a method inside another method in java, if you can then how?

0 Answers  


Is it possible to compare various strings with the help of == operator?

0 Answers  


What is the purpose of the finally clause?

3 Answers  


What is user defined exception?

4 Answers  


What does t in java mean?

0 Answers  






Does java map allow duplicates?

0 Answers  


why is S capital in System.out.println ????

6 Answers  


What is the return type of read()?

3 Answers  


Tell me the latest versions in java related areas?

0 Answers  


Make a data structure and implement an algorithm to print all the files in a directory. (The root directory can have sub-directories too.)

0 Answers   Amazon,


What are different ways of object creation in java ?

0 Answers  


Difference between operator overloading and function overloading

0 Answers   Tavant Technologies, Virtusa,


Categories