stream式编程入门
实体类
public class Apple {
private int id;
private String color;
private int weight;
private String origin;
public Apple(int id, String color, int weight, String origin) {
this.id = id;
this.color = color;
this.weight = weight;
this.origin = origin;
}
@Override
public String toString() {
return "Apple{" +
"id=" + id +
", color='" + color + '\'' +
", weight=" + weight +
", origin='" + origin + '\'' +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public String getOrigin() {
return origin;
}
public void setOrigin(String origin) {
this.origin = origin;
}
}
Lambda表达式
public class SimpleLambda {
public static void main(String[] args) {
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("hello");
}
}).start();
new Thread(()-> System.out.println("hello world")).start();
/**
* 参数特性:
* 可以忽略 类型
* 参数只有一个情况 ,可以省略括号
* 代码编写特性:
* 1.单行表达式 省略掉return
* 2.代码块加return
* 3.方法引用
*/
start((name,age) -> name+age);
start(((name, age) -> {
return "name"+name;
}));
//静态
start(SimpleLambda::doFormat);
//普通
start(new SimpleLambda()::doFormat2);
//前置条件,必须是函数式接口
//参数的传递
//代码便携方式
}
public static String doFormat(String param2,int age){
return "name"+param2;
}
public String doFormat2(String param2,int age){
return "name"+param2;
}
public static void start(myRun2 myRun2){
// new Thread(myRun2).start();
myRun2.run("w",22);
}
//1.只能一个方法--》函数式接口
//2.默认方法除外
//3.Object 下面的方法除外
@FunctionalInterface
public interface myRun2 /*extends Runnable*/{
String run(String name,Integer age);
String toString();
boolean equals(Object obj);
}
}
stream
import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class AppleStore {
private static List<Apple> appleStore=new ArrayList<>();
static {
appleStore.add(new Apple(1,"red",500,"湖南"));
appleStore.add(new Apple(2,"red",400,"湖南"));
appleStore.add(new Apple(3,"green",300,"湖南"));
appleStore.add(new Apple(4,"green",200,"天津"));
appleStore.add(new Apple(5,"green",100,"天津"));
}
//找出红色的苹果
//重量
//产地
public void testq(){
for (Apple apple : appleStore) {
if (apple.getColor().equals("red")) {
//add
}
}
}
//过滤一个颜色
public void test2(){
List<Apple> red = appleStore.stream()
.filter(a -> a.getColor().equals("red"))
.filter(a->a.getWeight()>300)
.collect(Collectors.toList());
}
//过滤一个颜色
public void test3(Predicate<? super Apple> pr){
List<Apple> red = appleStore.stream()
.filter(pr)
.collect(Collectors.toList());
System.out.println(red);
}
//求出每个颜色的平均重量
public void test4(){
// 1.基于颜色分组
Map<String ,List<Apple>> maps=new HashMap<>();
for (Apple apple : appleStore) {
//如果list不存在就创建一个
List<Apple> apples = maps.computeIfAbsent(apple.getColor(), key -> new ArrayList<Apple>());
apples.add(apple);
}
}
public void test5(){
appleStore.stream()
.collect(Collectors.groupingBy(a -> a.getColor(), //基于颜色分组
Collectors.averagingInt(a -> a.getWeight()))) //统计平均重量
.forEach((key,value)-> System.out.println(key+":"+value));
}
public static void main(String[] args) {
new AppleStore().test3(a->a.getColor().equals("red")&&a.getWeight()>300);
new AppleStore().test5();
/*
* 流的生成 与不可重复使用
* Arrays.stream(new int[]{1,2,3,4});
Stream.of(1,2,3,4,5);
*
*/
Stream<Apple> stream1 = appleStore.stream();
Stream<Apple> stream2 = stream1.filter(a -> a.getColor().equals("red"));
//Stream<Apple> stream3 = stream1.filter(a -> a.getWeight()>100); //报错流使用过就会close
//中间节点-》懒节点
appleStore.stream()
//中间节点
.filter(a->{
//过滤
return a.getColor().equals("red")||a.getColor().equals("green");
})
//流是一个一个执行的
// .peek(a-> System.out.println(a.getColor()))
// .peek(a-> System.out.println(a.getWeight()))
/**
* 影响方式:
* 1。过滤
* 2。转换
*/
//转换-》String
.map(a->a.getColor())
//去重
.distinct()
.peek(a-> System.out.println(a))
//终值节点
.toArray();
/**
* 采集
* 1.list
* 2.map
* 3.group by
* 4.数组
* 5.求出最大值
* 6.求任意值
*/
//基于颜色分组
Map<Integer, Apple> collect = appleStore.stream().collect(Collectors.toMap(a -> a.getId(), a -> a));
collect.entrySet().forEach(k->{
System.out.println(k);
});
}
}