java8 forEach、filter、map

2019-11-19 01:20:18

参考地址 java8 forEach、filter、map

1 forEach

forEach用于遍历元素。


1.1 准备数据

List<Employee>:


List<Employee> employeeList = new ArrayList<>();

employeeList.add(new Employee("Alice", 23, "London", 1200.00));

employeeList.add(new Employee("Bob", 19, "London", 2000.00));

employeeList.add(new Employee("Charles", 25, "New York", 1650.00));

employeeList.add(new Employee("Dorothy", 20, "Hong Kong", 1200.00));

1

2

3

4

5

Map<String, Employee>:


// key - name, value - Employee

Map<String, Employee> map1 = employeeList.stream()

                .collect(toMap(Employee::getName, Function.identity()));

1

2

3

Person:


public class Person {


    private String userName;

    private Integer age;


    public Person() {

    }


    public String getUserName() {

        return userName;

    }


    public void setUserName(String userName) {

        this.userName = userName;

    }


    public Integer getAge() {

        return age;

    }


    public void setAge(Integer age) {

        this.age = age;

    }

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

1.2 在List中使用forEach

例子:


(1)获取所有Employee的name


List<String> names = new ArrayList<>();

employeeList.forEach(employee -> names.add(employee.getName()));

1

2

1.3 在Map中使用forEach

例子:


(1)获取所有Employee的人员信息


List<String> personInfos = new ArrayList<>();

map1.forEach((key, value) -> personInfos.add(key + "/" + value.getAge() + "/" + value.getCity()));

1

2

2 filter

filter用于过滤元素。


2.1 通过filter()过滤元素,通过collect() 收集元素

例子:


(1)筛选出住在London的Employee


List<Employee> afterFilter = employeeList.stream()

                .filter(employee -> "London".equals(employee.getCity()))

                .collect(toList());

1

2

3

2.2 filter()、findAny()、orElse()配合使用

filter()、findAny()、orElse()配合使用,可以根据条件获取某个元素,如果没有返回指定的值。


2.2.1 单条件

例子:


(1)找到名字为Alice的任何一个对象,如果不存在返回null


Employee alice = employeeList.stream()

                .filter(employee -> "Alice".equals(employee.getName()))

                .findAny()

                .orElse(null);

1

2

3

4

2.2.2 多条件

例子:


(1)根据city和age筛选对象


Employee result = employeeList.stream()

.filter(employee -> ("London".equals(employee.getCity()) && employee.getAge() < 20))

.findAny()

.orElse(null);

1

2

3

4

2.3 通过filter()过滤,通过map()转换

有时候经过筛选之后,我们想得到的可能不是对象本身,而是对象中的一个属性,可以通过map转换。


例子:


(1)找到名字为Alice的任何一个对象,返回它的属性,如果不存在返回""


String attribute = employeeList.stream()

.filter(employee -> "Alice".equals(employee.getName()))

.map(employee -> (employee.getName() + "/" + employee.getAge() + "/" + employee.getCity()))

.findAny()

.orElse("");

1

2

3

4

5

3 map

通过map可以将一种类型的对象转换成另一种类型的对象。


3.1 简单的同类型转换

(1)小写字母转换为大写字母


List<String> arrayBefore = Arrays.asList("a", "b", "c", "d");

List<String> arrayAfter = arrayBefore.stream()

.map(String::toUpperCase)

.collect(toList());

1

2

3

4

3.2 获取对象集合的某个属性集合

可以通过map提取对象集合的某个属性集合。


例子:


(1)获取List<Employee>中的name集合


List<String> nameList = employeeList.stream()

                .map(Employee::getName)

                .collect(toList());

1

2

3

3.3 一种类型的对象集合转换成另一种类型的对象集合

例子:


(1)将List<Employee> 转换为List<Person>


List<Person> personList = employeeList.stream()

                .map(employee -> {

                    Person person = new Person();

                    person.setName(employee.getName());

                    return person;

                })

                .collect(toList());

1

2

3

4

5

6

7



  • 2019-04-30 13:55:13

    浅谈mysql fulltext全文索引优缺点

    为什么会注意到mysql的fulltext? nima, 还是上次innodb转成tokudb引擎的事,这次alter修改表引擎的时候,提示percona tokudb是不支持fulltext索引的.

  • 2019-04-30 18:56:52

    elasticsearch文档操作

    使用了Elasticsearch提供的一整套强大的REST API,本文继续来看通过这一套API如何完成文档的基本操作。

  • 2019-05-05 14:04:11

    PHP使用CURL模拟POST/GET/PUT/DELETE方式提交数据

    最近因为工作需要,调用网盘接口来上传文件,我用了CURL库, 当然在用CURL库之前必须要在php中启用 cURL 设置 可以通过使用php_info()函数来得到cURL信息,如果看不到cURL信息的话,那么需要设置PHP并开启这个库。在Windows平台下,需要改一改php.ini文件的设置,找到 php_curl.dll,并取消前面的分号注释就行了。

  • 2019-05-07 00:04:04

    Tcpdf不使用css来实现线上合同和签章,图片悬浮

    以前虽然有了解过Tcpdf的用法,但是没有实际的应用过,最近在用tcpdf的时候发现对css支持很有限,使的如果想实现类似html的种种效果都很难,而我这次要做的就是 图片悬浮在文字上方形成类似水印或者盖章的效果。

  • 2019-05-09 11:46:30

    Glide使用高级技巧(解决Glide生成缓存Key问题)

    虽说Glide将缓存功能高度封装之后,使得用法变得非常简单,但同时也带来了一些问题。 比如之前有一位群里的朋友就跟我说过,他们项目的图片资源都是存放在七牛云上面的,而七牛云为了对图片资源进行保护,会在图片url地址的基础之上再加上一个token参数。也就是说,一张图片的url地址可能会是如下格式: