package pers.lan.jc.e;
import org.junit.Test;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Predicate;
import java.util.stream.Collectors;
/**
* @author lan [1728209643@qq.com]
* @create 2018-12-02 11:08
* @desc java lambda和函数式编程
*/
public class Java8 {
private List<Apple> apples = Arrays.asList(
Apple.builder().id(0).color("红色").unitPrice(2).weight(3).build(),
Apple.builder().id(1).color("红色").unitPrice(2).weight(4).build(),
Apple.builder().id(2).color("绿色").unitPrice(1).weight(2).build(),
Apple.builder().id(3).color("绿色").unitPrice(1).weight(3).build(),
Apple.builder().id(4).color("青色").unitPrice(1).weight(4).build(),
Apple.builder().id(5).color("青色").unitPrice(1).weight(5).build(),
Apple.builder().id(6).color("黑色").unitPrice(3).weight(5).build(),
Apple.builder().id(7).color("黑色").unitPrice(3).weight(4).build()
);
private Predicate<Apple> redApplePredicate = apple -> "红色".equals(apple.getColor());
private Predicate<Apple> predicate(String color) {
return (apple -> color.equals(apple.getColor()));
}
/**
* @desc 获取某种颜色的苹果
* @author lan [1728209643@qq.com]
* @create 2018/12/2 11:23
*/
@Test
public void testFilter_Predicate() {
List<Apple> appleList;
appleList = apples.stream().filter(redApplePredicate).collect(Collectors.toList());
System.out.println(appleList);
appleList = apples.stream().filter(predicate("绿色")).collect(Collectors.toList());
System.out.println(appleList);
appleList = apples.stream().filter(predicate("青色")).collect(Collectors.toList());
System.out.println(appleList);
appleList = apples.stream().filter(apple -> "红色".equals(apple.getColor())).collect(Collectors.toList());
System.out.println(appleList);
}
/**
* @desc 匹配 anyMatch/allMatch
* @author lan [1728209643@qq.com]
* @create 2018/12/2 12:33
*/
@Test
public void testMatch() {
//是否有红色的
boolean match = apples.stream().anyMatch(predicate("红色"));
System.out.println(match);
//是不是所有的都是红色
match = apples.stream().allMatch(predicate("红色"));
System.out.println(match);
}
/**
* @desc 查找 findAny/findFirst
* @author lan [1728209643@qq.com]
* @create 2018/12/2 12:37
*/
@Test
public void testFind() {
//第一个
Optional<Apple> first = apples.stream().findFirst();
System.out.println(first.get());
//任何一个
Optional<Apple> any = apples.stream().findAny();
System.out.println(any.get());
//查找重量大于4的第一个
first = apples.stream().filter(apple -> apple.getWeight() > 4).findFirst();
System.out.println(first.get());
}
/**
* @desc 计算苹果的价格forEach() -> Apple.setTotalPrice()
* @author lan [1728209643@qq.com]
* @create 2018/12/2 12:01
*/
@Test
public void testConsumer() {
apples.forEach(apple -> apple.setTotalPrice(apple.getUnitPrice() * apple.getWeight()));
System.out.println(apples);
}
/**
* @desc 把苹果变成有包装的苹果 Apple -> PackedApple, 可以用于将集合中的元素进行类型转换的操作
* @author lan [1728209643@qq.com]
* @create 2018/12/2 11:47
*/
@Test
public void testFunction() {
List<PackedApple> packedApples;
//把苹果变成有包装的苹果 Apple -> PackedApple
packedApples = apples.stream().map(
apple -> PackedApple.builder().apple(apple).packageBy("lan").packageType("礼盒包装").build()).collect(Collectors.toList());
System.out.println(packedApples);
packedApples = apples.stream().map(apple -> {
if ("红色".equals(apple.getColor())) {
return PackedApple.builder().apple(apple).packageBy("lan").packageType("礼盒包装").build();
} else if (predicate("青色").test(apple)) {
return PackedApple.builder().apple(apple).packageBy("小明").packageType("咬一口再包装").build();
} else {
return PackedApple.builder().apple(apple).packageBy("小强").packageType("简单包装").build();
}
}).collect(Collectors.toList());
System.out.println(packedApples);
}
/**
* @desc 有哪些颜色/有哪些价格
* @author lan [1728209643@qq.com]
* @create 2018/12/2 11:35
*/
@Test
public void testDistinct() {
//所有的颜色
List<String> colors = apples.stream().map(Apple::getColor).collect(Collectors.toList());
System.out.println(colors);
//有哪些颜色...
colors = apples.stream().map(Apple::getColor).distinct().collect(Collectors.toList());
System.out.println(colors);
//有哪些价格
List<Integer> unitPrices = apples.stream().map(Apple::getUnitPrice).distinct().collect(Collectors.toList());
System.out.println(unitPrices);
}
/**
* @desc 把颜色的每个字都分开
* @author lan [1728209643@qq.com]
* @create 2018/12/2 12:07
*/
@Test
public void testFlatMap() {
List<String> colors = apples.stream().map(apple -> apple.getColor().split("")).flatMap(Arrays::stream).collect(Collectors.toList());
System.out.println(colors);
List<String> list = Arrays.asList("Hello World!", "I'm Lan");
list = list.stream().map(s -> s.split("")).flatMap(Arrays::stream).collect(Collectors.toList());
System.out.println(list);
List<Integer[]> integers = Arrays.asList(
new Integer[]{0, 1, 5, 6},
new Integer[]{1, 2, 3, 5}
);
List<Integer> integerList = integers.stream().flatMap(Arrays::stream).distinct().collect(Collectors.toList());
System.out.println(integerList);
}
/**
* @desc 计算所有苹果的总价
* @author lan [1728209643@qq.com]
* @create 2018/12/2 12:48
*/
@Test
public void testReduce() {
//总价
AtomicInteger total1 = new AtomicInteger();
apples.forEach(apple -> total1.addAndGet(apple.getWeight() * apple.getUnitPrice()));
System.out.println(total1.get());
//总价
Integer total = apples.stream().map(apple -> apple.getUnitPrice() * apple.getWeight()).reduce(0, (a, b) -> a + b);
System.out.println(total);
//所有苹果的重量
Integer totalWeight = apples.stream().map(Apple::getWeight).reduce(0, (a, b) -> a + b);
System.out.println(totalWeight);
}
/**
* @desc max/min
* @author lan [1728209643@qq.com]
* @create 2018/12/2 12:57
*/
@Test
public void testMax_Min() {
//总价最高的
Optional<Apple> optional = apples.stream().max((o1, o2) -> o1.getUnitPrice() * o2.getWeight() - o2.getUnitPrice() * o2.getWeight());
System.out.println(optional.get());
//总价最低的
optional = apples.stream().min((o1, o2) -> o1.getUnitPrice() * o2.getWeight() - o2.getUnitPrice() * o2.getWeight());
System.out.println(optional.get());
//重量最大的
optional = apples.stream().max(Comparator.comparingInt(Apple::getWeight));
System.out.println(optional.get());
}
/**
* @desc 红色苹果的数量
* @author lan [1728209643@qq.com]
* @create 2018/12/2 13:02
*/
@Test
public void testCount() {
long count = apples.stream().filter(predicate("红色")).count();
System.out.println(count);
}
/**
* @desc collect
* @author lan [1728209643@qq.com]
* @create 2018/12/2 13:08
*/
@Test
public void test() {
//list变成map, apple -> <id, apple>
Map<Integer, Apple> appleMap = apples.stream().collect(Collectors.toMap(Apple::getId, apple -> apple));
System.out.println(appleMap);
//按颜色分组, apple -> <color, List<Apple>>
Map<String, List<Apple>> groupByColor = apples.stream().collect(Collectors.groupingBy(Apple::getColor));
System.out.println(groupByColor);
}
}
测试所用的Apple类
package pers.lan.jc.e;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @author lan [1728209643@qq.com]
* @create 2018-12-02 11:12
* @desc 你是我眼中的苹果
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Apple {
//id
private int id;
//颜色
private String color;
//重量
private int weight;
//单价
private int unitPrice;
//总价
private int totalPrice;
}
PackedApple
package pers.lan.jc.e;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @author lan [1728209643@qq.com]
* @create 2018-12-02 11:41
* @desc 包装过的苹果
*/
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class PackedApple {
//包装方式
private String packageType;
//包装人
private String packageBy;
//苹果
private Apple apple;
}