java list过滤完赋给自己_Java8对list集合进行排序、过滤、分组、去重、转map、遍历赋值等操作...
importlombok.Data;import java.util.*;importjava.util.stream.Collectors;public classTestMain {//測試集合
private List newsList = new ArrayList<>();public static voidmain(String[] args) {
TestMain testMain= newTestMain();//初始集合
List initList =testMain.newsList;//待賦值集合
List otherList = new ArrayList<>();//單個屬性排序,按title字符串排序
List titleSortList =initList.stream().sorted(Comparator.comparing(News::getTitle)).collect(Collectors.toList());//多個屬性排序, 按title、author字符串排序 thenComparing可以多個...
List titleAndAuthorSortList =initList.stream().sorted(Comparator.comparing(News::getTitle)
.thenComparing(Comparator.comparing(News::getAuthor)))
.collect(Collectors.toList());//多個屬性排序, 按title、hitNum倒序
List titleAndHitsSortList =initList.stream().sorted(Comparator.comparing(News::getTitle)
.thenComparing(Comparator.comparing(News::getHits).reversed()))
.collect(Collectors.toList());//多個屬性排序,按title、hot排序降序,hot需轉成數字進行排序
List 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 hotFilterList = initList.stream().filter(news -> news.getHits() > 30).collect(Collectors.toList());//按照title/content字段進行去重
List titleRepeatList =initList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(
()-> new TreeSet<>(Comparator.comparing(o -> o.title +o.content))),
ArrayList::new));//從 List 中取出title屬性的組成 新的list 集合
List titleList =initList.stream().map(News::getTitle).collect(Collectors.toList());//List轉Map key:title,value:content;使用去重后的集合防止key不唯一
Map titleAndContentMap = titleRepeatList.stream().collect(Collectors.toMap(o -> o.getTitle(), o ->o.getContent()));//單個屬性分組,title分組
Map> titleMap =initList.stream().collect(Collectors.groupingBy(News::getTitle));//多個屬性分組,title、author分組,groupingBy可以為多個
Map>> titleAndAuthorMap =initList.stream().collect(Collectors.groupingBy(News::getTitle, Collectors.groupingBy(News::getAuthor)));//利用stream().map()處理List,并給另外一個List賦值
otherList = initList.stream().map(news -> {returnnews;}).collect(Collectors.toList());//list遍歷
initList.forEach(news ->{
System.out.println(news.getContent());
});//map遍歷
titleAndAuthorMap.forEach((k,v) ->{
System.out.println(k);
Map> 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= newNews();
news.setTitle("title" +num);
news.setAuthor("author" +num);
news.setContent("content" +num);
news.setHits(hitNum);
news.setHot(hotNum+ "");
newsList.add(news);
i--;
}
}/*** 測試內部類*/@DataclassNews {//標題
privateString title;//作者
privateString author;//內容
privateString content;//點擊數
private inthits;//熱度
privateString hot;
}
}
總結
以上是生活随笔為你收集整理的java list过滤完赋给自己_Java8对list集合进行排序、过滤、分组、去重、转map、遍历赋值等操作...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 安卓怎么显示res文件夹中的html_a
- 下一篇: vins中imu融合_VINS-Mono