在Java 8中,我可以使用流来过滤部分字符串吗? [英] In Java 8, can I use streams to filter a partial string?
本文介绍了在Java 8中,我可以使用流来过滤部分字符串吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Java 8中,我可以使用流来过滤部分字符串吗?
In Java 8, can I use streams to filter a partial string?
我们假设我有一个动物列表,如:
Let us assume I have a list of animals like:
Brown Bear
Black Bear
Black Crow
Red Herring
Owl
Sparrow
Blackbacked Flocking Crow
让我们假设所有动物名称都在动物对象列表中
Let us assume all of the Animals names are in a list of Animals Objects
public class Animal{
public name;
public animalType;
}
有没有办法找到所有有黑色的动物,无论在某个名称的某个地方。如下所示...
Is there some way to find all of the animals that have Black regardless of the case somewhere in the name. Something like the following...
List<Animal> filtList = employeeList.stream()
.filter(x -> "%Black%".toUpperCase().equals(x.getLastName().toUpper()))
.collect(Collectors.toList());
推荐答案
没有 toUpper( )
String
的方法。
它是 toUpperCase()
而且你需要使用 contains()
在整个字符串中的任何地方检查BLACK,因此代码应该很简单,如下所示:
It is toUpperCase()
and also you need to use contains()
to check "BLACK" there anywhere in the whole string, so the code should be simple as shown below:
List<Employee> filtList = inputList.stream().
filter(value -> value.toUpperCase().//convert to uppercase for checking
contains("BLACK")).//filter values containing black
collect(Collectors.toList());//collect as list
这篇关于在Java 8中,我可以使用流来过滤部分字符串吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文