package com.stream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class TestStream6 {
public static void main(String args[]){
List<Stu> students = new ArrayList<Stu>();
students.add(new Stu("Joy",80));
students.add(new Stu("Joy",90));
students.add(new Stu("Tony",80));
students.add(new Stu("Tony",82));
students.add(new Stu("Joy",85));
List<Stu> stus = students.stream().collect(Collectors.toList());
System.out.println(stus);
Map<String, List<Stu>> results = students.stream().collect(Collectors.groupingBy(Stu::getName));
System.out.println(results);
}
}
class Stu{
int score;
String name;
Stu(String name,int score){
this.name = name;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}
package com.stream;
import java.util.ArrayList;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class TestStream6 {
public static void main(String args[]){
List<Stu> students = new ArrayList<Stu>();
students.add(new Stu("Joy",80));
students.add(new Stu("Joy",90));
students.add(new Stu("Tony",80));
students.add(new Stu("Tony",82));
students.add(new Stu("Joy",85));
List<Stu> stus = students.stream().collect(Collectors.toList());
System.out.println(stus);
Map<String, Long> results = students.stream().collect(Collectors.groupingBy(Stu::getName,Collectors.counting()));
System.out.println(results);
Map<String, Integer> results2 = students.stream().collect(Collectors.groupingBy(Stu::getName,Collectors.summingInt(Stu::getScore)));
System.out.println(results2);
Map<String, IntSummaryStatistics> results3 = students.stream().collect(Collectors.groupingBy(Stu::getName,Collectors.summarizingInt(Stu::getScore)));
System.out.println(results3);
}
}
class Stu{
int score;
String name;
Stu(String name,int score){
this.name = name;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}
[com.stream.Stu@eed1f14, com.stream.Stu@7229724f, com.stream.Stu@4c873330, com.stream.Stu@119d7047, com.stream.Stu@776ec8df]
{Tony=2, Joy=3}
{Tony=162, Joy=255}
{Tony=IntSummaryStatistics{count=2, sum=162, min=80, average=81.000000, max=82}, Joy=IntSummaryStatistics{count=3, sum=255, min=80, average=85.000000, max=90}}
int total = students.stream().collect(Collectors.reducing(0,Stu::getScore,(i,j)->i+j));
package com.stream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class TestStream6 {
public static void main(String args[]){
List<Stu> students = new ArrayList<Stu>();
students.add(new Stu("a",75));
students.add(new Stu("b",78));
students.add(new Stu("c",85));
students.add(new Stu("d",86));
Map<String, List<Stu>> results = students.stream().collect(Collectors.groupingBy((stu) -> {
if(stu.getScore()>80){
return "big";
}else{
return "small";
}
}));
for(String key:results.keySet()){
List<Stu> s = (List<Stu>) results.get(key);
System.out.println(key+":"+s.stream().map(Stu::getScore).collect(Collectors.toList()));
}
}
}