Stream flow data

.Stream a data stream
  from JDK1.8 beginning, because they have entered into the era of large data, which it also supports the cluster analysis of the data processing operation (processing operation of flow cytometry data), for which it provides a dedicated stream interface, while the interface Collection which is also provided with an interface for this example of the method:
  parallel flow analysis: default stream <E> parallelStream ( ) to participate in a plurality of threads analysis
  single thread analysis: default stream <E> stream ( )
basic operation --Stream interface
  primary function is to analyze the Stream processing data while operating primarily for analysis of the data in the collection.

 1 public class MyCollectionsDemo {
 2     public static void main(String[] args) {
 3         List<String> all = new ArrayList<>();
 4         //追加元素
 5         Collections.addAll(all, "python", "c++", "java", "Go", "Ruby");
 6         Stream<String> stream = all.stream();       //实例化Stream对象
 7 //        System.out.println(stream.count());     10e.g. with elements of the collection needs to acquire the letter G
//9streaming its greatest advantage is achieved as a function of programming data
//8the number of the output element
//                           // Each transition a lowercase letters, and determines whether the presence of the letter 
. 11          Long COUNT = stream.filter - COUNT () ((ELE)> ele.toLowerCase () the contains ( "J").);. // type assertion function interface 
12 is          System.out.println (COUNT);
 13 is      }
 14 }

- but above program is to achieve a number of some of the most basic statistical data, and acquire data content to meet the conditions of the time inside more often, so then you can realize the collection of operational data:
- Example: data collection

. 1  public  class MyCollectionsDemo {
 2      public  static  void main (String [] args) {
 . 3          List <String> = All new new the ArrayList <> ();
 . 4          // additional element 
. 5          Collections.addAll (All, "Python", "C ++" , "Java", "Go", "the Ruby" );
 . 6          Stream <String> Stream all.stream = ();        // instantiate objects Stream
 7          // the data satisfies the conditions set collected into List 
. 8          List < String> list = stream.filter ((ele ) -> ele.toLowerCase () contains ( "j").) collect (Collectors.toList ());.// assert type of function interface 
9         System.out.println(list);
10     }
11 }

- Stream data stream processing in the process, also allows the processing of data pages, there are provided two methods:
  the maximum amount of data extracted is provided: Stream <T> limit (long maxSize)
  to skip a specified amount data: Stream < T> skip (long n)

. 1  public  class MyCollectionsDemo {
 2      public  static  void main (String [] args) {
 . 3          List <String> = All new new the ArrayList <> ();
 . 4          // additional element 
. 5          Collections.addAll (All, "Python", "C ++" , "Java", "Go", "the Ruby", "JSP", "JSON", "HTML", "CSS" );
 . 6          Stream <String> Stream all.stream = ();        // instantiate Stream object
 7          // the data satisfies the conditions set collected into List 
. 8          List <String> stream.filter List = ((ELE) -> ele.toLowerCase ().contains("j")).skip(1).limit(2).collect(Collectors.toList());//Type assertion function interface 
. 9          System.out.println (List);
 10      }
 . 11 }

--operation result

[jsp, json]

Process finished with exit code 0

- at which time the first data collection escaped to the "java", the output obtained after two data .Stream operation mainly using its own characteristics to achieve the data analysis operation.

Two .MapReduce base model
  among the processing performed data analysis, one of the most important basic model that MapReduce model, for this model, a total is divided into two parts, one is the Map processing section, the second is Reduce analysis section. before analyzing the data, the data must be a reasonable treatment, but before they can do statistical analysis operations.

 1 package 类集合框架.集合工具类;
 2 
 3 import java.util.ArrayList;
 4 import java.util.DoubleSummaryStatistics;
 5 import java.util.List;
 6 import java.util.stream.Stream;
 7 
 8 /**
 9  * @author : S K Y
10  * @version :0.0.1
11  */
12 class Order{    //订单信息
13     private String name;    //购买的商品的名称
14     private double price;   //商品的单价
15     private int amount;     //商品的数量
16 
17     public Order(String name, double price, int amount) {
18         this.name = name;
19         this.price = price;
20         this.amount = amount;
21     }
22 
23     public String getName() {
24         return name;
25     }
26 
27     public double getPrice() {
28         return price;
29     }
30 
31     public int getAmount() {
32         return amount;
33     }
34 }
35 public class MyMapReduceDemo {
36     public static void main(String[] args) {
37         //如果要想使用Stream进行分析处理,则一定要将全部分析的数据保存在集合之中
38         List<Order> all = new ArrayList<>();
39         all.add(new Order("小娃娃",9.9,10));
40         all.add(new Order("电动变形金刚",999.9,2));
41         all.add(new Order("笔记本电脑",8999.9,4));
42         all.add(new Order("茶杯",2.9,200));
43         all.add(new Order("电动茶具",112.9,100));
44         //统计分析够爱商品之中带有电的产品,并且进行单价和数量的处理随后分析汇总
45         DoubleSummaryStatistics statistics = all.stream().filter(order -> order.getName().contains("电"))
46                 .mapToDouble(value -> value.getPrice() * value.getAmount()).summaryStatistics();
47         System.out.println("商品购买种类数量: " + statistics.getCount());
48         System.out.println("商品购买总价: " + statistics.getSum());
49         System.out.println("商品平均花费: " + statistics.getAverage());
50         System.out.println("商品最高花费: " + statistics.getMax());
51         System.out.println("商品最低花费: " + statistics.getMin());
52     }
53 }

--运行结果

商品购买种类数量: 3
商品购买总价: 49289.4
商品平均花费: 16429.8
商品最高花费: 35999.6
商品最低花费: 1999.8

Process finished with exit code 0

--这些分析操作只是JDK本身提供的支持,而实际之中肯定不可能这样进行,因为所有的数据如果都保存在内存之中,那么面对与大数据的环境,系统无法承载...

Guess you like

Origin www.cnblogs.com/skykuqi/p/11449478.html