JDK8新特性(常用)

DBC 371 0

一、stream().filter()的用法

stream.filter一般适用于list集合,主要作用就是模拟sql查询,从集合中查询想要的数据。filter里面的参数user是指集合里面的每一项

具体测试代码如下,相信看起来非常简单!

/**
 * @author DBC
 * @date 2022/4/27 11:19
 */
public class Test {
    public static void main(String[] args) {
        List<User> list = new ArrayList<>();
        //定义三个用户对象
        User user1 = new User();
        user1.setUsername("dbc");
        user1.setPassword("1111111111");
        User user2 = new User();
        user2.setUsername("奥特曼");
        user2.setPassword("222222222");
        User user3 = new User();
        user3.setUsername("猪头");
        user3.setPassword("33333333");
        User user4 = new User();
        user4.setUsername("鸭头");
        user4.setPassword("222222222");
        //添加用户到集合中
        list.add(user1);
        list.add(user2);
        list.add(user3);

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

    }
}

控制台输出

[User{username='dbc', password='1111111111'}]
Optional[User{username='奥特曼', password='222222222'}]

二、将一个list转换成一个由id为key,item为value的map集合

        ConcurrentMap<Long, GbDataFamilyIntegration> gbDataFamilyIntegrationMap = dataFamilyIntegrations.parallelStream().collect
                (Collectors.toConcurrentMap(GbDataFamilyIntegration::getFamilyCode, GbDataFamilyIntegration -> GbDataFamilyIntegration));

三、将一个列表转换为一个由自定义key,以列表为value的map,具体如下

        // 列表转map
        Map<Integer,List<ProjectFiles>> stateMap =  filesList
                .stream().collect(Collectors.groupingBy(ProjectFiles::getProjectStatus));

四、获得所有的id列表

        // 获取所有需要查询的id
        List<Long> questionnaireIdList = questionnaireInfoDOPage.getRecords().stream().map(QuestionnaireInfoDO::getId).collect(Collectors.toList());

五、从一个对象数组中通过一些规则取出想要的map

        Map<Integer, ProjectFilesVO> recordIdMap = filesVOList
                .stream().distinct().collect(Collectors.toMap(ProjectFilesVO::getProjectStatus, Function.identity(),(v1,v2)->v1));

        Map<Integer, ProjectFilesVO> recordIdMap = filesVOList
                .stream().distinct().collect(Collectors.toMap(ProjectFilesVO::getProjectStatus, Function.identity());
温馨提示

两种方法各有千秋,第二种可能会因为有重复的key报异常,第一种不会,但是也能很清晰的看出来,会有舍弃的操作在里面,各有利弊,根据项目情况而定。

六、stream.filter进阶用法

自定义过滤数组,相当于sql中的where

            resultLists = populationIncomeItemVOPage
                    .parallelStream().filter(item -> filterData(item, householderIdcard, householderName)).collect(Collectors.toList());
 // 自定义过滤器
    private boolean filterData(PovertyMonitoringHouseholdSectionVO item, String getHouseholderIdcard, String getHouseholderName) {
        boolean one = true;
        boolean two = true;
        if (StrUtil.isNotBlank(getHouseholderIdcard)) {
            one = item.getHouseholderIdcard().equals(getHouseholderIdcard);
        }
        if (StrUtil.isNotBlank(getHouseholderName)) {
            two = item.getHouseholderName().equals(getHouseholderName);
        }

        return one && two;
    }

过滤null值

.filter(Objects::nonNull)

七、数组金额汇总

 LongSummaryStatistics summaryNoRegister = resultCityFourList.stream().collect(Collectors.summarizingLong(StatisticsPropertyInfoDO::getNoRegisterAssetsProject));
        BigDecimal originalValue = resultCityFourList.stream()
                .map(StatisticsPropertyInfoDO::getOriginalValue)
                .reduce(BigDecimal.ZERO, BigDecimal::add)
                .setScale(6, BigDecimal.ROUND_HALF_UP);

发表评论 取消回复
表情 图片 链接 代码

分享