How to declare unique ArrayList ?

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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is get () in java?

570


What is pre increment and post increment in java?

504


Tell me about different OOPS concepts.

593


What are the types of java?

605


What is module in project?

518






When a thread is executing a synchronized method , then is it possible for the same thread to access other synchronized methods of an object ?

611


Is treeset sorted in java?

584


Why string is a class?

548


How is abstraction implemented in java ?

546


Can we define private and protected modifiers for variables in interfaces?

583


What is predicate in java?

560


What is definition and declaration?

528


What variables are stored in stack?

531


What is internal iteration in java se 8?

630


What is your platform?s default character encoding and how to know this?

1753