java8 新特性3 Stream的操作案例2
Posted 健康平安的活着
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java8 新特性3 Stream的操作案例2相关的知识,希望对你有一定的参考价值。
一 案例
数据:
public class StudentDb
public static List<Student> stuList=new ArrayList<>();
static
stuList.add(new Student("beijing",23,90.34,"2021-10-04"));
stuList.add(new Student("tianjin",34,30.34,"2021-09-04"));
stuList.add(new Student("shanghai",56,45.24,"2022-10-04"));
stuList.add(new Student("tianjin",89,70.89,"2021-06-03"));
stuList.add(new Student("shenzhen",95,95.89,"2022-10-03"));
stuList.add(new Student(45,89.89,"2022-07-03"));
1.1 查询出生为2021年,且按分数进行排序
List<Student> studentList=StudentDb.stuList;
studentList.stream()
.filter( (e)-> return e.getBirthday().split("-")[0].equals("2022");)
.sorted((t1,t2)-> Double.compare(t1.getScore(),t2.getScore())).
forEach((x)->System.out.println(x););
System.out.println("==========");
1.2 求出有多少个不同的学生名字
studentList.stream().map((e)->return e.getName();).distinct().forEach((e)->System.out.println("e:"+e););
1.3 将姓名进行拼接出一个字符串
System.out.println("===============");
String s= studentList.stream().map((x)->return x.getName()==null?"":x.getName();).reduce(",",String::concat);
System.out.println("s:"+s);
1.4 查询姓名是否包含shenzhen
boolean f=studentList.stream().anyMatch((e)->return e.getName().equals("shenzhen"););
System.out.println("f:"+f);
1.5 获取学生的总分数和
System.out.println("===============");
Optional<Double> total= studentList.stream().map((x)->return x.getScore();).reduce(Double::sum);
System.out.println("total:"+ total.get());
1.6 求5大聚合函数
Optional<Double> max= studentList.stream().map((e)->return e.getScore();).max(Double::compare);
System.out.println("max:"+ max.get());
Optional<Double> min= studentList.stream().map((e)->return e.getScore();).min(Double::compare);
Optional<Double> min2= studentList.stream().map((e)->return e.getScore();).min((x1,x2)->return Double.compare(x1,x2););
System.out.println("min:"+ min.get()+" min2:"+min2.get());
long count= studentList.stream().map((e)->return e.getScore();).count();
System.out.println("count:"+count);
//7.5大聚合函数
DoubleSummaryStatistics lss = studentList.stream().collect(Collectors.summarizingDouble((x)->return x.getScore();));
System.out.println("avg:"+lss.getAverage()+" max:"+lss.getMax()+" min:"+lss.getMin()+" sum:"+lss.getSum()+" count:"+lss.getCount());
Optional<Double> op = studentList.stream().map(Student::getScore).reduce(Double::sum);
System.out.println("op:"+op.get());
1.7 获取元素不为null个数据
Instant start=Instant.now();
Long gshu= studentList.stream().filter((x)->return x.getName()!=null;).count(); ;
System.out.println("geshu:"+gshu);
//9.统计个数
Optional<Integer> countx =studentList.stream().map((e) -> 1).reduce(Integer::sum);
System.out.println(countx.get());
Instant end=Instant.now();
System.out.println("耗时:"+ Duration.between(start,end).toMillis());
1.8 将旧集合中某个属性放到新集合中
List<String> nameList=new ArrayList<>();
studentList.stream().forEach((e)->nameList.add(e.getName()););
System.out.println("namelist:"+nameList);
List<String> list = studentList.stream().map(Student::getName).collect(Collectors.toList());
list.forEach(System.out::println);
System.out.println("----------------------------------");
1.9 将list属性放到map集合中
Map<String,Object> rMap=studentList.stream().collect(Collectors.toMap(x->x.getName(),x->x.getScore(), (entity1, entity2)->
return entity2;
));
rMap.forEach((k,v)->System.out.println("k:"+k+" v:"+v););
1.10 使用reduce求sum
Optional<Double> op = studentList.stream().map(Student::getScore).reduce(Double::sum);
System.out.println("op:"+op.get());
1.11 将map的key,value进行分别存储
//13.将map的key,value进行分别存储
Map<String,Integer> map = new HashMap<String, Integer>();
map.put("ss",34);
map.put("rt",56);
List<String> keyList=new ArrayList<>();
List<Integer> valueList=new ArrayList<>();
map.keySet().stream().forEach((x)-> keyList.add(x););
map.values().stream().forEach((x)-> valueList.add(x););
System.out.println("kk:"+keyList+" vv:"+valueList);
以上是关于java8 新特性3 Stream的操作案例2的主要内容,如果未能解决你的问题,请参考以下文章