I have a Arraylist object, it has duplecate values also. Now
question is i want delete duplecate data in that objet with
out using Set?
Answer Posted / sreenivas
Hi,
I hope the answer I am providing is the easiest one among
all others
import java.util.ArrayList;
import java.util.Iterator;
public class ArrayListDemo
{
public static void main(String[] args)
{
ArrayList containsDuplicate = new ArrayList
();
containsDuplicate.add("A");
containsDuplicate.add("B");
containsDuplicate.add("C");
containsDuplicate.add("D");
containsDuplicate.add("A");
containsDuplicate.add("B");
containsDuplicate.add("C");
containsDuplicate.add("A");
ArrayList noDuplicate = new ArrayList
(containsDuplicate);
ArrayList fresh = new ArrayList();
System.out.println("Hello World!!!!!!!!
with duplicates " + containsDuplicate.size());
System.out.println("Hello World!!!!!!!!
with duplicates " + noDuplicate.size());
for(int i =0;i<containsDuplicate.size();i++)
{
String outerObject = (String)
containsDuplicate.get(i);
int count = 0;
for(int j=0;j<noDuplicate.size
();j++)
{
String innerObject =
(String) noDuplicate.get(j);
if(outerObject.equals
(innerObject))
{
if((count == 0))
{
count++;
// fresh.add
(outerObject);
}
else
{
noDuplicate.remove(j);
}
}
}
}
for(int i=0; i< noDuplicate.size();i++)
{
System.out.println("Each
Element :::::::: " + noDuplicate.get(i));
}
}
}
| Is This Answer Correct ? | 1 Yes | 0 No |
Post New Answer View All Answers
How do you achieve polymorphism in java?
How do you include a string in java?
What are advantages of exception handling in java?
Explain the difference between runnable and callable interface in java?
Why is singleton not thread safe?
Difference between string, string builder, and string buffer?
What are the types of methods in java?
Explain the difference between collection api and stream api in java8?
Explain about OOPS concepts and fundamentals.
What is the java reflection api? Why it’s so important to have?
What is the integer of 16?
Why does java have two ways to create child threads? Which way is better?
Why is method overloading not possible by changing the return type in java?
How is string stored in java?
is there a separate stack for each thread in java? : Java thread