Java8对list集合进行排序、过滤、分组、去重、转map、遍历赋值等操作

转载自: https://www.e-learn.cn/topic/2522732

import lombok.Data;

import java.util.*;
import java.util.stream.Collectors;

public class TestMain {

    //测试集合
    private List<News> newsList = new ArrayList<>();

    public static void main(String[] args) {
        TestMain testMain = new TestMain();

        //初始集合
        List<News> initList = testMain.newsList;
        //待赋值集合
        List<News> otherList = new ArrayList<>();

        //单个属性排序,按title字符串排序
        List<News> titleSortList = initList.stream().sorted(Comparator.comparing(News::getTitle)).collect(Collectors.toList());

        //多个属性排序, 按title、author字符串排序 thenComparing可以多个...
        List<News> titleAndAuthorSortList = initList.stream().sorted(Comparator.comparing(News::getTitle)
                .thenComparing(Comparator.comparing(News::getAuthor)))
                .collect(Collectors.toList());

        //多个属性排序, 按title、hitNum倒序
        List<News> titleAndHitsSortList = initList.stream().sorted(Comparator.comparing(News::getTitle)
                .thenComparing(Comparator.comparing(News::getHits).reversed()))
                .collect(Collectors.toList());

        //多个属性排序,按title、hot排序降序,hot需转成数字进行排序
        List<News> titleAndHotSortList = initList.stream().sorted(Comparator.comparing(News::getTitle)
                .thenComparing((v1, v2) -> Integer.valueOf(v2.getHot()).compareTo(Integer.valueOf(v1.getHot()))))
                .collect(Collectors.toList());

        //集合过滤,hot大于30的数据
        List<News> hotFilterList = initList.stream().filter(news -> news.getHits() > 30).collect(Collectors.toList());

        //按照title/content字段进行去重
        List<News> titleRepeatList = initList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(
                () -> new TreeSet<>(Comparator.comparing(o -> o.title + o.content))),
                ArrayList::new));

        //从 List 中取出title属性的组成 新的list 集合
        List<String> titleList = initList.stream().map(News::getTitle).collect(Collectors.toList());

        //List转Map key:title,value:content;使用去重后的集合防止key不唯一
        Map<String, String> titleAndContentMap = titleRepeatList.stream().collect(Collectors.toMap(o -> o.getTitle(), o -> o.getContent()));

        //单个属性分组,title分组
        Map<String, List<News>> titleMap = initList.stream().collect(Collectors.groupingBy(News::getTitle));
        //多个属性分组,title、author分组,groupingBy可以为多个
        Map<String, Map<String, List<News>>> titleAndAuthorMap = initList.stream().collect(Collectors.groupingBy(News::getTitle, Collectors.groupingBy(News::getAuthor)));

        //利用stream().map()处理List,并给另外一个List赋值
        otherList = initList.stream().map(news -> {return news;}).collect(Collectors.toList());

        //list遍历
        initList.forEach(news -> {
            System.out.println(news.getContent());
        });

        //map遍历
        titleAndAuthorMap.forEach((k,v) -> {
            System.out.println(k);
            Map<String, List<News>> map = v;
        });

    }

    //代码块初始化数据
    {
        int i = 50;
        while (i > 0) {
            //1-5随机数
            int num = (int) (Math.random() * 5 + 1);
            //1-50随机数
            int hitNum = (int) (Math.random() * 50 + 1);
            //1-500随机数
            int hotNum = (int) (Math.random() * 500 + 1);
            News news = new News();
            news.setTitle("title" + num);
            news.setAuthor("author" + num);
            news.setContent("content" + num);
            news.setHits(hitNum);
            news.setHot(hotNum + "");
            newsList.add(news);
            i--;
        }
    }

    /**
     * 测试内部类
     */
    @Data
    class News {

        //标题
        private String title;
        //作者
        private String author;
        //内容
        private String content;
        //点击数
        private int hits;
        //热度
        private String hot;
    }


}

 

posted @ 2020-11-27 10:32  fy_qxl  阅读(3589)  评论(0编辑  收藏  举报