在现有的业务场景中,经常涉及到很多应用需要针对集合内容进行去重筛选,并且涉及到对象中的组合去重条件,Java8提供的流处理就非常方便,mark 一下网上的例子,供大家学习使用,且做个记录!
栗子如下:
package com.demo.jdk8.collectors;
import com.google.common.collect.Lists;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;
/**
* @author Shanks
* @date 2018-10-15
*/
public class CollectingAndThenTest {
@Test
void testCollectingAndThen() {
UserOrder order0 = new UserOrder("年会", "张三", "box0001");
UserOrder order1 = new UserOrder("婚庆", "张三", "box0002");
UserOrder order2 = new UserOrder("旅游", "李四", "box0003");
UserOrder order3 = new UserOrder("发布会", "张三", "box0002");
UserOrder order4 = new UserOrder("旅游", "李四", "box0004");
List<UserOrder> list = Lists.newArrayList(order0, order1, order2, order3, order4);
System.out.println("=====>只根据单一属性photographerName进行去重");
// <R, A> R collect(Collector<? super T, A, R> collector);
// collectingAndThen(Collector<T, A, R> downstream, Function<R, RR> finisher)
// toCollection(Supplier<C> collectionFactory)
ArrayList<UserOrder> distinctByName = list.stream()
.collect(
Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(UserOrder::getPhotographerName))),
userOrders -> new ArrayList<>(userOrders)
)
);
distinctByName.forEach(System.out::println);
System.out.println("=====>同时根据photographerName和deviceCode两个属性进行去重");
ArrayList<UserOrder> distinctByNameAndCode = list.stream()
.collect(
Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(userOrder -> userOrder.getPhotographerName() + userOrder.getDeviceCode()))),
ArrayList::new
)
);
distinctByNameAndCode.forEach(System.out::println);
}
}
打印结果:
=====>只根据单一属性photographerName进行去重
UserOrder{shootingTheme='年会', photographerName='张三', deviceCode='box0001'}
UserOrder{shootingTheme='旅游', photographerName='李四', deviceCode='box0003'}
=====>同时根据photographerName和deviceCode两个属性进行去重
UserOrder{shootingTheme='年会', photographerName='张三', deviceCode='box0001'}
UserOrder{shootingTheme='婚庆', photographerName='张三', deviceCode='box0002'}
UserOrder{shootingTheme='旅游', photographerName='李四', deviceCode='box0003'}
UserOrder{shootingTheme='旅游', photographerName='李四', deviceCode='box0004'}
实体类:
package com.demo.jdk8.collectors;
/**
* @author Shanks
* @date 2018-10-16
*/
public class UserOrder {
/**
* 拍摄主题
*/
private String shootingTheme;
/**
* 摄影师
*/
private String photographerName;
/**
* 设备编号
*/
private String deviceCode;
public UserOrder() {
}
public UserOrder(String shootingTheme, String photographerName, String deviceCode) {
this.shootingTheme = shootingTheme;
this.photographerName = photographerName;
this.deviceCode = deviceCode;
}
@Override
public String toString() {
return "UserOrder{" +
"shootingTheme='" + shootingTheme + '\'' +
", photographerName='" + photographerName + '\'' +
", deviceCode='" + deviceCode + '\'' +
'}';
}
public String getShootingTheme() {
return shootingTheme;
}
public void setShootingTheme(String shootingTheme) {
this.shootingTheme = shootingTheme;
}
public String getPhotographerName() {
return photographerName;
}
public void setPhotographerName(String photographerName) {
this.photographerName = photographerName;
}
public String getDeviceCode() {
return deviceCode;
}
public void setDeviceCode(String deviceCode) {
this.deviceCode = deviceCode;
}
}