zoukankan      html  css  js  c++  java
  • 对List<Map>里的map的某个属性重复的值进行处理的方法

    package test;

    import java.util.*;
    import java.util.stream.Collectors;

    public class Test5 {

    public static void main(String[] args) {

    List<LinkedHashMap<String, Object>> list = new ArrayList<>();
    list.add(new LinkedHashMap<String, Object>() {{
    put("id", "a");
    put("age", "13");
    put("sex", "男");
    }});
    list.add(new LinkedHashMap<String, Object>() {{
    put("id", "a");
    put("age", "14");
    put("sex", "男");
    }});
    list.add(new LinkedHashMap<String, Object>() {{
    put("id", "b");
    put("age", "14");
    put("sex", "男");
    }});
    list.add(new LinkedHashMap<String, Object>() {{
    put("id", "c");
    put("age", "14");
    put("sex", "男");
    }});
    list.add(new LinkedHashMap<String, Object>() {{
    put("id", "");
    put("age", "14");
    put("sex", "男");
    }});
    list.add(new LinkedHashMap<String, Object>() {{
    put("id", "c");
    put("age", "14");
    put("sex", "男");
    }});


    handleRepeatPrimaryKey(list, "id");
    List<LinkedHashMap<String, Object>> hasList = new ArrayList<>();
    hasList = list.stream().filter(x -> !x.get("id").toString().equals("")).collect(Collectors.toList());
    List<LinkedHashMap<String, Object>> notHasList = new ArrayList<>();
    notHasList = list.stream().filter(x -> x.get("id").toString().equals("")).collect(Collectors.toList());

    System.out.println(hasList);
    System.out.println(notHasList);


    }


    public static List<LinkedHashMap<String, Object>> handleRepeatPrimaryKey(List<LinkedHashMap<String, Object>> list, String id) {
    List<LinkedHashMap<String, Object>> itemList = new ArrayList<>();
    for (LinkedHashMap<String, Object> lhm : list) {
    if (itemList.size() == 0) {
    itemList.add(lhm);
    } else {
    ListIterator<LinkedHashMap<String, Object>> listIterator = itemList.listIterator();
    while (listIterator.hasNext()) {
    if (listIterator.next().get(id).toString().equals(lhm.get(id).toString())) {
    lhm.put(id, "");
    }
    }
    // 注意放在循环外面
    listIterator.add(lhm);
    }
    }
    return list;
    }
    }

    对重复主键进行处理,取第一个,剩余的主键都置空,嗯,能满足我的需求。不知道还有没有更好的方法?

  • 相关阅读:
    .NET中非对称加密RSA算法的密钥保存
    WGS84经纬度坐标到北京54高斯投影坐标的转换[转]
    [APPS] HTC Footprints & HTC Locations for MikG 2.x Read more:
    firefox+ssh无法看youtube视频的解决方案
    【转】sdemon命令实践
    How to share a custom ArcMap command (DLL)
    【转】sdemon命令实践
    红旗桌面版本最新运用法子和结果解答100例8
    红旗Linux桌面4.1文本安装过程图解(二)
    Ubuntu把在效能器范畴起更重要的脚色
  • 原文地址:https://www.cnblogs.com/rongyao/p/11261601.html
Copyright © 2011-2022 走看看