table of Contents
Stream
Stream data flow calculation, the calculation speed is very fast.
Parallel data streams is calculated as: public default Stream paralleStream (); (JDK 1.8)
Data flow is calculated as: public default Stream Stream (); (JDK 1.8)
Common methods:
method | Explanation |
---|---|
public long count() | Calculation of the number of data |
public Stream limit(long maxSize) | Set the number of set up can be placed |
public Stream skip(long n) | Set the number of skipped |
Example:
public class TestStream {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
Collections.addAll(list, "Java", "JavaScript", "C", "R", "C#");
//将数据转成数据流
Stream<String> stream = list.stream();
/*
获取集合中带有J的数据的个数
int number = (int) stream.filter((element) -> element.toLowerCase().contains("j")).count();
System.out.println(number);
结果:2
*/
/*
获取带J的数据重新组装成一个集合
List<String> subList = stream.filter((element) -> element.toLowerCase().contains("j")).collect(Collectors.toList());
System.out.println(subList.toString());
结果:[Java,JavaScript]
*/
/*
skip(跳过多少数据)
limit(集合最多放多少数据)
*/
//获取加了skip()和limit()的数据集合
List<String> newList = stream.filter((element) ->element.toLowerCase().contains("j")).skip(1).limit(1).collect(Collectors.toList());
System.out.println(newList);
stream.close();
//结果:[Java]
}
}
MapReduce
- Map processing: various kinds of data pre-processing (calculation, transformation, etc.).
- Reduce processing: calculation of statistical data, statistical operation for the processed data content.
Example:
public class TestMapReduce {
public static void main(String[] args) {
List<Ball> list = new ArrayList<>();
Collections.addAll(list,
new Ball("篮球", 199.9, 20),
new Ball("足球", 109.9, 200),
new Ball("排球", 59.9, 300),
new Ball("蹴鞠", 789.9, 5));
Stream<Ball> stream = list.stream();
DoubleSummaryStatistics doubleSummaryStatistics = stream.filter((element) -> (element.getName())
.contains("球")).mapToDouble((obj) -> obj.getPrice() * obj.getAmount()).summaryStatistics();
System.out.println("购买数量:" + doubleSummaryStatistics.getCount());
System.out.println("总花销:" + doubleSummaryStatistics.getSum());
System.out.println("最高价格:" + doubleSummaryStatistics.getMax());
System.out.println("最低价格:" + doubleSummaryStatistics.getMin());
System.out.println("平均价格:" + doubleSummaryStatistics.getAverage());
}
}
class Ball {
private String name;
private double price;
private double amount;
public Ball(String name, double price, double amount) {
this.name = name;
this.price = price;
this.amount = amount;
}
public double getPrice() {
return price;
}
public String getName() {
return name;
}
public double getAmount() {
return amount;
}
@Override
public String toString() {
return "Ball{" +
"name='" + name + '\'' +
", price=" + price +
", amount=" + amount +
'}';
}
}
result:
购买数量:3
总花销:43948.0
最高价格:21980.0
最低价格:3998.0
平均价格:14649.333333333334