Java8---函数式编程-示例
// Java8函数式编程示例—(Predicate、Stream、Optional) https://blog.csdn.net/weixin_41950473/article/details/84930562 // Set<Integer> nums = new HashSet<>(); // for (int i = 0; i < 100; i++) { // Integer num = (int) (100 * Math.random()); // nums.add(num); // System.out.println(num.intValue()); // } // // // predicate语义 // Predicate<Integer> p1 = x -> x.intValue() > 40 && x.intValue() < 50; // Predicate<Integer> p2 = x -> x.intValue() > 30 && x.intValue() < 80; // // List<Integer> res = nums.stream().filter(p1.and(p2)).collect(Collectors.toList());// 流式操作 // // Collections.sort(res, Comparator.comparing(Integer::intValue));// 排序,Comparator List<Integer> res = Arrays.asList(6,9,12); // Optional List<Integer> value = Optional.ofNullable(res) .map((param) -> { return param.stream() .map(x -> x / 3) .collect(Collectors.toList()); }) .orElseThrow(IllegalArgumentException::new); System.out.println("--->"+value);
相关推荐
Jruing 2020-11-01
89231645 2020-10-26
87204154 2020-09-24
81244053 2020-09-23
FalseNotFalse 2020-09-22
81540398 2020-09-04
84423067 2020-06-12
hongbing 2020-06-02
huavhuahua 2020-05-11
samsai00 2020-05-06
猛禽的编程艺术 2020-04-23
上海滩 2020-04-22
斑点喵 2020-03-04
cuiguanjun 2020-03-01
TheBigBlue 2020-02-20
banzhihuanyu 2020-02-19
banzhihuanyu 2020-02-15
89510196 2020-02-06
banzhihuanyu 2020-01-31