Java8使用stream操作两个list根据某字段匹配再对其中一个list进行赋值

x33g5p2x  于2021-11-25 转载在 Java  
字(4.5k)|赞(0)|评价(0)|浏览(1005)
  1. import com.google.common.collect.Lists;
  2. import lombok.extern.slf4j.Slf4j;
  3. import java.lang.reflect.Field;
  4. import java.util.*;
  5. import java.util.stream.Collectors;
  6. @Slf4j
  7. public class ListUtils {
  8. /** * lambda表达式对两个List进行循环,根据符合条件,进行相关的赋值操作并返回这个对象集合 * @param sourceList 待设置源列表 * @param srcEqualProp 源对象条件判断属性名 * @param srcSetProp 源对象待设置属性名 * @param targetList 资源提供者列表 * @param tarEqualProp 对象条件判断参数名 * @param tarGetProp 待获取对象属性名 * @param <T> * @param <U> * @return */
  9. public static <T,U>List<T> setListByEqualObjProperty(List<T> sourceList, String srcEqualProp, String srcSetProp,
  10. List<U> targetList, String tarEqualProp, String tarGetProp){
  11. List<T> resultList = Lists.newArrayList();
  12. resultList = sourceList.stream()
  13. .map(sur-> targetList.stream()
  14. .filter(tar -> Objects.equals(getValueByPropName(sur, srcEqualProp), getValueByPropName(tar, tarEqualProp)))
  15. .findFirst()
  16. .map(tar -> {
  17. setValueByPropName(sur, srcSetProp, getValueByPropName(tar, tarGetProp));
  18. return sur;
  19. } ).orElse(null))
  20. .collect(Collectors.toList());
  21. return resultList;
  22. }
  23. /** * 通过遍历两个List中按id属性相等的归结到resultList中 * @param oneList 源list 1 * @param twoList 源list 2 * @param equalKeyName 相等的map键值 */
  24. public static List<Map<Object, Object>> compareListHitData(List<Map<Object, Object>> oneList, List<Map<Object, Object>> twoList, Object equalKeyName) {
  25. List<Map<Object, Object>> resultList = oneList.stream().map(map -> twoList.stream()
  26. .filter(m -> Objects.equals(m.get(equalKeyName),map.get(equalKeyName)))
  27. .findFirst().map(m -> {
  28. map.putAll(m);
  29. return map;
  30. }).orElse(null))
  31. .filter(Objects::nonNull).collect(Collectors.toList());
  32. return resultList;
  33. }
  34. // 通过属性获取传入对象的指定属性的值
  35. public static <T> T getValueByPropName(Object object, String propName) {
  36. T value = null;
  37. try {
  38. // 通过属性获取对象的属性
  39. Field field = object.getClass().getDeclaredField(propName);
  40. // 对象的属性的访问权限设置为可访问
  41. field.setAccessible(true);
  42. // 获取属性的对应的值
  43. value = (T)field.get(object);
  44. } catch (Exception e) {
  45. return null;
  46. }
  47. return value;
  48. }
  49. // 通过属性设置传入对象的指定属性的值
  50. public static <U> void setValueByPropName(Object object, String propName, U updateValue) {
  51. try {
  52. // 通过属性获取对象的属性
  53. Field field = object.getClass().getDeclaredField(propName);
  54. // 对象的属性的访问权限设置为可访问
  55. field.setAccessible(true);
  56. // 设置属性的对应的值
  57. field.set(object, updateValue);
  58. } catch (Exception e) {
  59. log.error("setValueByPropName.error {}", propName, e);
  60. }
  61. }
  62. @Data
  63. public class Girl {
  64. private String id;
  65. private String name;
  66. }
  67. @Data
  68. public class SchoolBoy {
  69. private String girlId;
  70. private String id;
  71. private String name;
  72. private Integer age;
  73. private String girlName;
  74. }
  75. public static void main(String[] args) {
  76. List<SchoolBoy> schoolBoys = new ArrayList<>(3);
  77. SchoolBoy boy1 = new SchoolBoy();
  78. boy1.setGirlId("1");
  79. boy1.setId("10");
  80. boy1.setName("小明");
  81. SchoolBoy boy2 = new SchoolBoy();
  82. boy2.setGirlId("2");
  83. boy2.setId("11");
  84. boy2.setName("小豪");
  85. SchoolBoy boy3 = new SchoolBoy();
  86. boy3.setGirlId("3");
  87. boy3.setId("12");
  88. boy3.setName("小白");
  89. schoolBoys.add(boy1);
  90. schoolBoys.add(boy2);
  91. schoolBoys.add(boy3);
  92. List<Girl> girls = new ArrayList<>(3);
  93. Girl girl1 = new Girl();
  94. girl1.setId("1");
  95. girl1.setName("小英");
  96. Girl girl2 = new Girl();
  97. girl2.setId("2");
  98. girl2.setName("小美");
  99. Girl girl3 = new Girl();
  100. girl3.setId("3");
  101. girl3.setName("小花");
  102. girls.add(girl1);
  103. girls.add(girl2);
  104. girls.add(girl3);
  105. List<SchoolBoy> list = ListUtils.setListByEqualObjProperty(schoolBoys,"girlId", "girlName",
  106. girls, "id", "name");
  107. System.out.println(list.toString());
  108. List<Map<Object, Object>> oneList = new ArrayList<>();
  109. Map<Object, Object> oneMap = new HashMap<>();
  110. oneMap.put("id", 111);
  111. oneMap.put("userName", "林飞");
  112. Map<Object, Object> twoMap = new HashMap<>();
  113. twoMap.put("id", 222);
  114. twoMap.put("userName", "Hejinrong");
  115. oneList.add(oneMap);
  116. oneList.add(twoMap);
  117. List<Map<Object, Object>> twoList = new ArrayList<>();
  118. Map<Object, Object> threeMap = new HashMap<>();
  119. threeMap.put("id", 111);
  120. threeMap.put("userName", "林飞");
  121. Map<Object, Object> fourMap = new HashMap<>();
  122. fourMap.put("id", 333);
  123. fourMap.put("userName", "Hejinrong");
  124. twoList.add(threeMap);
  125. twoList.add(fourMap);
  126. List<Map<Object, Object>> resultList = compareListHitData(oneList, twoList, "id");
  127. System.out.println(resultList);
  128. System.out.println("Max memory =" + Runtime.getRuntime().maxMemory()/(double)1024/1024 +"M");
  129. System.out.println("Total memory= " + Runtime.getRuntime().totalMemory()/(double)1024/1024 +"M");
  130. }
  131. }

相关文章

最新文章

更多