Discussion Stream data stream

When we encounter a large data file, how would you handle it?
We'll divided into many sections, each of which is processed in a different thread, then outputs the result.

Stream like an iterator (the Iterator), unidirectional, not reciprocating, data can only be traversed once.
Stream operations in parallel, only imperative iterators, the serialization operation. As the name suggests, when a serial mode to traverse, and then read each item to read the next item. When used to traverse the parallel data is divided into a plurality of segments, each of which is processed in a different thread, and then outputs the results together.
Collection Improvement
1.forEach () output support: default void forEach (Consumer <? Super T> Action)
2. Stream stream objects acquired: default Stream stream
code for:

 public static void main(String[] args) {
        List<String> list=new ArrayList<>();
        Collections.addAll(list,"Zhangsan","LisI","Wangwu","Like","zhangyi","want");
        list.forEach(s-> System.out.println(s));  //lambda表达式
        System.out.println("===>");
        list.forEach(System.out::println);  //方法引用
        System.out.println("===>");
       // System.out.println(list.size());
    System.out.println(list.stream().count());
    List<String> newlist=new ArrayList<>();
        for (String item : list) {
            if (item.contains("Li")) {    //foreach循环
                newlist.add(item);
            }
        }
        System.out.println(newlist);
        }

Stream Operation:
Here Insert Picture Description

1.count () method: the amount of data to make a statistical operation
2.filter () Method: Data filtration
3. wish to receive specific data after filtration, can be used to complete the collector.
Collector: public <R, A> R collect (Collector <? Super T, A, R> collector)
data has been collected still belongs to the set of List, a List can be received directly
focused on two methods of operation Stream interface :

  1. Remove the maximum content provided: public Stream limit (long maxSize);
  2. Skipped data amount: Skip public Stream (Long n);
    Stream supports the Skip (n) method returns a stream throw the first n elements. If the stream is less than the n-th element, an empty stream. limit (n) and the skip (n) are complementary
    Stream support map method, it will accept a function as a parameter. This function is applied to each element, and maps it into a new element.
    . List list = list.skip (0) .limit (2) .map (s> s.toUpperCase ()) collect (Collectors.toList ());
    code for:
import com.sun.org.apache.xpath.internal.SourceTree;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collector;
import java.util.stream.Collectors;

public class TestStream {
    public static void main(String[] args) {
        List<String> list=new ArrayList<>();
        Collections.addAll(list,"Zhangsan","LisI","Wangwu","Like","zhangyi","want");
        list.forEach(s-> System.out.println(s));
        System.out.println("===>");
        list.forEach(System.out::println);  //方法引用
        System.out.println("===>");
       // System.out.println(list.size());
    System.out.println(list.stream().count());
    List<String> newlist=new ArrayList<>();
        for (String item : list) {
            if (item.contains("Li")) {    //foreach循环
                newlist.add(item);
            }
        }
        System.out.println(newlist);
        System.out.println(list.stream().filter(s->s.contains("Li")).count());  //过滤出包含li的然后计算
//将字符串转换为小写,然后过滤出含有l的并且长度大于2 的字符串
        System.out.println(list.stream().map(String::toLowerCase).filter(s -> s.contains("l")).filter(s -> s.length() > 2).collect(Collectors.toList()));
        //跳跃两个值,取跳跃后的三个,并且包含i的
        System.out.println(list.stream().skip(2).limit(3).filter(s->s.contains("i")).collect(Collectors.toList()));
    }
}

Run Results:
Here Insert Picture Description
parallelstream () method: multicore, performed in parallel, improving the speed of multithreaded.
Single core cpu environment, not recommended parallel stream, the cpu multicore large amount of data and there is a condition, recommended parallestream

MapReduce model:
MapReduce is the core of the whole Stream. MapReduce operation is composed of two stages:

  1. map (): refers to the pre-operation processing performed for the data. For example: simple mathematical operations, etc.
  2. reduce (): Statistical analysis of the data for
    statistical analysis: public DoubleStream mapToDouble (ToDoubleFunction <super T?> mapper);
    in this case the method returns a DoubleStream interface object, there is statistical operation can be completed, the statistical method used as follows:
    statistical methods: DoubleStream mapToDouble (ToDoubleFunction mapper <super T?>);
    Example: statistics operation:
    code for:
package Price;

public class Order {
        private String title;
        private double price;
        private int amount;

        public Order(String title, double price, int amount) {
            this.title = title;
            this.price = price;
            this.amount = amount;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public double getPrice() {
            return price;
        }

        public void setPrice(double price) {
            this.price = price;
        }

        public int getAmount() {
            return amount;
        }

        public void setAmount(int amount) {
            this.amount = amount;
        }
    }



package Price;

import java.util.ArrayList;
import java.util.DoubleSummaryStatistics;
import java.util.List;

public class MapReduce {
    public static void main(String[] args) {
        List<Order> oderlist = new ArrayList<>();
        oderlist.add(new Order("Iphone", 8999.99, 10));
        oderlist.add(new Order("外星人笔记本", 12999.99, 5));
        oderlist.add(new Order("MacBookPro", 18999.99, 5));
        oderlist.add(new Order("Java从入门到放弃.txt", 9.99, 20000));
        oderlist.add(new Order("中性笔", 1.99, 200000));
         double totalPrice=0.0D;
         for(Order od:oderlist){
             totalPrice+= od.getPrice()*od.getAmount();
         }
        System.out.println("总钱数为:"+totalPrice);
        System.out.println("===>方法");
        Double totalPrice1=oderlist.stream().map(order -> order.getAmount() * order.getPrice()).reduce((sum, x) -> sum + x).get();      //lambda表达式
        Double totalPrice2 = oderlist.stream().map(order -> order.getAmount() * order.getPrice()).reduce(Double::sum).orElseGet(()->0.0D);//方法引用
        Double totalPrice3 = oderlist.stream().mapToDouble(order -> order.getAmount() * order.getPrice()).reduce(Double::sum).orElseGet(()->0.0D);
        System.out.println(totalPrice1);
        System.out.println(totalPrice2);
        System.out.println(totalPrice3);
        System.out.println("====>");
        DoubleSummaryStatistics statistics = oderlist.stream().mapToDouble(order -> order.getPrice() * order.getAmount()).summaryStatistics();
        System.out.println("总订单数:" + statistics.getCount());
        System.out.println("总金额:" + statistics.getSum());
        System.out.println("最大金额:" + statistics.getMax());
        System.out.println("最小金额:" + statistics.getMin());
        System.out.println("平均金额:" + statistics.getAverage());
    }
}

stream characteristics
1. traversed only once:
the data stream from a data acquisition source, followed by the elements operate on the line, when the element through the pipeline, can no longer be manipulated, you can retrieve a new data source operating the data stream;
2. using an iterative manner inside:
Collection is processed, typically using iterator walker traversal, which is an external iteration;
and for the treatment stream, as long as the stated approach, the process by the stream object self-complete, which is an internal iterations, the iterative process for the large amount of data, to be more efficient than internal iterations external iteration.

Guess you like

Origin blog.csdn.net/weixin_42373873/article/details/90734870