如何在同一个流中使用多个过滤器和映射函数?

huangapple go评论481阅读模式
英文:

How to use multiple filters + map in the same stream?

问题

如何将这两个流合并为一个流?

  1. List<Integer> Type1ErrorRows = errors.stream()
  2. .filter(c -> (c.getClass() == Type1Error.class))
  3. .map(c -> ((Type1Error) c).getRowNumber())
  4. .collect(Collectors.toList());
  5. List<Integer> Type2ErrorRows = errors.stream()
  6. .filter(c -> (c.getClass() == Type2Error.class))
  7. .map(c -> ((Type2Error) c).getRowNumber())
  8. .collect(Collectors.toList());
  9. List<Integer> mergedRows = Stream.concat(Type1ErrorRows.stream(), Type2ErrorRows.stream())
  10. .collect(Collectors.toList());

注意:我只提供了代码的翻译部分,不包括其他内容。

英文:

How do I merge this to a single stream?

  1. List&lt;Integer&gt; Type1ErrorRows = errors.stream()
  2. .filter(c -&gt; (c.getClass() == Type1Error.class))
  3. .map(c -&gt; ((Type1Error) c).getRowNumber())
  4. .collect(Collectors.toList());
  5. List&lt;Integer&gt; Type2ErrorRows = errors.stream()
  6. .filter(c -&gt; (c.getClass() == Type2Error.class))
  7. .map(c -&gt; ((Type2Error) c).getRowNumber())
  8. .collect(Collectors.toList());

EDIT: Sorry, the question is misleading in this form (really stupid of me), I forgot to mention that I want all the row numbers in a single list as a result:

  1. Stream.concat(Type1ErrorRows.stream(), Type2ErrorRows.stream())
  2. .collect(Collectors.toList());

答案1

得分: 1

我会假设存在一个常见的接口,否则所有所需的转换将使流程变得太繁琐。

  1. public interface ErrorInterface {
  2. int getRowNumber();
  3. }

过滤对象的类是否被允许,进行类型转换,调用方法获取行号。

  1. Set<Class<?>> allowedClasses = Set.of(Type1Error.class, Type2Error.class);
  2. List<Integer> rowNumbers = errors.stream()
  3. .filter(err -> allowedClasses.contains(err.getClass()))
  4. .map(err -> (ErrorInterface) err)
  5. .map(ErrorInterface::getRowNumber)
  6. .toList();
英文:

I'll assume that a common interface exists, otherwise all the required casting will make it too cumbersome for streams.

  1. public interface ErrorInterface {
  2. int getRowNumber();
  3. }

Filter that object's class is allowed, cast, invoke method to get row number.

  1. Set&lt;Class&lt;?&gt;&gt; allowedClasses = Set.of(Type1Error.class, Type2Error.class);
  2. List&lt;Integer&gt; rowNumbers = errors.stream()
  3. .filter(err -&gt; allowedClasses.contains(err.getClass()))
  4. .map(err -&gt; (ErrorInterface) err)
  5. .map(ErrorInterface::getRowNumber)
  6. .toList();

答案2

得分: -2

流不是这个任务的正确工具。只需使用循环:

  1. List<Integer> type1 = new ArrayList<Integer>();
  2. List<Integer> type2 = new ArrayList<Integer>();
  3. for (Error error : errors) {
  4. if (error instanceof Type1Error e1) {
  5. type1.add(e1.getRowNumber());
  6. }
  7. else if (error instanceof Type2Error e2) {
  8. type2.add(e2.getRowNumber());
  9. }
  10. }
英文:

Streams are not the right tool for this job. Just use a loop:

  1. List&lt;Integer&gt; type1 = new ArrayList&lt;Integer&gt;();
  2. List&lt;Integer&gt; type2 = new ArrayList&lt;Integer&gt;();
  3. for (Error error : errors) {
  4. if (error instanceof Type1Error e1) {
  5. type1.add(e1.getRowNumber());
  6. }
  7. else if (error instanceof Type2Error e2) {
  8. type2.add(e2.getRowNumber());
  9. }
  10. }

huangapple
  • 本文由 发表于 2023年8月11日 15:25:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76881472.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定