赞
踩
一个 Stream 只可以使用一次;
stream has already been operated upon or closed,只能使用一次
功能:
(1)分页与基础操作;
List<String> queue = new LinkedList<>();
Collections.addAll(queue,"1","2","gy","hr","PIGE","ge","goole");
//获得Stream接口对象
Stream<String> stream = queue.stream();
//输出元素个数
//stream has already been operated upon or closed,只能使用一次
//System.out.println(stream.count());
//1、内容全部转小写,筛选是否包含字母g的集合
//List<String> list = stream.filter(e -> e.toLowerCase(Locale.ROOT).contains("g")).collect(Collectors.toList());
//2、分页:跳过两个skip(2),取一个limit(1);
List<String> list = stream.filter(e -> e.toLowerCase(Locale.ROOT).contains("g")).skip(2).limit(1).collect(Collectors.toList());
System.out.println(list); //[ge]
(2)筛选
List<Student> students = studentList.stream().filter(s -> (s.getSex() == 0 && s.getClassNo() == 2)).collect(Collectors.toList());
(3)排序
List<Student> students = studentList.stream().sorted(Comparator.comparing(Student::getAge)).collect(Collectors.toList());
List<Student> students = studentList.stream().sorted(Comparator.comparing(Student::getAge).thenComparing(Student::getClassNo)).collect(Collectors.toList());
List<Student> students = studentList.stream().sorted(Comparator.comparing(Student::getAge).reversed()).collect(Collectors.toList());
(4)分组
//按照班级进行分组
Map<Integer, List<Student>> classMap = studentList.stream().collect(Collectors.groupingBy(Student::getClassNo));
结果是map:
key: 1 value:[{classNo:1,name:"张三",age:12},{classNo:1,name:"李四",age:13}]
key: 2 value:[{classNo:2,name:"王五",age:12},{classNo:1,name:"贼六",age:11}]
key: 3 value:[{classNo:3,name:"对七",age:12}]
(5) 求和
BigDecimal score= studentList.setScore(studentList.stream().map(studentList::getScore).reduce(BigDecimal.ZERO, BigDecimal::add));
(6) 去重
// 使用Stream的distinct()方法进行去重操作
List<String> names = Arrays.asList("张三", "李四", "王五", "张三", "李四");
List<String> distinctNames = names.stream().distinct().collect(Collectors.toList());
List<User> collect = userList.stream().collect(Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(
Comparator.comparing(p -> p.getOrgName() + "," + p.getUserName()))), ArrayList::new));
MapReduce是一种计算模型,它将大型数据操作作业分解为可以跨服务器集群并行执行的单个任务。每个MapReduce工作由两个阶段组成:Map;Reduce。
Map处理数据
Reduce分析数据
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。