How to compare two Streams in Java 8

后端 未结 6 1882
眼角桃花
眼角桃花 2020-12-03 06:38

What would be a good way to compare two Stream instances in Java 8 and find out whether they have the same elements, specifically for purposes of unit testing?<

6条回答
  •  北荒
    北荒 (楼主)
    2020-12-03 07:21

    public static boolean equalStreams(Stream...streams) {
        List>is = Arrays.stream(streams).map(Stream::iterator).collect(Collectors.toList());
        while(is.stream().allMatch(Iterator::hasNext))
            if(is.stream().map(Iterator::next).distinct().limit(2).count()>1) return false;
        return is.stream().noneMatch(Iterator::hasNext);
    }
    

提交回复
热议问题