1、在Java之前对List进行过滤的方式

public class BeforeJava8 {

    public static void main(String[] args) {

        List<Person> peoples = Arrays.asList(
new Person("java", 22),
new Person("js", 35),
new Person("css", 31)
); Person result = getStudentByName(peoples, "java");
System.out.println(result); } private static Person getStudentByName(List<Person> peoples, String name) {
Person result = null;
for (Person person : peoples) {
if (name.equals(person.getName())) {
result = person;
}
}
return result;
}
}

2、在Java8使用Filter过滤List

public class FilterJava8 {

    public static void main(String[] args) {

        List<Person> peoples = Arrays.asList(
new Person("java", 22),
new Person("js", 35),
new Person("css", 31)
); Person result1 = peoples.stream()
.filter(p -> "java".equals(p.getName()))
.findAny()
.orElse(null);
System.out.println(result1); Person result2 = peoples.stream()
.filter(p -> "spring".equals(p.getName()))
.findAny()
.orElse(null);
System.out.println(result2); Person result3 = peoples.stream()
.filter((p) -> "java".equals(p.getName()) && 22 == p.getAge())
.findAny()
.orElse(null);
System.out.println(result3); // 使用map收集
String name = peoples.stream()
.filter(p -> "js".equals(p.getName()))
.map(Person::getName)
.findAny()
.orElse("");
System.out.println(name);
System.out.println("---------"); List<String> names = peoples.stream()
.map(Person::getName)
.collect(Collectors.toList());
names.forEach(System.out::println);
}
}

Java8过滤器(Filter)的更多相关文章

  1. Angularjs在控制器(controller.js)的js代码中使用过滤器($filter)格式化日期/时间实例

    Angularjs内置的过滤器(filter)为我们的数据信息格式化提供了比较强大的功能,比如:格式化时间,日期.格式化数字精度.语言本地化.格式化货币等等.但这些过滤器一般都是在VIEW中使用的,比 ...

  2. 网站过滤器Filter

    实际上,Filter与Servlet及其相似,区别只是FIlter的doFilter()方法里多了一个FilterChain的参数,通过该参数可以控制是否放行用户的请求.网站有了过滤器十分的方便,可以 ...

  3. paip.输出内容替换在Apache 过滤器filter的设置

    paip.输出内容替换在Apache 过滤器filter的设置 作者Attilax  艾龙,  EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog ...

  4. java Servlet中的过滤器Filter

    web.xml中元素执行的顺序listener->filter->struts拦截器->servlet. 1.过滤器的概念 Java中的Filter 并不是一个标准的Servlet ...

  5. [ionic开源项目教程] - 第6讲 过滤器filter的使用

    过滤器filter的使用 1.回顾 再熟悉一下tab1.html的代码: <div class="list"> <a ng-repeat="item i ...

  6. selvert的过滤器filter处理中文乱码

    注意问题:在学习用selvert的过滤器filter处理中文乱码时,在filter配置初始化时用了utf-8处理中文乱码,而在提交的jsp页面中却用了gbk.虽然两种都可以出来中文乱码,但是却造成了处 ...

  7. MVC之 自定义过滤器(Filter)

    MVC之 自定义过滤器(Filter) 一.自定义Filter 自定义Filter需要继承ActionFilterAttribute抽象类,重写其中需要的方法,来看下ActionFilterAttri ...

  8. Servlet中的过滤器Filter用法

    1.过滤器的概念 Java中的Filter 并不是一个标准的Servlet ,它不能处理用户请求,也不能对客户端生成响应. 主要用于对HttpServletRequest 进行预处理,也可以对Http ...

  9. Servlet中的过滤器Filter详解

    加载执行顺序 context-param->listener->filter->servlet web.xml中元素执行的顺序listener->filter->stru ...

随机推荐

  1. [daily][emacs][go] 配置emacs go-mode的编辑环境以及环境变量问题

    1. 安装go 安装go-mode 使用emacs编辑go代码的时候,你需要有正常可运行的go环境. 并且有emacs的go-mode package https://www.emacswiki.or ...

  2. [strongswan] strongswan METHOD宏

    使用METHOD宏的函数定义: METHOD(message_t, get_message_id, uint32_t, private_message_t *this) { return this-& ...

  3. Git 提示fatal: remote origin already exists 错误解决办法

    今天使用git 添加远程github仓库的时候提示错误:fatal: remote origin already exists. 最后找到解决办法如下: 1.先删除远程 Git 仓库 $ git re ...

  4. [Android]使用Spring for Android改善数据交互流程

    如果开发一个和服务端有数据交互android应用,流程通常是这样的:界面收集用户数据之后,将它转换成JSON或者XML格式的字符串,以HTTP的方式提交给服务端,获得返回的文本数据,再将数据解析为ja ...

  5. spring 初始化

    一.Spring 容器高层视图 Spring 启动时读取应用程序提供的Bean配置信息,并在Spring容器中生成一份相应的Bean配置注册表,然后根据这张注册表实例化Bean,装配号Bean之间的依 ...

  6. mvc framework ui component understand.

    mvc: .htm是v,   context和contex中的contextNode 是m,  view controller, custom controller ,component contro ...

  7. [LeetCode] 55. Jump Game_ Medium tag: Dynamic Programming

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  8. 关于课堂测试ATM系统的总结

    第一节课就是考试,是要求用Java语言编写模仿ATM的系统操作,说实话真的好难,Java语言,王主任是让我们自学的,然后就让我们写一个这比较大的程序,好难,也可能是我太笨了吧... 不过话说回来,说到 ...

  9. openFileDialog的Filter属性设置

    OpenFileDialog对话框的Filter属性说明: 首先说明一个示例,分析一下Filter属性的构成:“ Excel文件|*.xls ”,前面的“Excel文件”成为标签,是一个可读的字符串, ...

  10. Python使用DDA算法和中点Bresenham算法画直线

    title: "Python使用DDA算法和中点Bresenham算法画直线" date: 2018-06-11T19:28:02+08:00 tags: ["图形学&q ...