简体   繁体   English

如何使用迭代器和 for 循环中的流/过滤器/lambdas?

[英]How do I use streams/filters/lambdas from an iterator and for loop?

I'm not too familiar with streams, filters, and lambdas as I'm used to writing older Java code.我不太熟悉流、过滤器和 lambda,因为我习惯于编写较旧的 Java 代码。 I wanted to try using new things in newer versions of Java.我想尝试在较新版本的 Java 中使用新东西。 I can't seem to get anything started without getting compiling problems.如果没有编译问题,我似乎无法开始任何事情。 Any help is appreciated, thank you.任何帮助表示赞赏,谢谢。

List<Obj> a = getListForA();    // ['werdsegs', 'wsghnmrst', 'vaasdfdas', 'iujhgfds']
List<Obj> b = getListForB();    // ['aalaksdjf', 'erftghjuk', 'werdsejfksd', 'asdklgjaklgj', 'poijgndf']

a.forEach((item) -> {
    String foo = item.substring(0,5);
    for(Iterator<Obj> i = b.iterator(); i.hasNext();) {
        Obj o = i.next();
        if(foo.equals(o.substring(0,5)) {
            i.remove();
        }
    }
});

I expect for any item that contains the substring, remove it from the list for b.我希望对于包含子字符串的任何项目,将其从 b 的列表中删除。

After the code runs, list b should show the list below with werdsejfksd missing from ['aalaksdjf', 'erftghjuk', 'asdklgjaklgj', 'poijgndf']代码运行后,列表B应该显示在列表下面werdsejfksd从失踪['aalaksdjf', 'erftghjuk', 'asdklgjaklgj', 'poijgndf']

Your code runs fine if you replace Obj with String and fix the compilation errors:如果您用String替换Obj并修复编译错误,您的代码运行良好:

List<String> a = Arrays.asList("werdsegs", "wsghnmrst", "vaasdfdas", "iujhgfds");
List<String> b = new ArrayList<>(Arrays.asList("aalaksdjf", "erftghjuk", "werdsejfksd", "asdklgjaklgj", "poijgndf"));

a.forEach((item) -> {
    String foo = item.substring(0,5);
    for (Iterator<String> i = b.iterator(); i.hasNext(); ) {
        String o = i.next();
        if (foo.equals(o.substring(0,5))) {
            i.remove();
        }
    }
});

System.out.println(b); // prints: [aalaksdjf, erftghjuk, asdklgjaklgj, poijgndf]

As suggested by jonathan Heindl in another answer , you can simplify the code by using removeIf(...) .正如removeIf(...)另一个答案中所建议的那样,您可以使用removeIf(...)来简化代码。 I'd build a Set first, though, for better performance:不过,为了更好的性能,我会先构建一个Set

List<String> a = Arrays.asList("werdsegs", "wsghnmrst", "vaasdfdas", "iujhgfds");
List<String> b = new ArrayList<>(Arrays.asList("aalaksdjf", "erftghjuk", "werdsejfksd", "asdklgjaklgj", "poijgndf"));

Set<String> set = a.stream().map(s -> s.substring(0,5)).collect(Collectors.toSet());
b.removeIf(s -> set.contains(s.substring(0,5)));

System.out.println(b); // prints: [aalaksdjf, erftghjuk, asdklgjaklgj, poijgndf]

If you want a new list, instead of modifying b , you can use the filter(...) method of Stream :如果你想要一个新列表,而不是修改b ,你可以使用Streamfilter(...)方法:

List<String> a = Arrays.asList("werdsegs", "wsghnmrst", "vaasdfdas", "iujhgfds");
List<String> b = Arrays.asList("aalaksdjf", "erftghjuk", "werdsejfksd", "asdklgjaklgj", "poijgndf");

Set<String> set = a.stream().map(s -> s.substring(0,5)).collect(Collectors.toSet());
List<String> c = b.stream().filter(s -> ! set.contains(s.substring(0,5))).collect(Collectors.toList());

System.out.println(c); // prints: [aalaksdjf, erftghjuk, asdklgjaklgj, poijgndf]

I guess like this我猜像这样

List<String> a = getListForA();    // ['werdsegs', 'wsghnmrst', 'vaasdfdas', 'iujhgfds']
List<String> b = getListForB();    // ['aalaksdjf', 'erftghjuk', 'werdsejfksd', 'asdklgjaklgj', 'poijgndf']

b.removeIf(bObj->a.stream().anyMatch(aObj->aObj.substring(0,5).equals(bObj.substring(0,5))));

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

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