stream中filter写法

shjqk 2019-04-16 07:59:19
集合用stream过滤,集合中对象有firstname,lastname
方法中传入2个String变量,firstname和lastname,
想按变量内容过滤,
传入的变量可能有内容,可能是空字符串
在filter中变量为空不做过滤,变量不为空则只保留firstname和lastname相符的
这样的filter怎么写,谢谢
.stream().filter
...全文
2726 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_43859787 2020-08-18
  • 打赏
  • 举报
回复
满足条件的留下还是过滤掉老是记不清
  • 打赏
  • 举报
回复
// 生成一个 list集合 List<Person> list = Stream.of(new Person("aaa", null), new Person(null, "111"), new Person("", "222"), new Person("bbb", ""), new Person("ccc", "333")).collect(Collectors.toList()); // 将list中对象中的firstName属性或lastName属性为null或空字符串的对象过滤 List<Person> notnull = list.stream().filter(person -> // firstName属性为null的拦截 person.getFirstName() != null && // lastName属性为null的拦截 person.getLastName() != null&& // firstName属性为空字符串的拦截 !"".equals(person.getFirstName()) && // lastName属性为空字符串的拦截 !"".equals(person.getLastName()) ).collect(Collectors.toList());
frost2 2020-08-08
  • 打赏
  • 举报
回复
一年多前的贴,没想到现在才看到! 就当练练手,条件判断我没有写,有需要的可以自己去改; public class Filter { public static void main(String[] args) { List<Person> personList = new ArrayList<>(); personList.add(new Person("aaa", "111")); personList.add(new Person("aaa", "222")); personList.add(new Person("ccc", "111")); filterPerson(personList, "aaa", "111"); } private static List<Person> filterPerson(List<Person> personList, String firstName, String lastName) { //过滤掉list中firstName与参数firstName不同的元素 List<Person> firstNameList = personList.stream().filter(selectByKey(Person::getFirstName, firstName)).collect(Collectors.toList()); System.out.println("firstNameList = " + firstNameList); //过滤掉list中lastName与参数lastName不同的元素 List<Person> lastNameList = personList.stream().filter(selectByKey(Person::getLastName, lastName)).collect(Collectors.toList()); System.out.println("lastNameList = " + lastNameList); return null; } private static <T> Predicate<T> selectByKey(Function<? super T, ?> function, String name) { return t -> name == function.apply(t); } } @Getter @Setter @ToString class Person { private String firstName; private String lastName; public Person(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } }
shjqk 2019-04-17
  • 打赏
  • 举报
回复
我没表达清楚,再补充一下 如下面的集合 List<Person> personList= new ArrayList<Person>(); personList.add(new Person("aaa","111")); personList.add(new Person("aaa","222")); personList.add(new Person("bbb","111")); 实现如下的过滤效果 private static List<Person> filterPerson(List<Person> personList,String firstname,String lastname){ //如果firstname ,lastname传空,不过滤 //如果firstname 传空,lastname传 111 过滤 Person("aaa","222") //如果firstname 传aaa lastname传空 过滤 Person("bbb","111") return personList.stream().filter(......).collect(Collectors.toList()); }
大隐藏于寺 2019-04-16
  • 打赏
  • 举报
回复
就是对集合中的元素做下对比,就像用if .... else... 一样


    public static List<String> filter(List<String> list,String firstName,String lastName){
       return list.stream().filter(str -> StringUtils.isEmpty(str)
               ||  StringUtils.equals(str,firstName) || StringUtils.equals(str,lastName))
                .collect(Collectors.toList());
    }

50,523

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧