码迷,mamicode.com
首页 > 其他好文 > 详细

stream流,转set和map

时间:2021-02-08 12:35:00      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:pac   integer   rri   list集合   zhang   rgb   重写   cti   hat   

package com.mayikt.stream;

import com.mayikt.entity.UserEntity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Test01 {
    public static void main(String[] args) {
        List<UserEntity> userEntityList = new ArrayList<>();
        userEntityList.add(new UserEntity("mayikt", 20));
        userEntityList.add(new UserEntity("meite", 28));
        userEntityList.add(new UserEntity("zhangsan", 35));
        userEntityList.add(new UserEntity("xiaowei", 16));
        userEntityList.add(new UserEntity("xiaowei", 16));

        /**
         * 创建stream两种方式
         * 1.串行流
         * 2.并行流
         */
        Stream<UserEntity> stream = userEntityList.stream();

        /**
         * 转换成set集合
         */
        Set<UserEntity> userSet = stream.collect(Collectors.toSet());
        userSet.forEach(userEntity -> System.out.println(userEntity));

        /**
         * stream将list转换成map
         * list集合只有元素,此处将user的userName作为key,user对象作为value,转map
         */
        /*
        Map<String, UserEntity> userEntityMap = userSet.stream().collect(Collectors.toMap(new Function<UserEntity, String>() {
            @Override
            public String apply(UserEntity userEntity) {
                return userEntity.getUserName();
            }
        }, new Function<UserEntity, UserEntity>() {
            @Override
            public UserEntity apply(UserEntity userEntity) {
                return userEntity;
            }
        }));
        */
        Map<String, UserEntity> userEntityMap = userSet.stream()
                .collect(Collectors.toMap(userEntity -> userEntity.getUserName(), userEntity -> userEntity));
        userEntityMap.forEach((k, v) -> System.out.println(k + "," + v));

        /**
         * Stream reduce求和
         */
        Stream<Integer> integerStream = Stream.of(10, 50, 30 ,20);
        Optional<Integer> reduce = integerStream.reduce((a1, a2) -> a1 + a2);
        System.out.println(reduce.get());
    }
}

  

注意:stream转set的时候,set是去重的,因此UserEntity要重写equals和hashCode。

UserEntity:

package com.mayikt.entity;

import java.util.Objects;

public class UserEntity {

    private String userName;
    private int age;

    public UserEntity(String userName, int age) {
        this.userName = userName;
        this.age = age;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public int getAge() {
        return age;
    }

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

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        UserEntity that = (UserEntity) o;
        return age == that.age &&
                Objects.equals(userName, that.userName);
    }

    @Override
    public int hashCode() {

        return Objects.hash(userName, age);
    }

    @Override
    public String toString() {
        return "UserEntity{" +
                "userName=‘" + userName + ‘\‘‘ +
                ", age=" + age +
                ‘}‘;
    }
}

 

stream流,转set和map

标签:pac   integer   rri   list集合   zhang   rgb   重写   cti   hat   

原文地址:https://www.cnblogs.com/tenWood/p/14385961.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!