List<T> list對象去重方法總結
【去重方法總結】:(java8)
package com.caox.utils;
import com.caox.model.UserInfo;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import static java.util.Comparator.comparingLong;
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toCollection;
/**
* @author : nazi
* @version : 1.0
* @date : 2019/7/5 17:54
* 去重List<對象>
*/
public class ListObjectNoRepeatUtils {
/**
* 去重字符串重複
* @param stringList 字符串List
* @return 返回去重字符串
*/
public static List<String> removeStringListDupli(List<String> stringList) {
Set<String> set = new LinkedHashSet<>();
set.addAll(stringList);
stringList.clear();
stringList.addAll(set);
return stringList;
}
/**
* 根據對象屬性去重 屬性:userId
* @param persons
* @return
*/
public static List<UserInfo> removeDupliById(List<UserInfo> persons) {
Set<UserInfo> personSet = new TreeSet<>((o1, o2) -> o1.getUserId().compareTo(o2.getUserId()));
personSet.addAll(persons);
return new ArrayList<>(personSet);
}
/**
* 根據對象多個屬性去重 屬性:userId + userName
* @param persons
* @return
*/
public static List<UserInfo> removeDupliByMorePro(List<UserInfo> persons){
List<UserInfo> personList = persons.stream().collect(Collectors
.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> {
// 根據useId和userName進行去重
return o.getUserId() + "," + o.getUsername() +"," + o.getAge();
}))), ArrayList::new));
return personList;
}
/**
* 根據對象單個屬性去重 屬性:userId
* @param persons
* @return
*/
public static List<UserInfo> removeDupliByUserId(List<UserInfo> persons){
List<UserInfo> unique = persons.stream().collect(
collectingAndThen(
toCollection(() -> new TreeSet<>(comparingLong(UserInfo::getUserId))), ArrayList::new)
);
return unique;
}
/**
* 根據對象單個屬性去重 屬性:userId
* @param persons
* @return
*/
public static List<UserInfo> removeDupliByUserIdNew(List<UserInfo> persons){
List<UserInfo> personList = new ArrayList<>();
persons.stream().filter(distinctByKey(p -> p.getUserId()))
.forEach(p -> personList.add(p));
return personList;
}
/**
* 根據key去重重複
* @param keyExtractor key執行器
* @param <T> 泛型
* @return
*/
public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
Map<Object, Boolean> map = new ConcurrentHashMap<>();
return t -> map.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
}
}
【對應實體類】:
package com.caox.model;
import lombok.Data;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author : nazi
* @version : 1.0
* @date : 2019/6/5 14:27
*/
@Data
public class UserInfo {
private Integer userId;
private String username;
private Date birthDate;
private Integer age;
private float fRate;
private char ch;
public Date getBirthDate() {
return birthDate;
}
public String getBirthDatestr() {
SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
return formater.format(getBirthDate());
}
public UserInfo(Integer userId, String username, Date birthDate, Integer age, float fRate, char ch) {
this.userId = userId;
this.username = username;
this.birthDate = birthDate;
this.age = age;
this.fRate = fRate;
this.ch = ch;
}
public UserInfo(Integer userId, String username) {
this.userId = userId;
this.username = username;
}
public UserInfo(Integer userId, String username, Integer age) {
this.userId = userId;
this.username = username;
this.age = age;
}
@Override
public String toString() {
return "UserInfo{" +
"userId=" + userId +
", username='" + username + '\'' +
", birthDate=" + birthDate +
", age=" + age +
", fRate=" + fRate +
", ch=" + ch +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
UserInfo userInfo = (UserInfo) o;
if (!userId.equals(userInfo.userId)) return false;
return username.equals(userInfo.username);
}
@Override
public int hashCode() {
int result = userId.hashCode();
result = 31 * result + username.hashCode();
return result;
}
}