후안 Pollacchi :
나는 아래의 코드를 변경해야합니다 :
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 );
}
}
}
스트림으로. 나는 스트림을 사용 설정 한 방법이에 방법의 첫 번째 줄을 대체 할 AtomicReferences을 구현하는 것입니다 것을 알고있다 :
AtomicReference<Set<Label>> infos = new AtomicReference<>( new HashSet<Label>() );
어떻게 루프는 스트림과 지금하고있는 것과 같은 기능을 수행 할 수 있습니까?
ETO :
당신은 그것을없이 할 수 있습니다 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;