List去除重复数据的五种方式
程序员文章站
2022-04-14 16:43:58
...
List去除重复数据的五种方式
参考网址:
https://mp.weixin.qq.com/s?__biz=MzI4Njc5NjM1NQ==&mid=2247499540&idx=2&sn=9d7cc14298bd4da9fb2e59dd17101a83&chksm=ebd5c038dca2492efb80e9a849b354bafa7e2290fa82d5af8a802c8611bdaaf13e876420725c&mpshare=1&scene=23&srcid=1110cWvHRB4QeStswgcAM23r&sharer_sharetime=1605534647788&sharer_shareid=9d1e76e919cc0b2f3ca23ed1f5ef67a8#rd
准备测试的实体类
package com.shaoming.pojo;
import lombok.Data;
/**
* @Auther: shaoming
* @Date: 2020/11/16 17:35
* @Description:
*/
@Data
public class User {
private Integer id;
private String name;
}
说明:
@Data是lombok(简称小辣椒)的注解
测试代码
package com.shaoming.集合;
import com.shaoming.pojo.User;
import org.junit.Test;
import java.util.*;
import java.util.stream.Collectors;
/**
* @Auther: shaoming
* @Date: 2020/11/16 21:55
* @Description: 测试arraList去重的五种方式
*/
public class ArrayListTestDistinct {
private static List<User> userList = new ArrayList<User>();
private static User user1 = new User();
private static User user2 = new User();
static {
user1.setId(1);
user1.setName("user1的name");
user2.setId(2);
user2.setName("user2的name");
userList.add(user1);
userList.add(user1);
userList.add(user1);
userList.add(user2);
userList.add(user2);
userList.add(user2);
}
//1.使用LinkedHashSet删除arraylist中的重复数据
@Test
public void testquchongByLinkedeHashSet() {
List<User> userList = new ArrayList<User>();
LinkedHashSet<User> userLinkedHashSet = new LinkedHashSet<>(userList);
System.out.println(userLinkedHashSet);
}
//2.使用java8新特性stream进行List去重
@Test
public void testquchongByJava8ofStream1() {
ArrayList<Integer> numbersList = new ArrayList<>(Arrays.asList(1, 1, 2, 3, 3, 3, 4, 5, 6, 6, 6, 7, 8));
System.out.println(numbersList);
List<Integer> listWithoutDuplicates = numbersList.stream().distinct().collect(Collectors.toList());
System.out.println(listWithoutDuplicates);
}
@Test
public void testquchongByJava8ofStream2() {
//获得去重后的list集合
userList.forEach(user -> System.out.println(user));
System.out.println("====使用java8的stream api对arrayList数据进行去重===============");
List<User> userListdistinct = ArrayList去重.userList.stream().distinct().collect(Collectors.toList());
userListdistinct.forEach(user -> System.out.println(user));
}
//3.利用HashSet不能添加重复数据的特性 由于HashSet不能保证添加顺序,所以只能作为判断条件保证顺序:
@Test
public void testquzhongByHashSet(){
HashSet<User> userHashSet = new HashSet<>(userList.size());
List<User> userArrayList = new ArrayList<>(userList.size());
for (User user : userList) {
if(userHashSet.add(user)){
userArrayList.add(user);
}
}
userList.clear();
userList.addAll(userArrayList);
userList.forEach(System.out::println);
}
//4.利用List的contains方法循环遍历,重新排序,只添加一次数据,避免重复:
@Test
public void testByListOfMethodToContains(){
List<User> userArrayList = new ArrayList<User>(userList.size());
for (User user : userList) {
if(!userArrayList.contains(user)){
userArrayList.add(user);
}
}
userList.clear();
userList.addAll(userArrayList);
userList.forEach(System.out::println);
}
//5.双重for循环去重
@Test
public void testTwoFortoDistinct(){
for(int i = 0 ; i< userList.size();i++){
for(int j = 0 ; j< userList.size();j++){
if(i!=j&&userList.get(i)==userList.get(j)) {
userList.remove(userList.get(j));
}
}
}
userList.forEach(System.out::println);
}
}
说明:
1.这是我自己的测试方法,如有不当请指正
2.具体细节可以参考我参考的文章连接地址
}
}
userList.forEach(System.out::println);
}
}
> 说明:
>
> 1.这是我自己的测试方法,如有不当请指正
>
> 2.具体细节可以参考我参考的文章连接地址
上一篇: List去除重复数据的五种方式
下一篇: SQL去除重复数据