日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java8 stream index_Java8的stream用法整理

發布時間:2025/3/8 java 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java8 stream index_Java8的stream用法整理 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

/***@authorindex

* @date 2020/10/27

**/

public classTestcollectingAndThen {

@Testpublic voidtest(){final int NUM = 14;

List peopleList = new ArrayList<>(NUM);

String[] names= {"小張", "小龍", "小牛", "小豬", "小黑", "小紅", "小白"};for (int i = 0; i < 5; i++) {//添加5個19歲的隨機性別和名字的小朋友

peopleList.add(new People(19, (int) (Math.random() * 2), names[(int) (Math.random() *names.length)]));

}for (int i = 5; i < 8; i++) {//添加3個31歲的隨機性別和名字的小朋友

peopleList.add(new People(31, (int) (Math.random() * 2), names[(int) (Math.random() *names.length)]));

}for (int i = 8; i < NUM; i++) {//添加6個22歲的隨機性別和名字的小朋友

peopleList.add(new People(22, (int) (Math.random() * 2), names[(int) (Math.random() *names.length)]));

}//collectingAndThen先對stream里的元素進行collecting,之后再對結果進行操作,//下面的結果是一個map,對map計算元素數目

System.out.println("分組數目:");

Integer groupCount=peopleList.stream().collect(

Collectors.collectingAndThen(Collectors.groupingBy(People::getName), Map::size));

System.out.println(groupCount);

System.out.println("-------------------------------------");//按照名字分組

System.out.println("按照名字分組");

System.out.println(

peopleList.stream().collect(Collectors.groupingBy(People::getName))

);

System.out.println("-------------------------------------");//按照名字分組(分組的結果是一個map),并統計每一個分組(map中的每一個value)中的元素數目

System.out.println("統計每一個分組(map中的每一個value)中的元素數目");

System.out.println(

peopleList.stream().collect(Collectors.groupingBy(People::getName, Collectors.counting()))

);

System.out.println("-------------------------------------");//按照名字分組(分組的結果是一個map),并取出每一組的最大值

System.out.println("取出每一組的最大值");

System.out.println(

peopleList.stream().collect(Collectors.groupingBy(People::getName, Collectors.maxBy(new Comparator() {

@Overridepublic intcompare(People o1, People o2) {return o1.getAge() -o2.getAge();

}

})))

);

}

}

總結

以上是生活随笔為你收集整理的java8 stream index_Java8的stream用法整理的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。