list集合利用Stream流处理数据

list实体转List<Map<String, Object>>集合

List<Entity> a = new ArrayList<>();
String str = JSON.toJSONString(a);
List<Map<String, Object>> map = JSONArray.parseObject(str, List.class);

list排序

List<Map<String, String>> list = new ArrayList<>();
Map<String, String> a = new HashMap<>();
a.put("a","11111");
Map<String, String> b = new HashMap<>();
b.put("a","22222");
Map<String, String> c = new HashMap<>();
c.put("a","11111");
list.add(a);
list.add(b);
list.add(c);
list = list.stream().sorted(Comparator.comparing(item -> item.toString())).collect(Collectors.toList());
/*
// 另一种排序方法
Collections.sort(list, new Comparator<Map<String, String>>() {
    public int compare(Map<String, String> o1, Map<String, String> o2) {
        return new BigDecimal(o1.get("a")).compareTo(new BigDecimal(o2.get("a")));
    }
});
// 另一种排序方法
Collections.sort(list, (s1, s2) -> s1.get("a").compareTo(s2.get("a")));
*/

合并两个list【a和b不能为空】

List<String> a = new ArrayList<>();
a.add("1");
a.add("3");
List<String> b = new ArrayList<>();
b.add("3");
b.add("5");
a = Stream.of(a, b) .flatMap(Collection::stream).distinct().collect(Collectors.toList());

字符串转集合

String a = "HNWN,HNWNBX";
List<String> b = Arrays.asList(a.split(","));
// b = [HNWN, HNWNBX]

根据a和b对集合进行去重

List<Map<String, String>> mapList = new ArrayList<>();
Map<String, String> a = new HashMap<>();
a.put("a", "aaa");
a.put("b", "bbb");
a.put("c", "c");
Map<String, String> b = new HashMap<>();
b.put("a", "aaa");
b.put("b", "bbb");
mapList.add(a);
mapList.add(b);
mapList = mapList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() ->
                new TreeSet<>(Comparator.comparing(o -> o.get("a")+o.get("b")))), ArrayList::new));
// mapList = [{a=aaa, b=bbb, c=c}]

带时间的List<实体类>转List<Map<String, Object>>集合

List<AbtComHxRiskconConsult> consultList = new ArrayList<>();
// 实体类转map
String consult = JSON.toJSONStringWithDateFormat(consultList, "yyyy-MM-dd", SerializerFeature.WriteDateUseDateFormat);
List<Map<String, Object>> consults = JSONObject.parseObject(consult, List.class);

List<Map<String, Object>>集合转list实体

List<Map<String, Object>> personList = new ArrayList<>();
String str = JSON.toJSONString(personList);
List<Person> accountList = JSON.parseArray(str, Person.class);

对逗号分隔的字符串排序

String kk = "32,5,100";
List<Integer> split = Arrays.stream(kk.split(",")).map(s -> Integer.valueOf(s.trim())).sorted().collect(Collectors.toList());
String hh = Joiner.on(",").join(split);

提取里面某个值为key,另一个值为value

List<EnumEntity> enumList = enumRepository.getEnumByDefcode("PGBZ");
Map<String, String> enumDefcodeMap = enumList.stream().collect(Collectors.toMap(EnumEntity::getEnumcode, EnumEntity::getEnumname));

按患者分组,提取访视点为集合

Map<String, List<String>> reportMap = reportList.stream().collect(Collectors.groupingBy(Report::getPatientId,Collectors.mapping(Report::getViewpoint,Collectors.toList())));

分组

Map<String, Map<String, String>> enum1 = processList.stream().collect(Collectors.toMap(item -> item.get("title"), Function.identity()));

求某个值之和

Integer count = activityGoodsSkus.stream().mapToInt(TeamActivityGoodsVo::getCount).sum();
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值