您好,登錄后才能下訂單哦!
小編給大家分享一下怎么使用Java Stream API將List按自定義分組規(guī)則轉(zhuǎn)換成Map,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
測(cè)試數(shù)據(jù)是List里的4個(gè)員工對(duì)象實(shí)例:
根據(jù)員工所在的城市進(jìn)行分組:
結(jié)果分成了三組:
第一組的員工在上海:
第二組的員工在成都:
統(tǒng)計(jì)每組員工個(gè)數(shù):
把員工進(jìn)行分組,得分大于101分的在一組,小于等于101的在另一組:
分組結(jié)果:
package java8;import java.util.ArrayList;import java.util.List;import java.util.Map;import java.util.Map.Entry;import java.util.function.Consumer;import java.util.stream.Collectors;class Employee { private String city; private String name; private int score; public Employee(String name, String city, int score){ this.city = city; this.name = name; this.score = score; } public String getCity(){ System.out.println("city: " + this.city); return this.city; } public String getName() { return this.name; } public int getScore() { return this.score; } @Override public String toString() { return String.format("Employee: " + this.name + " city: " + this.city); }}class Person { private String name; private int age; Person(String name, int age) { this.name = name; this.age = age; } @Override public String toString() { return String.format("Person{name='%s', age=%d}", name, age); }}// Jerry 2016-01-15 20:51PM ? 多用于extends generic的type,接受所有Object的sub classpublic class StreamTest { private static void printMap(Map<? extends Object, ? extends Object> map) { for(Entry<? extends Object, ? extends Object> entry:map.entrySet()) { System.out.println("key = " + entry.getKey() + " , Value = " + entry.getValue()); } } public static void main(String[] args) { ArrayList<Employee> employees = new ArrayList<Employee>(); employees.add(new Employee("A", "Shanghai",100)); employees.add(new Employee("B", "Chengdu",101)); employees.add(new Employee("C", "Shenzhen",102)); employees.add(new Employee("D", "Chengdu",104)); // group by City Map<String, List<Employee>> employeesByCity = employees.stream().collect( Collectors.groupingBy(Employee::getCity)); // default void forEach(Consumer<? super T> action) { for(Map.Entry<String, List<Employee>> entry:employeesByCity.entrySet()) { System.out.println("key= " + entry.getKey() + " , Value = " + entry.getValue()); entry.getValue().forEach(System.out::println); } // 2016-01-15 20:37PM Consumer<Employee> aa = a -> { System.out.println("Employee: " + a.getName() + " : " + a.getScore()); }; List<Employee> chengduEmployee = employeesByCity.get("Chengdu"); chengduEmployee.forEach(aa); // test for counting Map<String, Long> employeesByCity2 = employees.stream().collect( Collectors.groupingBy(Employee::getCity, Collectors.counting())); printMap(employeesByCity2); // calculate average score Map<String, Double> employeesByCity3 = employees.stream().collect( Collectors.groupingBy(Employee::getCity, Collectors.averagingInt(Employee::getScore))); printMap(employeesByCity3); /*Stream<Person> people = Stream.of(new Person("Paul", 24), new Person("Mark", 30), new Person("Will", 28)); Map<Integer, List<String>> peopleByAge = people.collect(groupingBy(p -> p.age, mapping((Person p) -> p.name, toList()))); System.out.println(peopleByAge);*/ /* * 分區(qū)是一種特殊的分組,結(jié)果 map 至少包含兩個(gè)不同的分組——一個(gè)true,一個(gè)false。 * 例如,如果想找出最優(yōu)秀的員工,你可以將所有雇員分為兩組,一組銷售量大于 N, * 另一組小于 N,使用 partitioningBy 收集器: */ System.out.println("partition result"); Map<Boolean, List<Employee>> partitioned = employees.stream().collect(Collectors.partitioningBy(e -> e.getScore() > 101)); printMap(partitioned); /* * 你也可以將 groupingBy 收集器傳遞給 partitioningBy 收集器來(lái)將聯(lián)合使用分區(qū)和分組。例如,你可以統(tǒng)計(jì)每個(gè)分區(qū)中的每個(gè)城市的雇員人數(shù): Map<Boolean, Map<String, Long>> result = employees.stream().collect(partitioningBy(e -> e.getNumSales() > 150, groupingBy(Employee::getCity, counting()))); 這樣會(huì)生成一個(gè)二級(jí) Map: {false={London=1}, true={New York=1, Hong Kong=1, London=1}} */ }}
以上是“怎么使用Java Stream API將List按自定義分組規(guī)則轉(zhuǎn)換成Map”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。