浮光笔记:stream流

//map
List<String> strs = ids.stream().map(id -> String.valueOf(id)).collect(Collectors.toList());

//收集为数组
Map<Long, List<Long>> map = mappingPOS.stream().filter(e -> starQuestIds.contains(e.getQuestId()))
                .collect(Collectors.groupingBy(QuestMappingPO::getIndexId, 
                        Collectors.mapping(QuestMappingPO::getQuestId, Collectors.toList())));

//flatmap
flatMap(ids -> {
  return ids.stream().map(e -> new AbstractMap.SimpleEntry<>(e, "value"));
}).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a, b) -> a));

//分组后统计每组数量
Map<String, Integer> wordCount = frameTexts.parallelStream().filter(StringUtils::isNotBlank)
                .flatMap(text -> segmenter.process(text, JiebaSegmenter.SegMode.SEARCH)
                        .stream().map(seg -> seg.word)
                ).collect(Collectors.groupingBy(Function.identity(), Collectors.summingInt(e -> 1)));

超复杂stream

Map<PageFingerInfoPO, EditionWordVO> virtualMap = fingers.stream().collect(Collectors.groupingBy(PageFingerInfoPO::getPageId)).values().stream().flatMap(fids -> {
    Long pageId = fids.get(0).getPageId();
    //书页对应目录
    List<Long> dirIds = dirs.stream().filter(e -> e.getPageIds().contains(pageId)).map(DirectoryVO::getId).collect(Collectors.toList());
    if (dirIds.isEmpty()) {
        return Stream.empty();
    }

    List<EditionWordVO> validWords = words.stream().filter(e -> dirIds.contains(e.getDirectoryId())).collect(Collectors.toList());
    if (validWords.isEmpty()) {
        return Stream.empty();
    }

    return fids.stream().filter(f -> StringUtils.isNotBlank(f.getEvalText())).flatMap(f -> {
        //单词类型匹配,把框文本拆分成一个个单词
        String frameText = f.getEvalText();
//                String[] splitWords = split2Word(frameText);
        List<String> splitWords = new ArrayList<>();
        Matcher matcher = SINGLE_WORD_PATTERN.matcher(frameText);
        while (matcher.find()) {
            splitWords.add(matcher.group());
        }

        return validWords.stream().flatMap(e -> {
            if (isChinese && frameText.contains(e.getText())) { //中文类型
                return Stream.of(e);
            }

            if (e.getType() >= 5 && StringUtils.containsIgnoreCase(frameText, e.getText())) { //英语会认的词、会写的词 不区分大小写匹配
                return Stream.of(e);
            }

            String[] texts = e.getTexts().toArray(new String[]{});
            if (texts.length == 0 || splitWords.isEmpty()) {
                return Stream.empty();
            }

            return splitWords.stream().filter(t -> StringUtils.equalsAnyIgnoreCase(t, texts))
                    .map(t -> {
                        EditionWordVO vo = new EditionWordVO();
                        BeanUtils.copyProperties(e, vo);
                        vo.setText(t); //设置匹配的文本,无需后面再匹配一次
                        return vo;
                    });
        }).map(e -> new AbstractMap.SimpleEntry<>(new PageFingerInfoPO().setIndexType(TYPE_FINGER_VIRTUAL).setPageId(f.getPageId()).setMainFingerId(f.getId()).setEvalText(e.getText()), e));
    });
}).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a, b) -> a));

自定义去重key

    public static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
        Map<Object, Boolean> seen = new ConcurrentHashMap<>();
        return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值