默认方法与静态方法
@FunctionalInterface// FunctionalInterface注解可校验是否符合函数式编程public interface IUserCredential { String verIifUser(String username); static void login() { System.out.println("Login Success"); } default void signOut() { System.out.println("Log out"); }}
class RunnerMain implements IUserCredential { @Override public String verIifUser(String username) { return "萨瓦迪卡"; } public static void main(String[] args) { // Predicate 接收参数T对象,返回一个boolean类型结果 Predicate<String> pred = "admin"::equals; System.out.println(pred.test("周杰伦")); // Consumer 接收参数T对象,没有返回值 Consumer<String> con = (message) -> { System.out.println(message + "sent"); }; con.accept("message"); // Function 接收参数T对象,返回R对象 Function<String, Integer> function = (gender) -> "male".equals(gender) ? 1 : 0; System.out.println(function.apply("male")); System.out.println(function.apply("male1")); // Supplier 不接受任何参数,直接通过get()获取指定类型的对象 Supplier<String> supplier = () -> UUID.randomUUID().toString(); System.out.println(supplier.get()); // UnaryOperator 接口参数T对象,执行业务处理后,返回更新后的T对象 UnaryOperator<String> un = (img) -> { img += "200*120"; return img; }; System.out.println(un.apply("原图--")); // BinaryOperator 接口接收两个T对象,执行业务处理后,返回一个T对象 BinaryOperator<Integer> bin = (i1, i2) -> i1 > i2 ? i1 : i2; System.out.println(bin.apply(10, 20)); // 静态方法引用 EtPerson etPerson = Person::getNumber; System.out.println(etPerson.howMany()); // 实例方法引用 Person per = new Person(); EtPerson et = per::numeral; System.out.println(et.howMany()); // 构造方法引用 需要接口绑定 BindPerson person = Person::new; System.out.println(person.singer()); }}class Person { public static int getNumber() { return 1; } public int numeral() { return 1; }}interface EtPerson { int howMany();}interface BindPerson { Person singer();}
常用集合转换为stream
public class SteamTest { public static void main(String[] args) { // 1. 批量数据 -> Stream对象 // 多个数据 Stream<String> stream = Stream.of("admin", "tom", "damu"); // 数组 String[] strArrays = new String[]{"xueqi", "biyao"}; Stream<String> stream2 = Arrays.stream(strArrays); // 列表 List<String> list = new ArrayList<>(); list.add("少林"); list.add("武当"); list.add("青城"); list.add("崆峒"); list.add("峨眉"); Stream<String> stream3 = list.stream(); // 集合 Set<String> set = new HashSet<>(); set.add("少林罗汉拳"); set.add("武当长拳"); set.add("青城剑法"); Stream<String> stream4 = set.stream(); // Map Map<String, Integer> map = new HashMap<>(); map.put("tom", 1000); map.put("jerry", 1200); map.put("shuke", 1000); Stream<Map.Entry<String, Integer>> stream5 = map.entrySet().stream();// stream转数组 toArray是结束操作 String[] array = stream.toArray(String[]::new); // str String str = stream.collect(Collectors.joining()); // list List<String> stringList = stream.collect(Collectors.toList()); // set Set<String> stringSet = stream.collect(Collectors.toSet()); // Map Map<String, String> stringMap = stream.collect(Collectors.toMap(x -> x, y -> y)); // stream常见API操作 List<String> accountList = new ArrayList<>(); accountList.add("人工"); accountList.add("智能"); accountList.add("深度"); accountList.add("学习"); accountList.add("数学"); accountList.add("线性"); accountList.add("代数"); // map() 中间操作,map()方法接收一个Functional接口 accountList = accountList.stream().map(x -> "python " + x).collect(Collectors.toList()); // filter() 添加过滤条件,过滤符合条件的用户 accountList = accountList.stream().filter(x -> x.length() > 1).collect(Collectors.toList()); // peek() 中间操作 accountList.stream().peek(x -> System.out.println("peek 1:" + x)) .peek(x -> System.out.println("peek 2:" + x)) .forEach(System.out::println); // Stream中对于数字运算的支持 List<Integer> intList = new ArrayList<>(); intList.add(20); intList.add(19); intList.add(7); intList.add(8); intList.add(86); intList.add(11); intList.add(3); intList.add(20); // skip() 中间操作,有状态,跳过部分数据 intList.stream().skip(2).collect(Collectors.toList()).forEach(System.out::println); // limit() 中间操作,有状态,限制输出数据量 intList.stream().limit(3).collect(Collectors.toList()).forEach(System.out::println); // distinct() 中间操作,有状态,剔除重复的数据 intList.stream().distinct().forEach(System.out::println); // max() 获取最大值 Optional<Integer> optionalMax = intList.stream().max(Comparator.comparingInt(x -> x)); optionalMax.ifPresent(System.out::println); // min() 获取最小值 Optional<Integer> optionalMin = intList.stream().min(Comparator.comparingInt(x -> x)); optionalMin.ifPresent(System.out::println); // sorted() 中间操作,有状态,排序 intList.stream().sorted().collect(Collectors.toList()).forEach(System.out::println); // reduce() 合并处理数据 intList.stream().reduce(Integer::sum).ifPresent(System.out::println); }}
Optional
//使用后public String test(){ String fairly = redisTemplate.opsForValue().get("fairly"); Optional<String> optional = Optional.ofNullable(fairly); return optional.orElseGet(optional::get);}public String test1(){ String fairly = redisTemplate.opsForValue().get("fairly"); assert fairly != null; if(fairly.isEmpty()){ return null; } return fairly;}