How to declare unique ArrayList ?
Answers were Sorted based on User's Feedback
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 |
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 |
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 |
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 |
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 |
Justify your answer that you can't define a method inside another method in java, if you can then how?
Is it possible to compare various strings with the help of == operator?
What is the purpose of the finally clause?
What is user defined exception?
What does t in java mean?
Does java map allow duplicates?
why is S capital in System.out.println ????
What is the return type of read()?
Tell me the latest versions in java related areas?
Make a data structure and implement an algorithm to print all the files in a directory. (The root directory can have sub-directories too.)
What are different ways of object creation in java ?
Difference between operator overloading and function overloading
0 Answers Tavant Technologies, Virtusa,