前言
对于jdk1.8的特性之前没有很好地的了解过,现在花一些时间整理一下。只摘取频率使用较高的方法进行介绍。
一、分组 将id一样的苹果放在一起
Map<Integer, List<Apple>> collect = apples.stream().collect(Collectors.groupingBy(Apple::getId));
System.out.println("分组-res:" + collect);
二、Lis转成map
Collectors.toMap:(keyMapper,valueMapper,mergeFunction);
keyMapper:
表示的是key的映射方法,就是输出key之前的操作函数;valueMapper:
表示value的映射方法,就是输出value的前置函数;mergeFunction:
表示key一致时需要进行的操作。当key相同时如果没有合并函数,就会报IllegalStateException异常。
Map<Integer, Apple> collect2 = apples.stream().collect(Collectors.toMap(Apple::getId, a -> a, (k1, k2) -> k1));
System.out.println("转成-res:" + collect2);
三、过滤Filter
List<Apple> collect1 = apples.stream().filter(a -> a.id == 1).collect(Collectors.toList());
System.out.println("过滤Filter-res:" + collect1);
四、计算
reduce
(T identity, BinaryOperator accumulator)第一个函数表示初始值,第二个表示计算方法。
BigDecimal reduce1 = apples.stream().map(Apple::getMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
System.out.println("求和-res:" + reduce1);
五、查找最大值、最小值
Optional<Apple> collect3 = apples.stream().collect(Collectors.maxBy((a1, a2) -> Integer.compare(a1.getId(), a2.getId())));
System.out.println("id最大值-res:" + collect3);
Optional<Apple> collect4 = apples.stream().collect(Collectors.minBy((a1, a2) -> Integer.compare(a1.getId(), a2.getId())));
System.out.println("id最大值-res:" + collect4);
六、去重
Object collect5 = apples.stream().collect(collectingAndThen(toCollection(() -> new TreeSet<>(comparing(Apple::getId))), ArrayList::new));
System.out.println("去重-res:" + collect5);
七、排序
List<Apple> collect6 = apples.stream().sorted(comparing(Apple::getId)).collect(Collectors.toList());
System.out.println("排序-正序-res:"+collect6);
List<Apple> collect7 = apples.stream().sorted(comparing(Apple::getId).reversed()).collect(Collectors.toList());
System.out.println("排序-倒序-res:"+collect7);
八、日期
//9 LocalDate
LocalDate now = LocalDate.now();
System.out.println(String.format("今天:%s", now));
//明天
LocalDate plus = now.plus(1, ChronoUnit.DAYS);
System.out.println(String.format("明天:%s", plus));
//前后比较
System.out.println(String.format("明天比今天早:%s", now.isAfter(plus)));
//指定日期
LocalDate of = LocalDate.of(2020, 12, 12);
System.out.println(of);
//String->LocalDate
String format2 = of.format(DateTimeFormatter.ISO_LOCAL_DATE);
System.out.println(format2);
//10 LocalDateTime
LocalDateTime now1 = LocalDateTime.now();
String format1 = now1.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
System.out.println(format1);
//11 LocalTime
LocalTime now2 = LocalTime.now();
String format = now2.format(DateTimeFormatter.ofPattern("HH:mm:ss"));
System.out.println(format);
九、完整代码
public class Test {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
ArrayList<Apple> apples = new ArrayList<Apple>();
Apple apple = new Apple(1, "apple1", new BigDecimal("2.2"), 10);
Apple apple1 = new Apple(1, "apple2", new BigDecimal("2.4"), 1);
Apple apple2 = new Apple(3, "apple3", new BigDecimal("2.6"), 2);
Apple apple3 = new Apple(4, "apple4", new BigDecimal("2.1"), 5);
apples.add(apple);
apples.add(apple1);
apples.add(apple2);
apples.add(apple3);
//1:分组 将id一样的苹果放在一起
Map<Integer, List<Apple>> collect = apples.stream().collect(Collectors.groupingBy(Apple::getId));
System.out.println("分组-res:" + collect);
/**2. Lis转成map Collectors.toMap:(keyMapper,valueMapper,mergeFunction),keyMapper表示的是key的映射方法,就是输出key之前的操作函数;相对应的是valueMapper,它表示
/* value的映射方法,就是输出value的前置函数;mergeFunction表示key一致时需要进行的操作。当key相同时如果没有合并函数,就会报IllegalStateException异常。
**/
// Map<Integer, Apple> collect1 = apples.stream().collect(Collectors.toMap(Apple::getId, a -> a));
//System.out.println(collect1);//Exception in thread "main" java.lang.IllegalStateException: Duplicate key Apple{id=1, name='apple1', money=2.2, num=10}
//2.1 (k1, k2) -> k1表示去重id一样的数据
Map<Integer, Apple> collect2 = apples.stream().collect(Collectors.toMap(Apple::getId, a -> a, (k1, k2) -> k1));
System.out.println("转成-res:" + collect2);
//3.过滤Filter:
List<Apple> collect1 = apples.stream().filter(a -> a.id == 1).collect(Collectors.toList());
System.out.println("过滤Filter-res:" + collect1);
//4 .reduce(T identity, BinaryOperator<T> accumulator)第一个函数表示初始值,第二个表示计算方法
List<Integer> list1 = new ArrayList<>();
list1.add(1);
list1.add(1);
list1.add(1);
Integer reduce = list1.stream().reduce(10, Integer::sum);
System.out.println("reduce-res:" + reduce);
//5. 求和 :根据某个属性就行求和
BigDecimal reduce1 = apples.stream().map(Apple::getMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
System.out.println("求和-res:" + reduce1);
//6. 查找最大值、最小值
Optional<Apple> collect3 = apples.stream().collect(Collectors.maxBy((a1, a2) -> Integer.compare(a1.getId(), a2.getId())));
System.out.println("id最大值-res:" + collect3);
Optional<Apple> collect4 = apples.stream().collect(Collectors.minBy((a1, a2) -> Integer.compare(a1.getId(), a2.getId())));
System.out.println("id最大值-res:" + collect4);
//7.去重
Object collect5 = apples.stream().collect(collectingAndThen(toCollection(() -> new TreeSet<>(comparing(Apple::getId))), ArrayList::new));
System.out.println("去重-res:" + collect5);
//8.排序
//8.1 正序
List<Apple> collect6 = apples.stream().sorted(comparing(Apple::getId)).collect(Collectors.toList());
System.out.println("排序-正序-res:"+collect6);
List<Apple> collect7 = apples.stream().sorted(comparing(Apple::getId).reversed()).collect(Collectors.toList());
System.out.println("排序-倒序-res:"+collect7);
//9 LocalDate
LocalDate now = LocalDate.now();
System.out.println(String.format("今天:%s", now));
//明天
LocalDate plus = now.plus(1, ChronoUnit.DAYS);
System.out.println(String.format("明天:%s", plus));
//前后比较
System.out.println(String.format("明天比今天早:%s", now.isAfter(plus)));
//指定日期
LocalDate of = LocalDate.of(2020, 12, 12);
System.out.println(of);
//String->LocalDate
String format2 = of.format(DateTimeFormatter.ISO_LOCAL_DATE);
System.out.println(format2);
//10 LocalDateTime
LocalDateTime now1 = LocalDateTime.now();
String format1 = now1.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
System.out.println(format1);
//11 LocalTime
LocalTime now2 = LocalTime.now();
String format = now2.format(DateTimeFormatter.ofPattern("HH:mm:ss"));
System.out.println(format);
}
}
class Apple {
public Integer id;
private String name;
private BigDecimal money;
private Integer num;
public Apple(int id, String name, BigDecimal money, int num) {
this.name = name;
this.id = id;
this.money = money;
this.num = num;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public BigDecimal getMoney() {
return money;
}
public void setMoney(BigDecimal money) {
this.money = money;
}
public Integer getNum() {
return num;
}
public void setNum(Integer num) {
this.num = num;
}
@Override
public String toString() {
return "Apple{" +
"id=" + id +
", name='" + name + '\'' +
", money=" + money +
", num=" + num +
'}';
}
}