Stream基本操作:

Collection中提供了此接口的实例化方法:default Stream<E> stream()
Stream的主要功能是对数据进行分析处理,尤其是集合中数据的分析操作。

代码示例:

显示元素个数:

  1. public class Demo2 {
  2. public static void main(String[] args) {
  3. List<String> list = new ArrayList<>();
  4. Collections.addAll(list, "Hello", "Word", "Happy", "Hi", "Has");
  5. Stream<String> stream = list.stream();
  6. //元素个数
  7. System.out.println(stream.count());
  8. }
  9. }

outPut:
5

filter:

Stream<T> filter(Predicate<? super T> predicate);
采用了断言式的函数式接口

  1. public class Demo2 {
  2. public static void main(String[] args) {
  3. List<String> list = new ArrayList<>();
  4. Collections.addAll(list, "Hello", "Word", "Happy", "Hi", "Has");
  5. Stream<String> stream = list.stream();
  6. //有几个是含有字母H的
  7. System.out.println(stream.filter((ele) -> ele.toUpperCase().contains("H")).count());
  8. }
  9. }

outPut:
4

数据采集:

  1. public class Demo2 {
  2. public static void main(String[] args) {
  3. List<String> list = new ArrayList<>();
  4. Collections.addAll(list, "Hello", "Word", "Happy", "Hi", "Has");
  5. Stream<String> stream = list.stream();
  6. //将满足条件的数据收集变为List集合
  7. List<String> list1=stream.filter((ele) -> ele.toUpperCase().contains("H")).collect(Collectors.toList());
  8. System.out.println(list1);
  9. }
  10. }

outPut:
[Hello, Happy, Hi, Has]

分页处理:

  1. public class Demo2 {
  2. public static void main(String[] args) {
  3. List<String> list = new ArrayList<>();
  4. Collections.addAll(list, "Hello", "Word", "Happy", "Hi", "Has");
  5. Stream<String> stream = list.stream();
  6. //将满足条件的数据收集变为List集合
  7. //限制最多3个,跳过一个
  8. List<String> list1=stream.filter((ele) -> ele.toUpperCase().contains("H")).limit(3).skip(1).collect(Collectors.toList());
  9. System.out.println(list1);
  10. }
  11. }

MapReduce:

分为两部分,Map处理部分,Reduce分析部分。

代码结构大致如下:

  1. java.util.stream.Stream;
  2. class People{
  3. String name;
  4. int price;
  5. People(String name,int price){
  6. this.name=name;
  7. this.price=price;
  8. }
  9. public void setName(String name) {
  10. this.name = name;
  11. }
  12. public void setPrice(int price) {
  13. this.price = price;
  14. }
  15. public String getName() {
  16. return name;
  17. }
  18. public int getPrice() {
  19. return price;
  20. }
  21. }
  22. public class Demo2 {
  23. public static void main(String[] args) {
  24. List<People> list = new ArrayList<>();
  25. list.add(new People("张二三",10000));
  26. list.add(new People("李二四",20000));
  27. list.add(new People("王二五",30000));
  28. list.add(new People("赵二六",40000));
  29. list.add(new People("王二麻子",50000));
  30. LongSummaryStatistics longSummaryStatistics = list.stream().filter((ele) -> ele.getName().contains("二")).mapToLong((price) -> price.getPrice() * price.getPrice()).summaryStatistics();
  31. System.out.println(longSummaryStatistics.getMax());
  32. System.out.println(longSummaryStatistics.getAverage());
  33. System.out.println(longSummaryStatistics.getCount());
  34. System.out.println(longSummaryStatistics.getMin());
  35. System.out.println(longSummaryStatistics.getSum());
  36. }
  37. }

这些大数据只是JDK本身提供的支持。