List collection features:
1 is an underlying array data structure.
2 is due to the nature of the array, a random access is achieved, and faster, holding data elements in accordance with the order of insertion.
3 lower delete and move elements of performance, because it will lead to move the entire collection of elements.
4 elements of the collection can be repeated.
5 sequential.
6 thread-safe
Create a collection and add elements
List<Integer> list = new ArrayList<Integer>();
list.add(100);
list.add(200);
list.add(300);
list.add(300);
list.add(400);
list.add(600);
list.add(500);
list.add(700);
list.add(600);
1. The first embodiment: the use of a collection set
list = new ArrayList<Integer>(new LinkedHashSet<Integer>(list));
System.out.println("去重复 未排序 :" + list);
2. Using the method contains
List<Integer> withoutList = new ArrayList<Integer>();
if (list != null && list.size() > 0) {
for (int i = 0; i < list.size(); i++) {
if (!withoutList.contains(list.get(i))) {
withoutList.add(list.get(i));
}
}
}
System.out.println("去重复 未排序 :" + withoutList);
Collections.sort(withoutList);
System.out.println("去重复 已排序 :" + withoutList);
3. Using the equals method
if (list != null && list.size() > 0) {
for (int i = 0; i < list.size() - 1; i++) {
for (int j = list.size() - 1; j > i; j--) {
if (list.get(j).equals(list.get(i))) {
list.remove(j);
}
}
}
}
System.out.println("去重复 未排序 :" + list);
4. Use Collections.frequency (List, "value") value in the method of the number of List
List<Integer> withoutList = new ArrayList<Integer>();
for (Integer l : list) {
if (Collections.frequency(withoutList, l) < 1) {
withoutList.add(l);
}
}
System.out.println("去重复 未排序 :" + withoutList);
result:
Undeduplicated: [100, 200, 300, 300, 400, 600, 500, 700, 600]
deduplication unsorted: [100, 200, 300, 400, 600, 500, 700]
deduplication sorted: [100 , 200, 300, 400, 500, 600, 700]