JAVA 8 stream学习
本文最后更新于65 天前,其中的信息可能已经过时,如有错误请发送邮件到434658198@qq.com

1、什么是stream

Stream 使用一种类似用 SQL 语句从数据库查询数据的直观方式来提供一种对 Java 集合运算和表达的高阶抽象。

这种风格将要处理的元素集合看作一种流, 流在管道中传输, 并且可以在管道的节点上进行处理, 比如筛选, 排序,聚合等。

元素流在管道中经过中间操作(intermediate operation)的处理,最后由最终操作(terminal operation)得到前面处理的结果。

2、常用的流操作

2.1、map:map 方法用于映射每个元素到对应的结果

@Slf4j
public class Streamdemo {
    
    public static List<User> createList() {

        User user = new User(1L, "by", 18, "音乐");
        User user1 = new User(2L, "gz", 26, "音乐");
        User user2 = new User(3L, "aby", 19, "旅行");
        List<User> userList = Lists.newArrayList(user, user1, user2);
        return userList;

    }

    public static void main(String[] args) {

        List<User> userList = createList();
        // List对象转List<Long>
        List<Long> longList = userList.stream().map(o->o.getId()).collect(Collectors.toList());
        log.info("longList"+longList);
    }

}

2.2、filter:filter 方法用于通过设置的条件过滤出元素

2.21、集合相减

@Slf4j
public class Streamdemo {

    public static List<User> createList() {

        User user = new User(1L, "by", 18, "音乐");
        User user1 = new User(2L, "gz", 26, "音乐");
        User user2 = new User(3L, "aby", 19, "旅行");
        List<User> userList = Lists.newArrayList(user, user1, user2);
        return userList;

    }

    public static List<User> newList() {

        User user = new User(1L, "by", 18, "音乐");
        User user1 = new User(2L, "gz", 26, "音乐");
        List<User> userList = Lists.newArrayList(user, user1);
        return userList;

    }

    public static void main(String[] args) {

        List<User> userList = createList();
        List<User> newList = newList();
        //userList - newList
        List<User> resultList = createList().stream().filter(o->!newList.contains(o)).collect(Collectors.toList());
        log.info("resultList"+resultList);

    }

}

2.3 Collectors : Collectors 类实现了很多归约操作

2.31、List转Map

@Slf4j
public class Streamdemo {


    public static List<User> createList() {

        User user = new User(1L, "by", 18, "音乐");
        User user1 = new User(2L, "gz", 26, "音乐");
        User user2 = new User(3L, "aby", 19, "旅行");
        List<User> userList = Lists.newArrayList(user, user1, user2);
        return userList;

    }

    public static void main(String[] args) {

        List<User> userList = createList();

        //key = id , value = userName
        Map<Long, String> lsMap = userList.stream().collect(Collectors.toMap(User::getId, User::getUserName));
        log.info("lsMap" + lsMap);

        //key = userName , value = love
        Map<String, String> ssMap = userList.stream().collect(Collectors.toMap(o -> o.getUserName(), o -> o.getLove()));
        log.info("ssMap" + ssMap);

        //key = id , value = User
        Map<Long, User> luMap = userList.stream().collect(Collectors.toMap(o -> o.getId(), o -> o));
        log.info("luMap" + luMap);

        //key = love + userName , value = User
        Map<String,User> map = new HashMap<>(16);
        userList.forEach(o->{
            map.put(o.getLove()+o.getUserName(),o);
        });

    }

参考:https://lw900925.github.io/java/java8-stream-api.html

如果觉得本文对您有所帮助,可以支持下博主,一分也是缘
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇