Java stream常用实例代码

Posted Neo Yang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java stream常用实例代码相关的知识,希望对你有一定的参考价值。

目录

定义对象实体

package com.elon.model;

/**
 * 简单用户实体类
 *
 * @author elon
 */
public class User 

    /**
     * 用户账号
     */
    private String account = "";

    /**
     * 用户名
     */
    private String name = "";

    /**
     * 年龄
     */
    private int age = 0;

    public User() 

    

    public User(String account, String name, int age)
        this.account = account;
        this.name = name;
        this.age = age;
    

    public String getName() 
        return name;
    

    public void setName(String name) 
        this.name = name;
    

    public String getAccount() 
        return account;
    

    public void setAccount(String account) 
        this.account = account;
    

    public int getAge() 
        return age;
    

    public void setAge(int age) 
        this.age = age;
    


场景一 根据属性过滤对象

    /**
     * 使用流过滤数据.
     *
     * @author elon
     */
    public static void filterUser(List<User> userList) 
        // 1、过滤数据
        List<User> filterUserList = userList.stream().filter(u->u.getAge() > 12).collect(Collectors.toList());

        // 2、打印过滤结果数量
        System.out.printf("过滤后的数据:%s%n", new Gson().toJson(filterUserList));
    

场景二 提取对象中的属性

    /**
     * 提取对象中的某个属性值。
     *
     * @param userList 用户对象列表
     * @author elon
     */
    public static void distillField(List<User> userList) 
        List<String> nameList = userList.stream().map(User::getName).collect(Collectors.toList());

        // 打印提取的用户名
        System.out.printf("打印提取的用户名:%s%n", new Gson().toJson(nameList));
    

场景三 按对象属性分组

基于对象中的某个属性分组,属性值相同的对象放一组。

    /**
     * 根据某个属性对对象做分组.
     *
     * @param userList 用户列表
     * @auhtor elon
     */
    public static void groupUser(List<User> userList) 
        Map<Integer, List<User>> userMap = userList.stream().collect(Collectors.groupingBy(User::getAge));

        System.out.printf("分组后的对象:%s\\n", new Gson().toJson(userMap));
    

场景四、对象列表转换为Map

将list转换为Map的目的是为了在后面可以快速查询。

package com.elon.service;

import com.elon.model.User;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class Transform2MapClass 

    /**
     * 提取列表中的对象属性,转换为Map
     *
     * @param userList 用户对象列表
     * @author elon
     */
    public static void transform2Map(List<User> userList) 
        Map<String, Integer> account2AgeMap = userList.stream().collect(Collectors.toMap(
                User::getAccount, User::getAge, (k1,k2)->k1, HashMap::new));
        System.out.printf("提取的用户账号和年龄键值对:%s\\n", new Gson().toJson(account2AgeMap));

        Map<String, User> account2UserMap = userList.stream().collect(Collectors.toMap(User::getAccount, user -> user));
        System.out.printf("提取的用户账号和对象键值对:%s\\n", new Gson().toJson(account2UserMap));
    


场景五、列表去重

    /**
     * 列表去重.
     *
     * @param userList 用户列表
     * @author elon
     */
    public static void distinct(List<User> userList) 

        List<User> distinctUserList = userList.stream().distinct().collect(Collectors.toList());

        System.out.printf("打印去重后的用户信息:%s%n", new Gson().toJson(distinctUserList));
    

对象使用的是equals方法比较去重。如果是对象列表去重,对象类需要改写hashCode和equals方法。

以上是关于Java stream常用实例代码的主要内容,如果未能解决你的问题,请参考以下文章

java stream的常用例子

Java 8 Stream API实例

各种stream类中的close()方法

Java8 List Stream常用操作总结记录

Java8 List Stream常用操作总结记录

java8 Stream 常用方法讲解