Java 新特性

Java 16 新特性

Stream

// Java 16前:filter + map
Optional<JSONObject> waybillNoOptional = waybillNoList.stream()
        .filter(JSONObject.class::isInstance) // 相当于:i -> i instanceof JSONObject
        .map(i -> (JSONObject) i)
        .filter(item -> "1".equals(item.getString("waybillType")))
        .findFirst();
// Java 16:mapMulti 替代 filter+ map,一步完成类型判断和向下转换
Optional<JSONObject> waybillNoOptional = waybillNoList.stream()
                .<JSONObject>mapMulti((i,consumer)->{
                    if(i instanceof JSONObject j) consumer.accept(j);
                }).filter(item -> "1".equals(item.getString("waybillType")))
                .findFirst();

List<Bookshelf> bookshelfClassic = authors.stream()
    .flatMap(author -> author.getBooks().stream()
        .map(book -> new Bookshelf(author.getName(), book.getTitle()))
    ).collect(Collectors.toList());

// mapMulti 替代 flatMap 
List<Bookshelf> bookshelfMM = authors.stream()
    .<Bookshelf>mapMulti((author, consumer) -> {
        for (Book book : author.getBooks()) {
            consumer.accept(new Bookshelf(author.getName(), book.getTitle()));
        }
    }).collect(Collectors.toList());

Java 9 新特性

List

// List.of:List不可变(即:不能增删改,但List元素如果是对象,对象的属性可修改)
List<String> li = List.of("A","B");
// 不可变List
List<String> deptIds = deptList.stream().map(DeptDTO::getDeptId).toList();

Java 8 新特性

  • Java 8 允许您通过::关键字传递方法或构造函数的引用。

Lambda 表达式

List<String> names = Arrays.asList("banana", "apple", "orange","fruit","grape");
// 老版本Java排列字符串
Collections.sort(names, new Comparator<String>() {
    @Override
    public int compare(String a, String b) {
        return a.compareTo(b);
    }
});

// Java 8 匿名函数的方式
Collections.sort(names, (String a, String b) -> {
    return a.compareTo(b);
});
// 以上可以简写为如下
Collections.sort(names, (String a, String b) -> a.compareTo(b));
// List的sort方法
names.sort((a, b) -> a.compareTo(b));

函数式接口(Functional Interfaces)

  • 只包含一个抽象的方法,但可以有多个非抽象方法的接口,可以隐式转换为lambda表达式,一般以@FunctionalInterface进行标记,如下两个例子。

    • java.lang.Runnable
    • java.util.concurrent.Callable
  • 内置的函数式接口

    • Predicate 接口是只有一个参数的返回布尔类型值的 断言型 接口。

    • Function接口接受一个参数并生成结果。

    • Supplier接口产生给定泛型类型的结果。

    • Consumer 接口表示要对单个输入参数执行的操作。

    • Comparator是老 Java 中的经典接口, Java 8 在此之上添加了多种默认方法。

Optional

// 获取值,没有时返回默认值
Optional.ofNullable(u).map(user->user.name).orElse("Unknown");

// 多层取值,没有时异常提示
Optional.ofNullable(comp).map(Competition::getResult).map(User::getName).orElseThrow(()->new IllegalArgumentException("The value of param comp isn't available."));

// 过滤,获取不到时异常提示
Optional.ofNullable(name).filter(User::isNameValid).orElseThrow(()->new IllegalArgumentException("Invalid username"));
// 获取第一个
Optional<ChargeItem> itemCharge = chargeItemList.stream().filter(i -> i.getChargeItemId().equals(item.getChargeItemId())).findFirst();

Streams

List<String> strList = new ArrayList<>();
strList.add("one");
strList.add("two");
strList.add("three");
strList.add("four");
strList.add("five");
strList.add("six");
strList.add("seven");
strList.add("eight");

Filter(过滤)

strList = strList.stream().filter(i->"one".equals(i)).collect(Collectors.toList());
// 过滤空数据
Set<Long> orderNoSet = subDtoList.stream().map(ChargeSubDTO::getParentOrderNo).filter(Objects::nonNull).collect(Collectors.toSet());

Sorted(排序)

strList = strList.stream().filter(i->"one".equals(i))
                .sorted(Comparator.comparing(Function.identity()))
                .collect(Collectors.toList());

// 倒序
reports.stream().filter(i -> Objects.nonNull(i.getReportTime())).sorted(Comparator.comparing(ReportPO::getReportTime).reversed()).collect(Collectors.toList());

Map(转换对象)

// map
strList = strList.stream().filter(i->"one".equals(i))
                .map(String::toUpperCase).filter(Objects::nonNull).distinct()
                .sorted(Comparator.comparing(Function.identity()))
			    .collect(Collectors.toList());

// flatMap 
List<String> strList = new ArrayList<>();
strList.add("one");
strList.add("two");

List<String> strList2 = new ArrayList<>();
strList2.add("five");
strList2.add("six");

List<List<String>> list = new ArrayList<>();
list.add(strList);
list.add(strList2);

List<String> allList = list.stream().flatMap(Collection::stream).collect(Collectors.toList());

// Collectors.toMap
Map<String, String> greenPayMap = poList.stream().collect(Collectors.toMap(OrderPO::getPid, OrderPO::getGreenPay, (o, n) -> o));
// (o, n) -> o) 处理重复的`Key`,避免当出现相同 `Key`值时会抛出 `IllegalStateException` 异常。

// Collectors.groupingBy
Map<String, List<OrderDetailPO>> detailMap = detailList.stream().collect(Collectors.groupingBy(OrderDetailPO::getPid));

Match(匹配)

// 是否全匹配
strList.stream().allMatch(i->i.startsWith("o"));
// 是否有一个匹配
strList.stream().anyMatch(i->i.startsWith("o"));
// 是否全部不匹配
strList.stream().noneMatch(i->i.startsWith("o"));

Count(计数)

strList.stream().filter(i->"one".equals(i)).count();

Reduce(规约)

// 字符串连接,concat = "ABCD"
String concat = Stream.of("A", "B", "C", "D").reduce("", String::concat);
// 求最小值,minValue = -3.0
double minValue = Stream.of(-1.5, 1.0, -3.0, -2.0).reduce(Double.MAX_VALUE, Double::min);
// 求和,sumValue = 10, 有起始值
int sumValue = Stream.of(1, 2, 3, 4).reduce(0, Integer::sum);
// 求和,sumValue = 10, 无起始值
sumValue = Stream.of(1, 2, 3, 4).reduce(Integer::sum).get();

Collectors

// 1、分组
Map<String, List<User>> cityMap = users.stream().collect(Collectors.groupingBy(User::getCity));

// 2、分组并计数
Map<String, Long> countByCity = users.stream().collect(Collectors.groupingBy(User::getCity, Collectors.counting()));

// 3、分组并求平均值
Map<String, Double> i = users.stream().collect(Collectors.groupingBy(User::getCity, Collectors.averagingInt(User::getScore)));  

// 4、拼接
String str = Stream.of("A", "B", "C", "D").collect(Collectors.joining());

// 5、Collectors.mapping
List<String> names = people.stream().collect(Collectors.mapping(Person::getName, Collectors.toList()));  

// 6、Collectors.groupingBy + Collectors.mapping
Map<String, List<ReportItemTypeInfo>> mapLabClassList = labClassVOList.stream()
                .filter(i -> StringUtils.isNotBlank(i.getLabItemType()))
                .collect(Collectors.groupingBy(LabClassVO::getLabItemType, Collectors.mapping(j -> new ReportItemTypeInfo(j.getLabId(), j.getLabName()), Collectors.toList())));

Map<String, Set<String>> packagesItemsVoMap = packagesItemsService.queryListByIds(packagesIds).stream()
    .collect(Collectors.groupingBy(
        PackagesSeVo::getPackagesId,
        Collectors.mapping(PackagesSeVo::getItemsId, Collectors.toSet())
    ));

posted @ 2024-03-11 17:26  Soul-Q  阅读(111)  评论(0)    收藏  举报