java8

集合

import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class a {
    public static void main(String[] args){
        List<String> names = Arrays.asList("peter", "anna", "mike", "xenia");
        System.out.println(names.get(0));

        //lambda 表达式
        Collections.sort(names, ( a, b) -> a.compareTo(b));
        System.out.println(names.get(0));

//        只有那些函数式接口(Functional Interface)才能缩写成 Lambda 表示式。
        Converter<String, Integer> converter = (from) -> Integer.valueOf(from);
        Integer converted = converter.convert("123");
        System.out.println(converted);    // 123


//        通过 :: 关键字来引用类的方法或构造器
        Converter<String, Integer> converter2 = Integer::valueOf;

        //访问外部变量,类似匿名内部类
        int num = 1;
        Converter<Integer, String> stringConverter =
                (from) -> String.valueOf(from + num);

        //内置的函数式接口
        Predicate<String> predicate = (s) -> s.length() > 0;

        predicate.test("foo");              // true
        predicate.negate().test("foo");     // false

        Predicate<Boolean> nonNull = Objects::nonNull;
        Predicate<Boolean> isNull = Objects::isNull;

        Predicate<String> isEmpty = String::isEmpty;
        Predicate<String> isNotEmpty = isEmpty.negate();

        //Optional
        Optional<String> optional = Optional.of("bam");

       System.out.println(optional.isPresent());           // true
        optional.get();                 // "bam"
        System.out.println(optional.orElse("fallback"));    // "bam"

        optional.ifPresent((s) -> System.out.println(s.charAt(0)));     // "b"


        //Stream
        List<String> stringCollection = new ArrayList<>();
        stringCollection.add("ddd2");
        stringCollection.add("aaa2");
        stringCollection.add("bbb1");
        stringCollection.add("aaa1");
        stringCollection.add("bbb3");
        stringCollection.add("ccc");
        stringCollection.add("bbb2");
        stringCollection.add("ddd1");

        //1. Stream filter过滤
        stringCollection
                .stream()
                .filter((s) -> s.startsWith("a"))
                .forEach(System.out::println);
//        foreach 是一个终端操作,它的返参是 void, 我们无法对其再次进行流操作。

        //2. Stream sorted 排序
        stringCollection
                .stream()
                .sorted()
                .filter((s) -> s.startsWith("a"))
                .forEach(System.out::println);


//        sorted 不会对 stringCollection 做出任何改变,stringCollection 还是原有的那些个元素,且顺序不变:
        System.out.println(stringCollection);

        //3. Stream map 将 List 中的每一个元素做功能处理
        stringCollection
                .stream()
                .map(String::toUpperCase)
                .sorted((a, b) -> b.compareTo(a))
                .forEach(System.out::println);

//       4. Stream match 用来做匹配操作,它的返回值是一个 boolean 类型
// 验证 list 中 string 是否都不是以 z 开头的,
        boolean noneStartsWithZ =
                stringCollection
                        .stream()
                        .noneMatch((s) -> s.startsWith("z"));

        boolean anyStartsWitha =
                stringCollection
                        .stream()
                        .anyMatch((s) -> s.startsWith("a"));

        System.out.println(noneStartsWithZ);      // true
        System.out.println(anyStartsWitha);

        // 5. Stream count
        // 先对 list 中字符串开头为 b 进行过滤,让后统计数量
        long startsWithB =
                stringCollection
                        .stream()
                        .filter((s) -> s.startsWith("b"))
                        .count();

        System.out.println(startsWithB);    // 3

        //   6   Stream  .collect 转成其他集合
        List<String> collect = stringCollection
                .stream()
                .filter((s) -> s.startsWith("b")).collect(Collectors.toList());
        System.out.println("collect:"+collect);

//        Parallel-Streams 并行流
        long count = stringCollection.stream().sorted().count();
        //并行流排序
        long count2 = stringCollection.parallelStream().sorted().count();


//        Map 集合
        Map<Integer, String> map = new HashMap<>();

        for (int i = 0; i < 10; i++) {
            // 与老版不同的是,putIfAbent() 方法在 put 之前,
            // 会判断 key 是否已经存在,存在则直接返回 value, 否则 put, 再返回 value
            map.putIfAbsent(i, "val" + i);
        }

// forEach 可以很方便地对 map 进行遍历操作
        map.forEach((key, value) -> System.out.println(value));
        map.entrySet().stream().forEach(System.out :: print);




    }


    @FunctionalInterface
    interface Converter<F, T> {
        T convert(F from);
    }
}

dto列表转vo
dto.stream().map(this::convert).collect(Collectors.toList())

其中convert方法
 private RecordVO convert(RecordDo recordDo){
        RecordVO vo = new RecordVO();
        BeanUtils.copyProperties(recordDo,vo);
        return vo;
    }

//对象列表转map
 Map<String,String> personMap = new HashMap<>();
        personMap = personList.stream()
                .collect(Collectors.toMap(RecordDo::getChannel, RecordDo::getAppId));

Function(需要有输入输出)

Function<R,T> 函数通畅用于封装一个通用的函数,对于参数传入的参数T,返回结果R。
对于日常中如果大部分代码相同,只是调用一个函数的差异,比如查询数据库结果集是调用不同的接口,则可以通过它来实现封装。

public interface Function<T, R> {
  R apply(T t);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值