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

What is the primary benefit of encapsulation?

0 Answers  


Difference between JVM and JRE?

3 Answers   Amdocs,


What are the 7 types of characters?

0 Answers  


Why vector is used in java?

0 Answers  


What is the importance of finally block in exception handling?

0 Answers  


What is compiler and what its output.

0 Answers   Cognizant,


How to create a fecelet view?

0 Answers  


If a class is declared without any access modifiers, where may the class be accessed in java programming?

0 Answers  


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

0 Answers  


How do you convert boolean to boolean?

0 Answers  


Difference between Application and Applet ?

4 Answers   Wipro,


How to sort a vector elements that contains the user define class object? (Note: If Suppose consider, A Student class contain two data members. They are String studentName and int rollNo. I am creating Four objects for this class, each object contains students details like name and roll no. Now i am storing that objects in vector and if i retiving the elements from the vector means then it should be display in sorting order)

3 Answers   ProdEx Technologies,


Categories