java8 数据集过滤removeIf和filter
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java8 数据集过滤removeIf和filter,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3042字,纯文字阅读大概需要5分钟。
内容图文
![java8 数据集过滤removeIf和filter](/upload/InfoBanner/zyjiaocheng/740/3e671c360c114f7290948af445a5c6a6.jpg)
对象如下,需求:只要30岁以下的人
//求职者的实体类 public class Person { private String name;//姓名 private Integer age;//年龄 private String gender;//性别 ... //省略构造方法和getter、setter方法 ... //重写toString,方便观看结果 @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + ", gender='" + gender + '\'' + '}'; } }
1、使用Iterator
的传统写法
Collection<Person> collection = new ArrayList(); collection.add(new Person("张三", 22, "男")); collection.add(new Person("李四", 19, "女")); collection.add(new Person("王五", 34, "男")); collection.add(new Person("赵六", 30, "男")); collection.add(new Person("田七", 25, "女")); //过滤30岁以上的求职者 Iterator<Person> iterator = collection.iterator(); while (iterator.hasNext()) { Person p = iterator.next(); if (p.getAge() >= 30) iterator.remove(); } System.out.println(collection.toString());//查看结果
2、不用lambda
的removeIf写法
Collection<Person> collection = new ArrayList(); collection.add(new Person("张三", 22, "男")); collection.add(new Person("李四", 19, "女")); collection.add(new Person("王五", 34, "男")); collection.add(new Person("赵六", 30, "男")); collection.add(new Person("田七", 25, "女")); collection.removeIf(new Predicate<Person>() { @Override public boolean test(Person p) { return p.getAge()>=30;//过滤30岁以上的求职者 } }); System.out.println(collection.toString());//查看结果
3、使用lambda
的removeIf写法(只有一行了,哈哈)
Collection<Person> collection = new ArrayList(); collection.add(new Person("张三", 22, "男")); collection.add(new Person("李四", 19, "女")); collection.add(new Person("王五", 34, "男")); collection.add(new Person("赵六", 30, "男")); collection.add(new Person("田七", 25, "女")); collection.removeIf(p -> p.getAge() >= 30);//过滤30岁以上的求职者 System.out.println(collection.toString());//查看结果
4、使用lambda
的filter写法
Collection<Person> collection = new ArrayList(); collection.add(new Person("张三", 22, "男")); collection.add(new Person("李四", 19, "女")); collection.add(new Person("王五", 34, "男")); collection.add(new Person("赵六", 30, "男")); collection.add(new Person("田七", 25, "女")); Stream<Person> personStream = collection.stream().filter(p -> p.getAge() < 30)).collect(Collectors.toList()); //由于使用了stream因此后面需要使用.collect(Collectors.toList())转换成list
System.out.println(collection.toString());//查看结果
下面是没有使用lambda的写法
Collection<Person> collection = new ArrayList(); collection.add(new Person("张三", 22, "男")); collection.add(new Person("李四", 19, "女")); collection.add(new Person("王五", 34, "男")); collection.add(new Person("赵六", 30, "男")); collection.add(new Person("田七", 25, "女")); Stream<Person> personStream = collection.stream().filter(new Predicate<Person>() { @Override public boolean test(Person p) { return p.getAge() < 30;//只保留男性 } }); collection = personStream.collect(Collectors.toList());//将Stream转化为List System.out.println(collection.toString());//查看结果
转载于:https://www.cnblogs.com/tiandi/p/11185236.html
内容总结
以上是互联网集市为您收集整理的java8 数据集过滤removeIf和filter全部内容,希望文章能够帮你解决java8 数据集过滤removeIf和filter所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。