简体   繁体   中英

using loop instead of stream (java9)

I wonder how to change into for or while loop by using same format

public List<String> noZ(List<String> strings) {
  return strings.stream().filter(x -> !x.contains("z")).collect(Collectors.toList());
}

This is what I did so far, but I'm not getting the results yet:

for (int i = 0; i < string.stream().size(); i++) {
   string x;
   if (x.contains("z")) {
      String.valueOf(strings.stream().filter);
   }
}

There are a few reasonable ways to rewrite this. One way:

public List<String> noZ(List<String> strings) {
    List<String> out = new ArrayList<>();
    for (String s : strings) {
        if (!s.contains("z")) {
            out.add(s);
        }
    }
    return out;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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