Java中List集合元素去重
1. for循环添加去重
这个是最基本的实现了,创建一个空的 List,添加前判断一下存在不存在,不存在才添加,这样就保证了元素不重复。
public static void main(String[] args) {
List<Integer> initList = Arrays.asList(1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6);
test1(initList);
}
/**
* for循环添加去重
*
* @param initList
*/
public static void test1(List<Integer> initList) {
List<Integer> list = new ArrayList(initList);
List<Integer> lists = new ArrayList<>();
for (Integer element : list) {
if (!lists.contains(element)) {
lists.add(element);
}
}
System.out.println("for循环添加去重:" + lists);
}
运行结果:
for循环添加去重:[1, 2, 3, 4, 5, 6]
2. for双循环去重
利用双循环,判断是否有相等的,再进行移除,这样就保证了元素不重复。
public static void main(String[] args) {
List<Integer> initList = Arrays.asList(1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6);
test2(initList);
}
/**
* for双循环去重
*
* @param initList
*/
public static void test2(List<Integer> initList) {
List<Integer> list = new ArrayList(initList);
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("for双循环去重:" + list);
}
运行结果:
for双循环去重:[1, 2, 3, 4, 5, 6]
3. for循环重复坐标去重
复制一个 lists,再循环 lists,判断 list 中的元素的首尾出现的坐标位置是否一致,如果一致,则说明没有重复的,否则重复,再删除重复的位置的元素,这样就保证了元素不重复。
public static void main(String[] args) {
List<Integer> initList = Arrays.asList(1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6);
test3(initList);
}
/**
* for循环重复坐标去重
*
* @param initList
*/
public static void test3(List<Integer> initList) {
List<Integer> list = new ArrayList(initList);
List<Integer> lists = new ArrayList(initList);
for (Integer element : lists) {
if (list.indexOf(element) != list.lastIndexOf(element)) {
list.remove(list.lastIndexOf(element));
}
}
System.out.println("for循环重复坐标去重:" + list);
}
运行结果:
for循环重复坐标去重:[1, 2, 3, 4, 5, 6]
4. Set去重
Set 是不包含重复元素的,把 List 先装进 HashSet,然后再装回来,这样就保证了元素的不重复。
public static void main(String[] args) {
List<Integer> initList = Arrays.asList(1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6);
test4(initList);
}
/**
* Set去重
*
* @param initList
*/
public static void test4(List<Integer> initList) {
List<Integer> list = new ArrayList(initList);
List<Integer> lists = new ArrayList(new HashSet(list));
System.out.println("Set去重:" + lists);
}
运行结果:
Set去重:[1, 2, 3, 4, 5, 6]
5. LinkedHashSet去重
如果要保证顺序性,可以把 HashSet 换成 LinkedHashSet
public static void main(String[] args) {
List<Integer> initList = Arrays.asList(1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6);
test5(initList);
}
/**
* LinkedHashSet去重
*
* @param initList
*/
public static void test5(List<Integer> initList) {
List<Integer> list = new ArrayList(initList);
List<Integer> lists = new ArrayList(new LinkedHashSet(list));
System.out.println("LinkedHashSet去重:" + lists);
}
运行结果:
LinkedHashSet去重:[1, 2, 3, 4, 5, 6]
6. Stream流去重
利用 Stream 的 distinct 方法去重,这样就保证了元素不重复。
public static void main(String[] args) {
List<Integer> initList = Arrays.asList(1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6);
test6(initList);
}
/**
* Stream流去重
*
* @param initList
*/
public static void test6(List<Integer> initList) {
List<Integer> list = new ArrayList(initList);
list = list.stream().distinct().collect(Collectors.toList());
System.out.println("Stream流去重:" + list);
}
运行结果:
Stream流去重:[1, 2, 3, 4, 5, 6]
7. 完整代码
import java.util.*;
import java.util.stream.Collectors;
/**
* List去重
*/
public class ListToRemove {
public static void main(String[] args) {
List<Integer> initList = Arrays.asList(1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6);
test1(initList);
test2(initList);
test3(initList);
test4(initList);
test5(initList);
test6(initList);
}
/**
* for循环添加去重
*
* @param initList
*/
public static void test1(List<Integer> initList) {
List<Integer> list = new ArrayList(initList);
List<Integer> lists = new ArrayList<>();
for (Integer element : list) {
if (!lists.contains(element)) {
lists.add(element);
}
}
System.out.println("for循环添加去重:" + lists);
}
/**
* for双循环去重
*
* @param initList
*/
public static void test2(List<Integer> initList) {
List<Integer> list = new ArrayList(initList);
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("for双循环去重:" + list);
}
/**
* for循环重复坐标去重
*
* @param initList
*/
public static void test3(List<Integer> initList) {
List<Integer> list = new ArrayList(initList);
List<Integer> lists = new ArrayList(initList);
for (Integer element : lists) {
if (list.indexOf(element) != list.lastIndexOf(element)) {
list.remove(list.lastIndexOf(element));
}
}
System.out.println("for循环重复坐标去重:" + list);
}
/**
* Set去重
*
* @param initList
*/
public static void test4(List<Integer> initList) {
List<Integer> list = new ArrayList(initList);
List<Integer> lists = new ArrayList(new HashSet(list));
System.out.println("Set去重:" + lists);
}
/**
* LinkedHashSet去重
*
* @param initList
*/
public static void test5(List<Integer> initList) {
List<Integer> list = new ArrayList(initList);
List<Integer> lists = new ArrayList(new LinkedHashSet(list));
System.out.println("LinkedHashSet去重:" + lists);
}
/**
* Stream流去重
*
* @param initList
*/
public static void test6(List<Integer> initList) {
List<Integer> list = new ArrayList(initList);
list = list.stream().distinct().collect(Collectors.toList());
System.out.println("Stream流去重:" + list);
}
}