RemDupFromList.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
| package java4s;import java.util.ArrayList;import java.util.HashSet;import java.util.Iterator;import java.util.List;public class RemDupFromList { public static void main(String[] args) { List li = new ArrayList(); li.add("one"); li.add("two"); li.add("three"); li.add("one");//Duplicate li.add("one");//Duplicate // We have facility to pass a List into Set constructor and vice verse to cast List li2 = new ArrayList(new HashSet(li)); //no order // List li2 = new ArrayList(new LinkedHashSet(li)); //If you need to preserve the order use 'LinkedHashSet' Iterator it= li2.iterator(); while(it.hasNext()) { System.out.println(it.next()); } }}Explanation
|
No comments:
Post a Comment