Java study notes-stream().filter()

Recently I saw other people use this kind of thing:
stream().filter(timerTaskDTO -> ("N")
        .equals(timerTaskDTO.getIsActive())).collect(Collectors.toList());、

 

Role: Find a collection of eligible objects, so the return value must be a List

 

usage:

 //在集合中查询用户名为huxiansen的集合
        List<User> userList = list.stream().filter(user -> "huxiansen".equals(user.getUsername())).collect(Collectors.toList());
 //在集合中查询出第一个用户密码为123456的用户
        Optional<User> user = list.stream().filter(userTemp -> "123456".equals(userTemp.getPassword())).findFirst();

 

I have also seen others use it like this:

User user1 = userList.stream().filter(user -> "zhangsan".equals(user.getUserName())).findAny().orElse(null);

findAny() means to return any of them; [Note: In Java 8 Stream, findFirst() returns the first element in the Stream, and findAny() returns any element in the Stream. ]
OrElse(null) means that if none is found, null is returned.

Guess you like

Origin blog.csdn.net/mumuwang1234/article/details/111499555