java使用面向过程方式实现List数据合并
本文大纲
一、基础构建类
二、连接构建类
三、实体类
四、测试
一、基础构建类
属性设置函数
package com.jareny.java.quick.link;
@FunctionalInterface
public interface BaseConsumer<T,V> {
/**
* accept 2 values
*
* @param t
* @param value
*/
void accept(T t, V value);
}
属性设置构建类
package com.jareny.java.quick.link;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
public class BaseBuilder<T, S> {
private T t;
private S s;
private List<Consumer<T>> list = new ArrayList<>();
public BaseBuilder(T t, S s) {
this.t = t;
this.s = s;
}
public static <T, S> BaseBuilder<T, S> of(T t, S s) {
return new BaseBuilder<>(t, s);
}
public <V> BaseBuilder<T, S> width(BaseConsumer<T, V> consumer, Function<S, V> function) {
list.add(e -> consumer.accept(e, function.apply(s)));
return this;
}
public <V> BaseBuilder<T, S> width(BaseConsumer<T, V> consumer, Function<S, V> function, V orElse) {
V value = function.apply(s);
if (null == value) {
list.add(e -> consumer.accept(e, value));
} else {
list.add(e -> consumer.accept(e, orElse));
}
return this;
}
public <V> BaseBuilder<T, S> width(boolean accept, BaseConsumer<T, V> consumer, Function<S, V> function) {
if (accept) {
list.add(e -> consumer.accept(e, function.apply(s)));
}
return this;
}
public <V> BaseBuilder<T, S> width(boolean accept, BaseConsumer<T, V> consumer, Function<S, V> function, V orElse) {
if (accept) {
list.add(e -> consumer.accept(e, function.apply(s)));
} else {
list.add(e -> consumer.accept(e, orElse));
}
return this;
}
/**
* 触发构建
*
* @return
*/
public T build() {
Optional.of(list)
.orElse(new ArrayList<>())
.stream()
.filter(Objects::nonNull)
.forEach(e -> e.accept(t));
return t;
}
}
二、连接构建类
package com.jareny.java.quick.link;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.stream.Collectors;
public class LinkBuilder<T, S> {
private List<T> targerList;
private List<S> sourceList;
public LinkBuilder(List<T> targerList, List<S> sourceList) {
this.targerList = targerList;
this.sourceList = sourceList;
}
/**
* 静态工厂方法,获取类型转换
*
* @param targerList 目标数据
* @param sourceList 源数据
* @param <T> 目标类型
* @param <S> 源类型
* @return 构造函数
*/
public static <T, S> LinkBuilder<T, S> of(List<T> targerList, List<S> sourceList) {
return new LinkBuilder<>(targerList, sourceList);
}
/**
* 开始链接
*
* @param tMapper 目标连接键
* @param sMapper 源数据连接键
* @param biConsumer 构建的过程
* @return
*/
public <V> List<T> join(Function<T, V> tMapper, Function<S, V> sMapper, BiConsumer<T, S> biConsumer) {
if (CollUtil.isEmpty(targerList)) {
return CollUtil.newArrayList();
}
//
Map<V, T> targerMap = targerList.stream().collect(Collectors.toMap(t -> tMapper.apply(t), Function.identity()));
Map<V, S> sourceMap = sourceList.stream().collect(Collectors.toMap(s -> sMapper.apply(s), Function.identity()));
if (MapUtil.isEmpty(sourceMap) || MapUtil.isEmpty(targerMap)) {
return CollUtil.newArrayList();
}
// 循环数据
for (T t : targerList) {
S s = sourceMap.get(tMapper.apply(t));
if (null == s) {
continue;
}
// 替换数据
biConsumer.accept(t, s);
}
return targerList;
}
}
三、实体类
目标类
package com.jareny.java.quick.link;
import lombok.Data;
import java.util.List;
@Data
public class PeopleVo {
private Long id;
private String name;
private Integer age;
private Integer higth;
private Long type;
private String typeName;
private String typeDesc;
private List<String> detailList;
}
合并数据类
package com.jareny.java.quick.link;
import lombok.Data;
import java.util.List;
@Data
public class PeopleType {
private Long id;
private String name;
private String desc;
private List<String> list;
}
四、测试
package com.jareny.java.quick.link;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import java.util.List;
import java.util.function.BiConsumer;
public class LinkBuilderTest {
public static void main(String[] args) {
List<PeopleVo> targerList = CollUtil.newArrayList();
PeopleVo peopleVo1 = new PeopleVo();
peopleVo1.setId(1001L);
peopleVo1.setName("peopleVo1");
peopleVo1.setAge(30);
peopleVo1.setHigth(171);
peopleVo1.setType(11L);
PeopleVo peopleVo2 = new PeopleVo();
peopleVo2.setId(1002L);
peopleVo2.setName("peopleVo2");
peopleVo2.setAge(33);
peopleVo2.setHigth(172);
peopleVo2.setType(22L);
PeopleVo peopleVo3 = new PeopleVo();
peopleVo3.setId(1003L);
peopleVo3.setName("peopleVo3");
peopleVo3.setAge(33);
peopleVo3.setHigth(173);
peopleVo3.setType(33L);
targerList.add(peopleVo1);
targerList.add(peopleVo2);
targerList.add(peopleVo3);
List<PeopleType> sourceList = CollUtil.newArrayList();
PeopleType peopleType11 = new PeopleType();
peopleType11.setId(11L);
peopleType11.setName("我是11类型");
peopleType11.setDesc("11描述");
peopleType11.setList(CollUtil.newArrayList("详情1,详情2"));
PeopleType peopleType22 = new PeopleType();
peopleType22.setId(22L);
peopleType22.setName("我是22类型");
peopleType22.setDesc("22描述");
sourceList.add(peopleType11);
sourceList.add(peopleType22);
// 设置链接构建过程
BiConsumer<PeopleVo, PeopleType> biConsumer = (t, s) -> BaseBuilder.of(t, s)
.width(PeopleVo::setTypeName, PeopleType::getName)
.width(ObjectUtil.isNull(s.getDesc()), PeopleVo::setTypeDesc, PeopleType::getDesc)
.width(CollUtil.isNotEmpty(s.getList()), PeopleVo::setDetailList, PeopleType::getList)
.build();
// BiConsumer<PeopleVo, PeopleType> biConsumer1 = (t, s) ->t.setTypeDesc(s.getDesc());
// 开始构建
List<PeopleVo> build = LinkBuilder.of(targerList, sourceList).join(PeopleVo::getType, PeopleType::getId, biConsumer);
System.out.println(JSONUtil.toJsonPrettyStr(build));
}
}
运行结果
连个列表的数据合并在一起了
[
{
"typeName": "我是11类型",
"type": 11,
"higth": 171,
"name": "peopleVo1",
"detailList": [
"详情1,详情2"
],
"id": 1001,
"age": 30
},
{
"typeName": "我是22类型",
"type": 22,
"higth": 172,
"name": "peopleVo2",
"id": 1002,
"age": 33
},
{
"type": 33,
"higth": 173,
"name": "peopleVo3",
"id": 1003,
"age": 33
}
]