默认方法与静态方法

  1. @FunctionalInterface
  2. // FunctionalInterface注解可校验是否符合函数式编程
  3. public interface IUserCredential {
  4. String verIifUser(String username);
  5. static void login() {
  6. System.out.println("Login Success");
  7. }
  8. default void signOut() {
  9. System.out.println("Log out");
  10. }
  11. }
  1. class RunnerMain implements IUserCredential {
  2. @Override
  3. public String verIifUser(String username) {
  4. return "萨瓦迪卡";
  5. }
  6. public static void main(String[] args) {
  7. // Predicate 接收参数T对象,返回一个boolean类型结果
  8. Predicate<String> pred = "admin"::equals;
  9. System.out.println(pred.test("周杰伦"));
  10. // Consumer 接收参数T对象,没有返回值
  11. Consumer<String> con = (message) -> {
  12. System.out.println(message + "sent");
  13. };
  14. con.accept("message");
  15. // Function 接收参数T对象,返回R对象
  16. Function<String, Integer> function = (gender) -> "male".equals(gender) ? 1 : 0;
  17. System.out.println(function.apply("male"));
  18. System.out.println(function.apply("male1"));
  19. // Supplier 不接受任何参数,直接通过get()获取指定类型的对象
  20. Supplier<String> supplier = () -> UUID.randomUUID().toString();
  21. System.out.println(supplier.get());
  22. // UnaryOperator 接口参数T对象,执行业务处理后,返回更新后的T对象
  23. UnaryOperator<String> un = (img) -> {
  24. img += "200*120";
  25. return img;
  26. };
  27. System.out.println(un.apply("原图--"));
  28. // BinaryOperator 接口接收两个T对象,执行业务处理后,返回一个T对象
  29. BinaryOperator<Integer> bin = (i1, i2) -> i1 > i2 ? i1 : i2;
  30. System.out.println(bin.apply(10, 20));
  31. // 静态方法引用
  32. EtPerson etPerson = Person::getNumber;
  33. System.out.println(etPerson.howMany());
  34. // 实例方法引用
  35. Person per = new Person();
  36. EtPerson et = per::numeral;
  37. System.out.println(et.howMany());
  38. // 构造方法引用 需要接口绑定
  39. BindPerson person = Person::new;
  40. System.out.println(person.singer());
  41. }
  42. }
  43. class Person {
  44. public static int getNumber() {
  45. return 1;
  46. }
  47. public int numeral() {
  48. return 1;
  49. }
  50. }
  51. interface EtPerson {
  52. int howMany();
  53. }
  54. interface BindPerson {
  55. Person singer();
  56. }

常用集合转换为stream

  1. public class SteamTest {
  2. public static void main(String[] args) {
  3. // 1. 批量数据 -> Stream对象
  4. // 多个数据
  5. Stream<String> stream = Stream.of("admin", "tom", "damu");
  6. // 数组
  7. String[] strArrays = new String[]{"xueqi", "biyao"};
  8. Stream<String> stream2 = Arrays.stream(strArrays);
  9. // 列表
  10. List<String> list = new ArrayList<>();
  11. list.add("少林");
  12. list.add("武当");
  13. list.add("青城");
  14. list.add("崆峒");
  15. list.add("峨眉");
  16. Stream<String> stream3 = list.stream();
  17. // 集合
  18. Set<String> set = new HashSet<>();
  19. set.add("少林罗汉拳");
  20. set.add("武当长拳");
  21. set.add("青城剑法");
  22. Stream<String> stream4 = set.stream();
  23. // Map
  24. Map<String, Integer> map = new HashMap<>();
  25. map.put("tom", 1000);
  26. map.put("jerry", 1200);
  27. map.put("shuke", 1000);
  28. Stream<Map.Entry<String, Integer>> stream5 = map.entrySet().stream();
  29. // stream转数组 toArray是结束操作
  30. String[] array = stream.toArray(String[]::new);
  31. // str
  32. String str = stream.collect(Collectors.joining());
  33. // list
  34. List<String> stringList = stream.collect(Collectors.toList());
  35. // set
  36. Set<String> stringSet = stream.collect(Collectors.toSet());
  37. // Map
  38. Map<String, String> stringMap = stream.collect(Collectors.toMap(x -> x, y -> y));
  39. // stream常见API操作
  40. List<String> accountList = new ArrayList<>();
  41. accountList.add("人工");
  42. accountList.add("智能");
  43. accountList.add("深度");
  44. accountList.add("学习");
  45. accountList.add("数学");
  46. accountList.add("线性");
  47. accountList.add("代数");
  48. // map() 中间操作,map()方法接收一个Functional接口
  49. accountList = accountList.stream().map(x -> "python " + x).collect(Collectors.toList());
  50. // filter() 添加过滤条件,过滤符合条件的用户
  51. accountList = accountList.stream().filter(x -> x.length() > 1).collect(Collectors.toList());
  52. // peek() 中间操作
  53. accountList.stream().peek(x -> System.out.println("peek 1:" + x))
  54. .peek(x -> System.out.println("peek 2:" + x))
  55. .forEach(System.out::println);
  56. // Stream中对于数字运算的支持
  57. List<Integer> intList = new ArrayList<>();
  58. intList.add(20);
  59. intList.add(19);
  60. intList.add(7);
  61. intList.add(8);
  62. intList.add(86);
  63. intList.add(11);
  64. intList.add(3);
  65. intList.add(20);
  66. // skip() 中间操作,有状态,跳过部分数据
  67. intList.stream().skip(2).collect(Collectors.toList()).forEach(System.out::println);
  68. // limit() 中间操作,有状态,限制输出数据量
  69. intList.stream().limit(3).collect(Collectors.toList()).forEach(System.out::println);
  70. // distinct() 中间操作,有状态,剔除重复的数据
  71. intList.stream().distinct().forEach(System.out::println);
  72. // max() 获取最大值
  73. Optional<Integer> optionalMax = intList.stream().max(Comparator.comparingInt(x -> x));
  74. optionalMax.ifPresent(System.out::println);
  75. // min() 获取最小值
  76. Optional<Integer> optionalMin = intList.stream().min(Comparator.comparingInt(x -> x));
  77. optionalMin.ifPresent(System.out::println);
  78. // sorted() 中间操作,有状态,排序
  79. intList.stream().sorted().collect(Collectors.toList()).forEach(System.out::println);
  80. // reduce() 合并处理数据
  81. intList.stream().reduce(Integer::sum).ifPresent(System.out::println);
  82. }
  83. }

Optional

  1. //使用后
  2. public String test(){
  3. String fairly = redisTemplate.opsForValue().get("fairly");
  4. Optional<String> optional = Optional.ofNullable(fairly);
  5. return optional.orElseGet(optional::get);
  6. }
  7. public String test1(){
  8. String fairly = redisTemplate.opsForValue().get("fairly");
  9. assert fairly != null;
  10. if(fairly.isEmpty()){
  11. return null;
  12. }
  13. return fairly;
  14. }