java8之Stream常用方法

⒈获得集合中的某个字段的list(Node实体,nodeTypeCode字段):
    List nodeList = allNodeList.stream()
                            .filter(x -> "1".equals(x.getNodeTypeCode()))//过滤使用
                            .map(Node::getNodeCode).collect(Collectors.toList());
    ⒉获得集合中某个字段组成数组:
    String[] areaCodes = resultList.stream().map(x -> x.getAreaCode()).toArray(String[]::new);

    ⒊根据某个字段分组
    Map> collect = 
                            warnAlarmRecords.stream().collect(groupingBy(WarnAlarmRecord::getAreaCode));
    4.模糊匹配过滤
    qrList = qrList.stream().filter(x ->x.getSampleName().contains(sampleName)).collect(Collectors.toList());
    5.根据字段分组、求和计算
    Map collect = hisValueList.stream()
                            .collect(Collectors.groupingBy(x -> formatFYTm.format(x.getIdxTime()), 
                            Collectors.summingDouble(NodeIndexHistValue::getIdxValue)));
    6.累加某个字段
    Double idxValue = nodeIndexValueList.stream().collect(Collectors.summingDouble(NodeIndexValue::getIdxValue));
    7.过滤数组中包含某元素的数据
    String[] sideLineCodes = sideLineList.stream().map(SideLine::getNodeCode).toArray(String[]::new);
    List scheduleBalanceRateList = lists.stream(). 

filter(x ->  Arrays.asList(sideLineCodes).contains(x.getTgNodeCode())).collect(Collectors.toList());
    8.根据实体中某个字段去重
    List measIndexs = measindexMntnRepository.findByNodeCode(Arrays.asList(nodeCodeArray));
            measIndexs = measIndexs.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(()->
                            new TreeSet<>(Comparator.comparing(MeasindexMntnPojo::getIdxCode))),ArrayList::new));
    9.List/List 去重
    list = list.stream().distinct().collect(Collectors.toList());
    
    10.拼接输出list中的元素
    String str = list.stream().collect(Collectors.joining(","));

11.排序(正序的话去掉reversed)

weathers = weathers.stream().sorted(Comparator.comparing(Weather::getDate))

.reversed().collect(Collectors.toList())

排序后取前2条

weathers = weathers.stream().sorted(Comparator.comparing(Weather::getDate)).
limit(2).collect(Collectors.toList());

你可能感兴趣的:(java)