繁体   English   中英

Java 8 Stream:1个对象到2个对象

[英]Java 8 Stream: 1 Object to 2 Objects

是否可以使用Stream API执行类似的操作?

 LinkedList<Point> l = new LinkedList<>();
        for(Edge e : EDGES){
            l.add(e.p1);
            l.add(e.p2);
        }

我想从三角形的3个边中获取点A和点B,并使用.distinct消除重复项

您可以使用flatMap生成所有边的所有Point的Stream,并使用distinct()删除重复项:

LinkedList<Point> l =
    EDGES.stream()
         .flatMap(e->Stream.of(e.p1,e.p2))
         .distinct()
         .collect(Collectors.toCollection(LinkedList::new);

就像是

List<Point> l = EDGES.stream().flatMap(e -> Stream.of(e.p1, e.p2)).distinct().collect(Collectors.toList());

或使用Set

Set<Point> l = EDGES.stream().flatMap(e -> Stream.of(e.p1, e.p2)).collect(Collectors.toSet());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM