除了使用Iterator进行迭代输出以外,
JDK1.8提供了一个专门可以进行数据处理的类:java.unit.Stream;
Stream意味着将Collection中所有数据交给Stream处理
<R> R collect(
Supplier
<R> supplier,
BiConsumer
<R,? super
T
> accumulator,
BiConsumer
<R,R> combiner)//收集器,收集去掉的部分
package com.Stream;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class StreamTest {
public static void main(String[] args) {
List<String>all = new ArrayList<String>();
all.add("hello");
all.add("hello");
all.add("world");
all.add("world");
all.add("Good");
Stream<String>stream = all.stream();//取得Stream类的对象
// System.out.println(stream.count());
// System.out.println(stream.distinct().count());
//去除掉所有重复数据后形成的新的数据集合,里面是不包含重复内容的集合
List<String>newAll = stream.distinct().collect(Collectors.toList());
newAll.forEach(System.out :: println);
}
}
数据的过滤操作:
List<String> newAll = stream.distinct().filter((x)->x.contains("a")).collect(Collectors.toList());
此时是区分大小写的,所以需要对数据进行额外的处理(此时map的功能是针对一行数据进而处理每行处理):
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class StreamTest {
public static void main(String[] args) {
List<String>all = new ArrayList<String>();
all.add("a1");
all.add("A1");
all.add("b");
all.add("c");
all.add("a");
Stream<String> stream = all.stream();
List<String> newAll = stream.map((x)->x.toLowerCase()).distinct().filter((x)->x.contains("a")).collect(Collectors.toList());
newAll.forEach(System.out::println);
}
}
在Stream接口里面提供了进行集合分页的操作:
package com.Stream;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class StreamTest {
public static void main(String[] args) {
List<String>all = new ArrayList<String>();
all.add("a1");
all.add("A1");
all.add("b");
all.add("c");
all.add("a");
Stream<String> stream = all.stream();
List<String> newAll = stream.map((x)->x.toLowerCase()).skip(2).limit(2).collect(Collectors.toList());
newAll.forEach(System.out::println);
}
}
在Stream中还可以进行数据的全部匹配和部分匹配
断言型接口包含:
package com.Stream;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class StreamTest {
public static void main(String[] args) {
List<String>all = new ArrayList<String>();
all.add("a1");
all.add("A1");
all.add("b");
all.add("c2");
all.add("a");
Stream<String> stream = all.stream();
Predicate<String> p1 = (x)->x.contains("a");
Predicate<String> p2 = (x)->x.contains("1");
if( stream.anyMatch( p1.and(p2)))System.out.println("1");
else System.out.println("0");
}
}
利用这样的匹配条件,可以对数据进行方便的查询操作。
如果要想更好的发挥出Stream的优势,跟MapReduce一起。
数据分析方法:
就是做数据统计使用的;
package com.Stream;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
class shopCar{
private String pname;
private double price;
private int amount;
public shopCar(String pname,double price , int amount) {
// TODO Auto-generated constructor stub
this.pname = pname;
this.price = price;
this.amount = amount;
}
public int getAmount() {
return amount;
}
public double getPrice() {
return price;
}
public String getPname() {
return pname;
}
}
public class StreamTest {
public static void main(String[] args) {
List<shopCar> all = new ArrayList<shopCar>();
all.add(new shopCar("A",10.0,3));
all.add(new shopCar("B",20.0,4));
all.add(new shopCar("C",30.0,5));
double s = all.stream().map( (x)->x.getAmount()*x.getPrice() ).reduce((sum,m)->sum+m).get();
System.out.println(s);
}
}
以上实现了一个最简单的MapReduce , 如果要更完善,要使用Stream里面的方法;
package com.Stream;
import java.util.ArrayList;
import java.util.DoubleSummaryStatistics;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
class shopCar{
private String pname;
private double price;
private int amount;
public shopCar(String pname,double price , int amount) {
// TODO Auto-generated constructor stub
this.pname = pname;
this.price = price;
this.amount = amount;
}
public int getAmount() {
return amount;
}
public double getPrice() {
return price;
}
public String getPname() {
return pname;
}
}
public class StreamTest {
public static void main(String[] args) {
List<shopCar> all = new ArrayList<shopCar>();
all.add(new shopCar("A",10.0,3));
all.add(new shopCar("B",20.0,4));
all.add(new shopCar("C",30.0,5));
DoubleSummaryStatistics dss = all.stream().mapToDouble( ( sc )->sc.getAmount()*sc.getPrice()).summaryStatistics();
System.out.println(dss.getAverage());
}
}
总结:
以上的操作忘了吧,就像未出现过;
Mapreduce , Map处理数据,Reduce分析数据;