多个循环代码迁移到 java 8:这是最好的方法吗?

我有以下旧式 java 多重循环,我想将其转换为 Java 8 流:


    List<Integer> list1 = Arrays.asList(1,2,3);

    List<Integer> list2 = Arrays.asList(3,5,6);

    List<Integer> list3 = Arrays.asList(3,4,9);

    boolean match = true;


    int list1Size = list1.size();

    int list2Size = list2.size();


    for (int i = 0; i < list1Size; i++ ) {

        for (int j = 0; j < list2Size && i < list1Size; j++) {

            if (list2.get(j).equals(list1.get(i))) {

                if (list3.get(j).equals(list1.get(i))) {

                    System.out.println(list1.get(i));

                }

            }

        }

    }

(输出为:3)


我能找到的最好方法是:


    IntStream.range(0, list1Size).forEach(

            ix1 -> {

                    IntStream.range(0, list2.size()).forEach(

                            ix2 -> {

                                if (ix2 < list2.size() && ix1 < list1.size())

                                    if (list2.get(ix2).equals(list1.get(ix1))) {

                                        if (list3.get(ix2).equals(list1.get(ix1))) {

                                            System.out.println(list1.get(ix1));

                                        }

                                    }

                            }

                    );

            }

    );

这对我来说似乎不太有吸引力......


请注意,我正在使用 2 个循环处理 3 个列表。


有没有更好的办法?


慕后森
浏览 54回答 3
3回答

qq_遁去的一_1

如果位置不重要,而您使用Set它,那么这将变得非常微不足道:set1.retainAll(set2); set1.retainAll(set3);使用流你可以这样做:set1.stream() &nbsp;&nbsp;&nbsp;.filter(set2::contains) &nbsp;&nbsp;&nbsp;.filter(set3::contains) &nbsp;&nbsp;&nbsp;.collect(Collectors.toList());

慕斯王

你可以这样做:Set<Integer>&nbsp;set1&nbsp;=&nbsp;new&nbsp;HashSet<>(list1); IntStream.range(0,&nbsp;Math.min(list2.size(),&nbsp;list3.size())) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.filter(i&nbsp;->&nbsp;Objects.equals(list2.get(i),&nbsp;list3.get(i)))&nbsp;//&nbsp;check&nbsp;where&nbsp;index&nbsp;have&nbsp;the&nbsp;same&nbsp;value &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.mapToObj(list2::get)&nbsp;//&nbsp;get&nbsp;the&nbsp;value&nbsp;(as&nbsp;the&nbsp;values&nbsp;are&nbsp;equal&nbsp;could&nbsp;be&nbsp;also&nbsp;list3) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.filter(set1::contains)&nbsp;//&nbsp;check&nbsp;if&nbsp;list1&nbsp;contains&nbsp;the&nbsp;value &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.forEach(System.out::println);输出3转换list1为集合 (&nbsp;set1) 通过提高包含查询 ( ) 的性能,可以显着提高速度O(n) -> O(1)。

温温酱

由于第一个元素的索引List并不重要,因此您可以Stream遍历元素list1并搜索同时list1出现在同一索引处的元素:list2list3Optional<Integer> result =&nbsp;&nbsp; &nbsp; list1.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.filter(IntStream.range(0,list2.size())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .filter(i -> list2.get(i).equals(list3.get(i)))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .mapToObj(list2::get)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toSet())::contains)&nbsp; &nbsp; &nbsp; &nbsp; .findFirst();或者,如果您只想打印第一个匹配项:list1.stream()&nbsp; &nbsp; &nbsp;.filter(IntStream.range(0,list2.size())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .filter(i -> list2.get(i).equals(list3.get(i)))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .mapToObj(list2::get)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toSet())::contains)&nbsp; &nbsp; &nbsp;.limit(1)&nbsp; &nbsp; &nbsp;.forEach(System.out::println);管道IntStream生成同时出现在同一索引中Set的所有元素。搜索that的元素属于 that 。list2list3filterlist1Set
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java