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;
    }


}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM