java 流过滤并包含
java streams filter and include
我这里有一个单词数组
我想获取所有单词:
- 长度超过 5 个字符并且
- 所有以 'i' 开头的单词,无论长度如何。
如何达到第二个标准?
List<String> words = Arrays.asList("art", "again", "orphanage", "forest", "cat", "bat");
List<String> result = words.stream()
.filter(word -> word.length() > 5)
.collect(Collectors.toList());
result.forEach(word -> System.out.println(word));
我的预期输出应该是 art again orphanage forest
Predicate<String> isStartWithA = word -> word.startsWith("a");
Predicate<String> fiveChar = word -> word.length() > 5;
List<String> words=Arrays.asList("art", "again", "orphanage", "forest", "cat", "bat");
List<String> result = words.stream() .filter(isStartWithA.or(fiveChar))
.collect(Collectors.toList());
result.forEach(word -> System.out.println(word))
我这里有一个单词数组
我想获取所有单词:
- 长度超过 5 个字符并且
- 所有以 'i' 开头的单词,无论长度如何。
如何达到第二个标准?
List<String> words = Arrays.asList("art", "again", "orphanage", "forest", "cat", "bat");
List<String> result = words.stream()
.filter(word -> word.length() > 5)
.collect(Collectors.toList());
result.forEach(word -> System.out.println(word));
我的预期输出应该是 art again orphanage forest
Predicate<String> isStartWithA = word -> word.startsWith("a");
Predicate<String> fiveChar = word -> word.length() > 5;
List<String> words=Arrays.asList("art", "again", "orphanage", "forest", "cat", "bat");
List<String> result = words.stream() .filter(isStartWithA.or(fiveChar))
.collect(Collectors.toList());
result.forEach(word -> System.out.println(word))