Java8使用stream操作两个list根据某字段匹配再对其中一个list进行赋值
【摘要】
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import java.lang.reflect.Fie...
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import java.lang.reflect.Field;
import java.util.*;
import java.util.stream.Collectors;
@Slf4j
public class ListUtils {
/**
* lambda表达式对两个List进行循环,根据符合条件,进行相关的赋值操作并返回这个对象集合
* @param sourceList 待设置源列表
* @param srcEqualProp 源对象条件判断属性名
* @param srcSetProp 源对象待设置属性名
* @param targetList 资源提供者列表
* @param tarEqualProp 对象条件判断参数名
* @param tarGetProp 待获取对象属性名
* @param <T>
* @param <U>
* @return
*/
public static <T,U>List<T> setListByEqualObjProperty(List<T> sourceList, String srcEqualProp, String srcSetProp,
List<U> targetList, String tarEqualProp, String tarGetProp){
List<T> resultList = Lists.newArrayList();
resultList = sourceList.stream()
.map(sur-> targetList.stream()
.filter(tar -> Objects.equals(getValueByPropName(sur, srcEqualProp), getValueByPropName(tar, tarEqualProp)))
.findFirst()
.map(tar -> {
setValueByPropName(sur, srcSetProp, getValueByPropName(tar, tarGetProp));
return sur;
} ).orElse(null))
.collect(Collectors.toList());
return resultList;
}
/**
* 通过遍历两个List中按id属性相等的归结到resultList中
* @param oneList 源list 1
* @param twoList 源list 2
* @param equalKeyName 相等的map键值
*/
public static List<Map<Object, Object>> compareListHitData(List<Map<Object, Object>> oneList, List<Map<Object, Object>> twoList, Object equalKeyName) {
List<Map<Object, Object>> resultList = oneList.stream().map(map -> twoList.stream()
.filter(m -> Objects.equals(m.get(equalKeyName),map.get(equalKeyName)))
.findFirst().map(m -> {
map.putAll(m);
return map;
}).orElse(null))
.filter(Objects::nonNull).collect(Collectors.toList());
return resultList;
}
// 通过属性获取传入对象的指定属性的值
public static <T> T getValueByPropName(Object object, String propName) {
T value = null;
try {
// 通过属性获取对象的属性
Field field = object.getClass().getDeclaredField(propName);
// 对象的属性的访问权限设置为可访问
field.setAccessible(true);
// 获取属性的对应的值
value = (T)field.get(object);
} catch (Exception e) {
return null;
}
return value;
}
// 通过属性设置传入对象的指定属性的值
public static <U> void setValueByPropName(Object object, String propName, U updateValue) {
try {
// 通过属性获取对象的属性
Field field = object.getClass().getDeclaredField(propName);
// 对象的属性的访问权限设置为可访问
field.setAccessible(true);
// 设置属性的对应的值
field.set(object, updateValue);
} catch (Exception e) {
log.error("setValueByPropName.error {}", propName, e);
}
}
@Data
public class Girl {
private String id;
private String name;
}
@Data
public class SchoolBoy {
private String girlId;
private String id;
private String name;
private Integer age;
private String girlName;
}
public static void main(String[] args) {
List<SchoolBoy> schoolBoys = new ArrayList<>(3);
SchoolBoy boy1 = new SchoolBoy();
boy1.setGirlId("1");
boy1.setId("10");
boy1.setName("小明");
SchoolBoy boy2 = new SchoolBoy();
boy2.setGirlId("2");
boy2.setId("11");
boy2.setName("小豪");
SchoolBoy boy3 = new SchoolBoy();
boy3.setGirlId("3");
boy3.setId("12");
boy3.setName("小白");
schoolBoys.add(boy1);
schoolBoys.add(boy2);
schoolBoys.add(boy3);
List<Girl> girls = new ArrayList<>(3);
Girl girl1 = new Girl();
girl1.setId("1");
girl1.setName("小英");
Girl girl2 = new Girl();
girl2.setId("2");
girl2.setName("小美");
Girl girl3 = new Girl();
girl3.setId("3");
girl3.setName("小花");
girls.add(girl1);
girls.add(girl2);
girls.add(girl3);
List<SchoolBoy> list = ListUtils.setListByEqualObjProperty(schoolBoys,"girlId", "girlName",
girls, "id", "name");
System.out.println(list.toString());
List<Map<Object, Object>> oneList = new ArrayList<>();
Map<Object, Object> oneMap = new HashMap<>();
oneMap.put("id", 111);
oneMap.put("userName", "林飞");
Map<Object, Object> twoMap = new HashMap<>();
twoMap.put("id", 222);
twoMap.put("userName", "Hejinrong");
oneList.add(oneMap);
oneList.add(twoMap);
List<Map<Object, Object>> twoList = new ArrayList<>();
Map<Object, Object> threeMap = new HashMap<>();
threeMap.put("id", 111);
threeMap.put("userName", "林飞");
Map<Object, Object> fourMap = new HashMap<>();
fourMap.put("id", 333);
fourMap.put("userName", "Hejinrong");
twoList.add(threeMap);
twoList.add(fourMap);
List<Map<Object, Object>> resultList = compareListHitData(oneList, twoList, "id");
System.out.println(resultList);
System.out.println("Max memory =" + Runtime.getRuntime().maxMemory()/(double)1024/1024 +"M");
System.out.println("Total memory= " + Runtime.getRuntime().totalMemory()/(double)1024/1024 +"M");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
文章来源: javaedge.blog.csdn.net,作者:JavaEdge.,版权归原作者所有,如需转载,请联系作者。
原文链接:javaedge.blog.csdn.net/article/details/121526659
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)