为了账号安全,请及时绑定邮箱和手机立即绑定

如何将其转换为流表达式?(使用原子引用)

如何将其转换为流表达式?(使用原子引用)

幕布斯7119047 2022-06-04 17:32:40
我需要更改以下代码:protected void checkNoDuplicateLabels( List<CompileResult> compileResult ) {    Set<Label> infos = new HashSet<>();    for ( AbstractTypeInfo info : infoRepo.getList() ) {        if ( info instanceof Label ) {            Label label = (Label) info;            if ( infos.contains( label ) ) {                compileResult.add( new CompileResult( Severity.FATAL, MessageFormat.format( "Duplicate label found! \n Type: '{0}' \n Language: '{1}'", label.getType(), label.getLanguage() ) ) );            }            infos.add( label );        }    }}成一条溪流。我知道将 Sets 与流一起使用的一种方法是实现 AtomicReferences,它将方法的第一行替换为:AtomicReference<Set<Label>> infos = new AtomicReference<>( new HashSet<Label>() );我怎样才能实现循环现在对流执行的相同功能?
查看完整描述

1 回答

?
牧羊人nacy

TA贡献1862条经验 获得超7个赞

你可以不这样做AtomicReference:


BinaryOperator<Label> logDuplicate = (label1, label2) -> {

    // Log label2 as duplicate

    compileResult.add(new CompileResult(Severity.FATAL, MessageFormat.format("Duplicate label found! \n Type: '{0}' \n Language: '{1}'", label2.getType(), label2.getLanguage())));

    return label1;

};


Set<Label> infos = infoRepo.getList()

                           .stream()

                           .filter(Label.class::isInstance)

                           .map(Label.class::cast)

                           .collect(toMap(identity(), identity(), logDuplicate, HashMap::new))

                           .keySet();

更新:


import static java.util.stream.Collectors.toMap;

import static java.util.function.Function.identity;


查看完整回答
反对 回复 2022-06-04
  • 1 回答
  • 0 关注
  • 87 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信