there are some duplicate values in ArrayList, how U'll get
that array with out duplicate?

Answers were Sorted based on User's Feedback



there are some duplicate values in ArrayList, how U'll get that array with out duplicate?..

Answer / sai



ArrayList list = new ArrayList();
list.add("a");
list.add("b");
list.add("a");
list.add("c");
list.add("b");
HashSet set = new HashSet(list);
list.clear();
list.addAll(set);
System.out.println("ArrayList : ");
for (Object data : list) {
System.out.println(data);

Is This Answer Correct ?    13 Yes 2 No

there are some duplicate values in ArrayList, how U'll get that array with out duplicate?..

Answer / phani raju

ArrayList list = new ArrayList();
list.add("a");
list.add("b");
list.add("a");
list.add("c");
list.add("b");
LinkedHashSet lset = new LinkedHashSet
(list);
list.clear();
list.addAll(lset);
System.out.println("ArrayList : ");
for (Object data : list) {
System.out.println(data);

Is This Answer Correct ?    5 Yes 0 No

there are some duplicate values in ArrayList, how U'll get that array with out duplicate?..

Answer / shilpa

List<Integer> arlList=new ArrayList<Integer>();
arlList.add(20);
arlList.add(2);
arlList.add(1);
arlList.add(6);
arlList.add(6);
Set<Integer> tr=new TreeSet<Integer>(arlList);
Iterator<Integer> iterator=tr.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}

Is This Answer Correct ?    4 Yes 0 No

there are some duplicate values in ArrayList, how U'll get that array with out duplicate?..

Answer / banti

public void removeDuplicateDataFromArrayList(){
ArrayList<String> al = new ArrayList<>();
al.add("a");
al.add("b");
al.add("c");
al.add("c");
al.add("a");

HashSet<String> hs = new HashSet<>();
hs.addAll(al);
al.clear();
al.addAll(hs);
System.out.println("The ArrayList values are :"+ al);

// Or

System.out.println("ArrayList : ");
for (Object data : al) {
System.out.println(data);
}

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More Core Java Interview Questions

Can we access a database using applets?

2 Answers   Hewitt,


Explain where variables are created in memory?

0 Answers  


transaction attributes ?

2 Answers  


diffrence b\w println() and printf()

8 Answers   NIIT,


What does exclamation mean in java?

0 Answers  






can two class in a code be public??if yes then how??

2 Answers  


What is set string?

0 Answers  


Convert a binary search tree to a sorted doubly linked list inplace.

1 Answers   Amazon,


What happens when a thrown exception is not handled?

0 Answers   Wipro,


What are features of java?

0 Answers  


What do you mean by mnemonics?

0 Answers  


Is string an object?

0 Answers  


Categories