首页
关于
友链
Search
1
wlop 4K 壁纸 4k8k 动态 壁纸
1,509 阅读
2
Nacos持久化MySQL问题-解决方案
958 阅读
3
Docker搭建Typecho博客
762 阅读
4
滑动时间窗口算法
746 阅读
5
Nginx反向代理微服务配置
715 阅读
生活
解决方案
JAVA基础
JVM
多线程
开源框架
数据库
前端
分布式
框架整合
中间件
容器部署
设计模式
数据结构与算法
安全
开发工具
百度网盘
天翼网盘
阿里网盘
登录
Search
标签搜索
java
javase
docker
java8
springboot
thread
spring
分布式
mysql
锁
linux
redis
源码
typecho
centos
git
map
RabbitMQ
lambda
stream
少年
累计撰写
189
篇文章
累计收到
24
条评论
首页
栏目
生活
解决方案
JAVA基础
JVM
多线程
开源框架
数据库
前端
分布式
框架整合
中间件
容器部署
设计模式
数据结构与算法
安全
开发工具
百度网盘
天翼网盘
阿里网盘
页面
关于
友链
搜索到
49
篇与
的结果
2022-09-20
JAVA8-Fork Join
JAVA8-Fork JoinFork:将一个任务拆分成多个线程执行。Join:将每个现场结果join,最后得到结果。范例:求数组总和前置数据:public static int[] data = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};1、原始实现方式 public static int calc() { int result = 0; for (int i = 0; i < data.length; i++) { result += data[i]; } return result; }原始实现方式调用:System.out.println("result=> " + calc());2、RecursiveTask实现RecursiveTask有返回值。package com.example.study.java8.forkjoin; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import lombok.ToString; import java.util.concurrent.RecursiveTask; @Data @AllArgsConstructor @NoArgsConstructor @ToString public class AccumulatorRecursiveTask extends RecursiveTask<Integer> { private int start; private int end; private int[] data; private int LIMIT=3; public AccumulatorRecursiveTask(int start, int end, int[] data) { this.start = start; this.end = end; this.data = data; } @Override protected Integer compute() { //如果小于3个就可以直接进行计算了,否则fork成多个,再计算 if(end -start <LIMIT) { int result =0; for(int i=start;i<end;i++){ result+=data[i]; } return result; } int mid = (start+end)/2; AccumulatorRecursiveTask left = new AccumulatorRecursiveTask(start,mid,data); AccumulatorRecursiveTask right = new AccumulatorRecursiveTask(mid,end,data); left.fork(); Integer rightResult = right.compute(); Integer leftResult = left.join(); return rightResult+leftResult; } } RecursiveTask调用: AccumulatorRecursiveTask task = new AccumulatorRecursiveTask(0, data.length, data); ForkJoinPool forkJoinPool = new ForkJoinPool(); Integer result = forkJoinPool.invoke(task); System.out.println("AccumulatorRecursiveTask result => " + result);3、RecursiveAction实现RecursiveAction:无返回值package com.example.study.java8.forkjoin; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import lombok.ToString; import java.util.concurrent.RecursiveAction; import java.util.concurrent.atomic.AtomicInteger; @Data @AllArgsConstructor @NoArgsConstructor @ToString public class AccumulatorRecursiveAction extends RecursiveAction { private int start; private int end; private int[] data; private int LIMIT = 3; public AccumulatorRecursiveAction(int start, int end, int[] data) { this.start = start; this.end = end; this.data = data; } @Override protected void compute() { //如果小于3个就可以直接进行计算了,否则fork成多个,再计算 if (end - start < LIMIT) { for (int i = start; i < end; i++) { AccumulatorHelper.accumulate(data[i]); } } else { int mid = (start + end) / 2; AccumulatorRecursiveAction left = new AccumulatorRecursiveAction(start, mid, data); AccumulatorRecursiveAction right = new AccumulatorRecursiveAction(mid, end, data); right.fork(); left.fork(); right.join(); left.join(); } } static class AccumulatorHelper { private static final AtomicInteger result = new AtomicInteger(0); static void accumulate(int value) { result.getAndAdd(value); } public static int getResult() { return result.get(); } static void rest() { result.set(0); } } } RecursiveAction调用: AccumulatorRecursiveAction action = new AccumulatorRecursiveAction(0, data.length, data); forkJoinPool.invoke(action); System.out.println("AccumulatorRecursiveAction result => " + AccumulatorRecursiveAction.AccumulatorHelper.getResult());最后输出结果result=> 55 AccumulatorRecursiveTask result => 55 AccumulatorRecursiveAction result => 55
2022年09月20日
144 阅读
0 评论
5 点赞
2022-09-19
JAVA8-Stream parallel 并行执行
Stream parallel 并行执行范例:求1~100000000的和,执行10次,看时间效率。代码package com.example.study.java8.collector; import java.util.function.Function; import java.util.stream.LongStream; import java.util.stream.Stream; /** * Stream parallel并行执行 * 实列:求10次,1~100000000的和,看时间效率。 */ public class ParallelProcessing { public static void main(String[] args) { //获取电脑CPU核数 System.out.println("当前电脑CPU核数= " + Runtime.getRuntime().availableProcessors()); System.out.println("The best process time(normalAdd)=> " + measureSumPerformance(ParallelProcessing::normalAdd, 100_000_000) + " MS"); System.out.println("The best process time(iterateStream1)=> " + measureSumPerformance(ParallelProcessing::iterateStream1, 100_000_000) + " MS"); System.out.println("The best process time(iterateStream2)=> " + measureSumPerformance(ParallelProcessing::iterateStream2, 100_000_000) + " MS"); System.out.println("The best process time(iterateStream3)=> " + measureSumPerformance(ParallelProcessing::iterateStream3, 100_000_000) + " MS"); System.out.println("The best process time(iterateStream4)=> " + measureSumPerformance(ParallelProcessing::iterateStream4, 100_000_000) + " MS"); System.out.println("The best process time(iterateStream5)=> " + measureSumPerformance(ParallelProcessing::iterateStream5, 100_000_000) + " MS"); } private static long measureSumPerformance(Function<Long, Long> adder, long limist) { long fastest = Long.MAX_VALUE; for (int i = 0; i < 10; i++) { Long startTimestamp = System.currentTimeMillis(); long result = adder.apply(limist); long duration = System.currentTimeMillis() - startTimestamp; // System.out.println("The result of sum=>"+result); if (duration < fastest) fastest = duration; } return fastest; } /** * 1、函数式编程实现:没有使用并行执行 * * @param limit * @return */ public static long iterateStream1(long limit) { return Stream.iterate(1L, i -> i + 1).limit(limit).reduce(0L, Long::sum); } /** * 2、函数式编程实现-进化:使用并行执行,要进行拆箱装箱 * * @param limit * @return */ public static long iterateStream2(long limit) { return Stream.iterate(1L, i -> i + 1).parallel().limit(limit).reduce(0L, Long::sum); } /** * 3、函数式编程实现-再次进化:只用并行执行,不进行拆箱装箱 * * @param limit * @return */ public static long iterateStream3(long limit) { return Stream.iterate(1L, i -> i + 1).mapToLong(Long::longValue).parallel().limit(limit).reduce(0L, Long::sum); } /** * 4、函数式编程实现-再次再次进化:只用并行执行 * * @param limit * @return */ public static long iterateStream4(long limit) { return LongStream.rangeClosed(1L, limit).parallel().sum(); } /** * 5、函数式编程实现-再次再次再次进化 * * @param limit * @return */ public static long iterateStream5(long limit) { return LongStream.rangeClosed(1L, limit).parallel().reduce(0L, Long::sum); } /** * 原始写法 * * @param limit * @return */ public static long normalAdd(long limit) { long result = 0L; for (long i = 1; i < limit; i++) { result++; } return result; } } 输出结果当前电脑CPU核数= 16 The best process time(normalAdd)=> 29 MS The best process time(iterateStream1)=> 794 MS The best process time(iterateStream2)=> 2718 MS The best process time(iterateStream3)=> 2132 MS The best process time(iterateStream4)=> 6 MS The best process time(iterateStream5)=> 24 MS4和5效率差不多输出结果:当前电脑CPU核数= 16 The best process time(normalAdd)=> 29 MS The best process time(iterateStream4)=> 4 MS The best process time(iterateStream5)=> 5 MS结果可以看到使用LongStream的parallel并发执行效率最高。使用注意点Source DecomposabilityArrayList Excellent( 极好的)LinkedList Poor(不好的)IntStream.range Excellent( 极好的)Stream.iterate Poor(不好的)HashSet Good(好的)TreeSet Good(好的)上面的例子就是使用的LongStream.rangeClosed(),就是IntStream.range效率Excellent( 极好的)。
2022年09月19日
156 阅读
0 评论
4 点赞
2022-09-19
JAVA8-自定义Collector
自定义Collector实现Collector接口package com.example.study.java8.collector; import java.util.*; import java.util.function.BiConsumer; import java.util.function.BinaryOperator; import java.util.function.Function; import java.util.function.Supplier; import java.util.stream.Collector; /** * 自定义Collector * T:元素类型 * List<T>:要创建的类型 * List<T>:最后要返回的类型 */ public class ToListCollector<T> implements Collector<T, List<T>, List<T>> { private void log(final String log) { System.out.println(Thread.currentThread().getName()+"-"+log); } //一定时可变的supplier,不是固定值 @Override public Supplier<List<T>> supplier() { log("supplier"); return ArrayList::new; } //要进行的操作 @Override public BiConsumer<List<T>, T> accumulator() { log("accumulator"); return List::add; } //将结果整合 @Override public BinaryOperator<List<T>> combiner() { log("combiner"); return (list1,list2)->{ list1.addAll(list2); return list1; }; } //返回结果 @Override public Function<List<T>, List<T>> finisher() { log("finisher"); // return t->t; return Function.identity(); } //特征值:CONCURRENT-并行,UNORDERED-排序,IDENTITY_FINISH-入参就出参 @Override public Set<Characteristics> characteristics() { log("characteristics"); return Collections.unmodifiableSet(EnumSet.of(Characteristics.IDENTITY_FINISH, Characteristics.CONCURRENT)); } } 使用测试package com.example.study.java8.collector; import java.util.Arrays; import java.util.List; import java.util.stream.Collector; /** * 使用自定义Collector测试 */ public class CustomToListCollector { public static void main(String[] args) { String[] arrs = new String[]{"chinese","japanse","english","freach","Korean"}; //测试 //对应接口中的: //Supplier<A> supplier(), BiConsumer<A, T> accumulator(),Function<A, R> finisher(); Collector<String, List<String>, List<String>> collector = new ToListCollector<String>(); List<String> list = Arrays.stream(arrs).filter(s -> s.length() > 6).collect(collector); System.out.println(list); System.out.println("================================"); //parallelStream(),并行使用测试 List<String> parallelList = Arrays.asList(new String[]{"chinese", "japanse", "english", "freach", "Korean"}) .parallelStream() .filter(s -> s.length() > 6) .collect(collector); System.out.println(parallelList); } } 输出结果main-supplier main-accumulator main-combiner main-characteristics main-characteristics [chinese, japanse, english] ================================ main-characteristics main-characteristics main-supplier main-accumulator main-combiner main-characteristics main-characteristics [chinese, japanse, english]由于数据量少,并没有出现并行。
2022年09月19日
207 阅读
0 评论
4 点赞
2022-09-19
JAVA8-Collector interface源码分析
Collector interface源码分析源码public interface Collector<T, A, R> { Supplier<A> supplier(); BiConsumer<A, T> accumulator(); Function<A, R> finisher(); BinaryOperator<A> combiner(); Set<Characteristics> characteristics(); }说明:1.T is the generic type of the items in the stream to be collected.2.A is the type of the accumulator, the object on which the partial result will be accumulated during thecollection process.3.R is the type of the object (typically, but not always, the collection) resulting from the collectoperation.T:传入参数类型,A:需要进行处理的方法,R:处理后返回的结果特征值:CONCURRENT:并行的UNORDERED:无序的IDENTITY_FINISH:传入什么,返回什么范例toList()源码1、入口menu.stream().filter(t->t.isVegetarian()).collect(Collectors.toList())2、Collectors.toList()源码: public static <T> Collector<T, ?, List<T>> toList() { return new CollectorImpl<>((Supplier<List<T>>) ArrayList::new, List::add, (left, right) -> { left.addAll(right); return left; }, CH_ID); }3、最后也就是Collector接口 CollectorImpl(Supplier<A> supplier, BiConsumer<A, T> accumulator, BinaryOperator<A> combiner, Set<Characteristics> characteristics) { this(supplier, accumulator, combiner, castingIdentity(), characteristics); }流程图1、Collector执行流程图 执行过程:通过Supplier supplier创建一个Container容器。查看stream里面是否还有元素。如果有,通过带2个入参一个返回结果参数的BiConsumer<A, T> accumulator进行数据处理。处理完后通过finisher返回执行结果。collect源码: 说明:1、获取容器container = collector.supplier().get(); 2、通过accumulator进行处理forEach(u -> accumulator.accept(container, u));3、执行完,返回执行结果return collector.characteristics().contains(Collector.Characteristics.IDENTITY_FINISH)2、combiner并行执行执行流程图 产生多个supplier并行处理,然后将多个处理结果combiner成一个,然后将结果返回。
2022年09月19日
153 阅读
0 评论
1 点赞
2022-09-18
JAVA8-Collectors API:summingDouble、summingInt、testSummingLong、toCollection、toConcurrentMap、toList、toSet、toMap
JAVA8-Collectors API:summingDouble、summingInt、testSummingLong、toCollection、toConcurrentMap、toList、toSet、toMap前置数据 public static final List<Dish> menu = Arrays.asList( new Dish("pork", false, 800, Dish.Type.MEAT), new Dish("beef", false, 700, Dish.Type.MEAT), new Dish("chicken", false, 400, Dish.Type.MEAT), new Dish("french fries", true, 530, Dish.Type.OTHER), new Dish("rice", true, 350, Dish.Type.OTHER), new Dish("season fruit", true, 120, Dish.Type.OTHER), new Dish("pizza", true, 550, Dish.Type.OTHER), new Dish("prawns", false, 300, Dish.Type.FISH), new Dish("salmon", false, 450, Dish.Type.FISH));范例:1、summingDouble public static void testAveragingDouble(){ System.out.println("testAveragingDouble"); //用reduce聚合求和 Optional.ofNullable(menu.stream().map(Dish::getCalories).reduce(Integer::sum)).get().ifPresent(System.out::println); //用collectors averagingDouble求平均值 Optional.ofNullable(menu.stream().collect(averagingDouble(Dish::getCalories))).ifPresent(System.out::println); }输出结果:testSummingDouble 4200.0 42002、summingDouble public static void testSummingInt() { System.out.println("testSummingInt"); Optional.ofNullable(menu.stream().collect(Collectors.summingInt(Dish::getCalories))).ifPresent(System.out::println); }输出结果:testSummingInt 42003、summingLong public static void testSummingLong() { System.out.println("testSummingLong"); Optional.ofNullable(menu.stream().collect(Collectors.summingLong(Dish::getCalories))).ifPresent(System.out::println); }输出结果:testSummingLong 42004、toCollection 返回Collection子类 public static void testToCollection() { System.out.println("testToCollection"); Optional.ofNullable(menu.stream().collect(Collectors.toCollection(LinkedList::new))).ifPresent(System.out::println); }输出结果:testToCollection [Dish{name='pork', vegetarian=false, calories=800, type=MEAT}, Dish{name='beef', vegetarian=false, calories=700, type=MEAT}, Dish{name='chicken', vegetarian=false, calories=400, type=MEAT}, Dish{name='french fries', vegetarian=true, calories=530, type=OTHER}, Dish{name='rice', vegetarian=true, calories=350, type=OTHER}, Dish{name='season fruit', vegetarian=true, calories=120, type=OTHER}, Dish{name='pizza', vegetarian=true, calories=550, type=OTHER}, Dish{name='prawns', vegetarian=false, calories=300, type=FISH}, Dish{name='salmon', vegetarian=false, calories=450, type=FISH}]5、toConcurrentMap public static void testToConcurrentMap() { System.out.println("testToConcurrentMap"); Optional.ofNullable(menu.stream().collect(Collectors.toConcurrentMap(Dish::getName,Dish::getCalories))).ifPresent(System.out::println); }输出结果:testToConcurrentMap {season fruit=120, chicken=400, pizza=550, salmon=450, beef=700, pork=800, rice=350, french fries=530, prawns=300}6、toConcurrentMap 按类型统计个数 public static void testToConcurrentMapWithBinaryOperator() { System.out.println("testToConcurrentMapWithBinaryOperator"); Optional.ofNullable(menu.stream().collect(Collectors.toConcurrentMap(Dish::getType,v->1L,(a,b)->a+b))).ifPresent(v->{ System.out.println(v); System.out.println(v.getClass()); }); }输出结果:testToConcurrentMapWithBinaryOperator {OTHER=4, MEAT=3, FISH=2} class java.util.concurrent.ConcurrentHashMap7、toConcurrentMap 按类型统计个数,返回指定类型Map public static void testToConcurrentMapWithBinaryOperatorAndSupplier() { System.out.println("testToConcurrentMapWithBinaryOperatorAndSupplier"); Optional.ofNullable(menu.stream().collect(Collectors.toConcurrentMap(Dish::getType,v->1L,(a,b)->a+b, ConcurrentSkipListMap::new))).ifPresent(v->{ System.out.println(v); System.out.println(v.getClass()); }); }输出结果:testToConcurrentMapWithBinaryOperatorAndSupplier {MEAT=3, FISH=2, OTHER=4} class java.util.concurrent.ConcurrentSkipListMap8、toList 转成list public static void testTolist() { System.out.println("testTolist"); Optional.ofNullable(menu.stream().filter(t->t.isVegetarian()).collect(Collectors.toList())).ifPresent(v->{ System.out.println(v); System.out.println(v.getClass()); }); }输出结果:testTolist [Dish{name='french fries', vegetarian=true, calories=530, type=OTHER}, Dish{name='rice', vegetarian=true, calories=350, type=OTHER}, Dish{name='season fruit', vegetarian=true, calories=120, type=OTHER}, Dish{name='pizza', vegetarian=true, calories=550, type=OTHER}] class java.util.ArrayList9、toSett 转成Set public static void testToSet() { System.out.println("testToSet"); Optional.ofNullable(menu.stream().filter(t->t.isVegetarian()).collect(Collectors.toSet())).ifPresent(v->{ System.out.println(v); System.out.println(v.getClass()); }); }输出结果:testToSet [Dish{name='french fries', vegetarian=true, calories=530, type=OTHER}, Dish{name='season fruit', vegetarian=true, calories=120, type=OTHER}, Dish{name='pizza', vegetarian=true, calories=550, type=OTHER}, Dish{name='rice', vegetarian=true, calories=350, type=OTHER}] class java.util.HashSet10、toMappublic static void testToMap() { System.out.println("testToMap"); Optional.ofNullable(menu.stream().collect(Collectors.toMap(Dish::getName,Dish::getCalories))).ifPresent(v->{ System.out.println(v); System.out.println(v.getClass()); }); //转成线程安全的map Optional.ofNullable(menu.stream().collect(Collectors.collectingAndThen(Collectors.toMap(Dish::getName, Dish::getCalories), Collections::synchronizedMap))).ifPresent(v->{ System.out.println(v); System.out.println(v.getClass()); }); }输出结果:testToMap {season fruit=120, chicken=400, pizza=550, salmon=450, beef=700, pork=800, rice=350, french fries=530, prawns=300} class java.util.HashMap {season fruit=120, chicken=400, pizza=550, salmon=450, beef=700, pork=800, rice=350, french fries=530, prawns=300} class java.util.Collections$SynchronizedMap11、toMap 按类型统计个数public static void testToMapWithBinaryOperator() { System.out.println("testToMapWithBinaryOperator"); Optional.ofNullable(menu.stream().collect(Collectors.toMap(Dish::getType,v->1L,(a,b)->a+b))).ifPresent(v->{ System.out.println(v); System.out.println(v.getClass()); }); }输出结果:testToMapWithBinaryOperator {MEAT=3, FISH=2, OTHER=4} class java.util.HashMap12、toMap 按类型统计个数,返回指定类型Ma ##p public static void testToMapWithBinaryOperatorAndSupplier() { System.out.println("testToMapWithBinaryOperatorAndSupplier"); Optional.ofNullable(menu.stream().collect(Collectors.toMap(Dish::getType,v->1L,(a,b)->a+b, ConcurrentSkipListMap::new))).ifPresent(v->{ System.out.println(v); System.out.println(v.getClass()); }); }输出结果:testToMapWithBinaryOperatorAndSupplier {MEAT=3, FISH=2, OTHER=4} class java.util.concurrent.ConcurrentSkipListMap
2022年09月18日
138 阅读
0 评论
4 点赞
2022-09-18
JAVA8-Collectors API: partitioningBy、reducing、summarizingLong、summarizingInt、summarizingDouble
JAVA8-Collectors API: partitioningBy、reducing、summarizingLong、summarizingInt、summarizingDouble前置数据: public static final List<Dish> menu = Arrays.asList( new Dish("pork", false, 800, Dish.Type.MEAT), new Dish("beef", false, 700, Dish.Type.MEAT), new Dish("chicken", false, 400, Dish.Type.MEAT), new Dish("french fries", true, 530, Dish.Type.OTHER), new Dish("rice", true, 350, Dish.Type.OTHER), new Dish("season fruit", true, 120, Dish.Type.OTHER), new Dish("pizza", true, 550, Dish.Type.OTHER), new Dish("prawns", false, 300, Dish.Type.FISH), new Dish("salmon", false, 450, Dish.Type.FISH));范例:1、partitioningBy 分组 public static void testPartitioningByWithPredicate() { System.out.println("testPartitioningByWithPredicate"); Map<Boolean, List<Dish>> collect = menu.stream().collect(Collectors.partitioningBy(Dish::isVegetarian)); Optional.ofNullable(collect).ifPresent(System.out::println); }输出结果:testPartitioningByWithPredicate {false=[Dish{name='pork', vegetarian=false, calories=800, type=MEAT}, Dish{name='beef', vegetarian=false, calories=700, type=MEAT}, Dish{name='chicken', vegetarian=false, calories=400, type=MEAT}, Dish{name='prawns', vegetarian=false, calories=300, type=FISH}, Dish{name='salmon', vegetarian=false, calories=450, type=FISH}], true=[Dish{name='french fries', vegetarian=true, calories=530, type=OTHER}, Dish{name='rice', vegetarian=true, calories=350, type=OTHER}, Dish{name='season fruit', vegetarian=true, calories=120, type=OTHER}, Dish{name='pizza', vegetarian=true, calories=550, type=OTHER}]}2、partitioningBy 分组后,再处理求平均数 public static void testPartitioningByWithPredicateAndCollecotr() { System.out.println("testPartitioningByWithPredicateAndCollecotr"); Map<Boolean, Double> collect = menu.stream().collect(Collectors.partitioningBy(Dish::isVegetarian, Collectors.averagingInt(Dish::getCalories))); Optional.ofNullable(collect).ifPresent(System.out::println); }输出结果:testPartitioningByWithPredicateAndCollecotr {false=530.0, true=387.5}3、reducing public static void testReducingBinaryOperator() { System.out.println("testReducingBinaryOperator"); Optional<Dish> collect = menu.stream().collect(Collectors.reducing(BinaryOperator.maxBy(Comparator.comparingInt(Dish::getCalories)))); collect.ifPresent(System.out::println); }输出结果:testReducingBinaryOperator Dish{name='pork', vegetarian=false, calories=800, type=MEAT}4、reducing public static void testReducingBinaryOperatorAndIdentity() { System.out.println("testReducingBinaryOperatorAndIdentity"); Integer collect = menu.stream().map(Dish::getCalories).collect(Collectors.reducing(0, (d1, d2) -> d1 + d2)); System.out.println(collect); }输出结果:testReducingBinaryOperatorAndIdentity 42005、reducing public static void testReducingBinaryOperatorAndIdentityAndFunction() { System.out.println("testReducingBinaryOperatorAndIdentityAndFunction"); Integer collect = menu.stream().collect(Collectors.reducing(0, Dish::getCalories,(d1, d2) -> d1 + d2)); System.out.println(collect); }输出结果:testReducingBinaryOperatorAndIdentityAndFunction 42006、summarizingDouble public static void testSummarizingDouble() { System.out.println("testSummarizingDouble"); DoubleSummaryStatistics collect = menu.stream().collect(Collectors.summarizingDouble(Dish::getCalories)); Optional.ofNullable(collect).ifPresent(System.out::println); }输出结果:testSummarizingDouble DoubleSummaryStatistics{count=9, sum=4200.000000, min=120.000000, average=466.666667, max=800.000000}7、summarizingInt public static void testSummarizingInt() { System.out.println("testSummarizingDouble"); IntSummaryStatistics collect = menu.stream().collect(Collectors.summarizingInt(Dish::getCalories)); Optional.ofNullable(collect).ifPresent(System.out::println); }输出结果:testSummarizingDouble IntSummaryStatistics{count=9, sum=4200, min=120, average=466.666667, max=800}8、summarizingLong public static void testSummarizingLong() { System.out.println("testSummarizingLong"); LongSummaryStatistics collect = menu.stream().collect(Collectors.summarizingLong(Dish::getCalories)); Optional.ofNullable(collect).ifPresent(System.out::println); }输出结果:testSummarizingLong LongSummaryStatistics{count=9, sum=4200, min=120, average=466.666667, max=800}
2022年09月18日
153 阅读
0 评论
2 点赞
2022-09-18
JAVA8-Collectors API: groupingByConcurrent、joining、mapping、maxby、minby
JAVA8-Collectors API: groupingByConcurrent、joining、mapping、maxby、minby前置数据 public static final List<Dish> menu = Arrays.asList( new Dish("pork", false, 800, Dish.Type.MEAT), new Dish("beef", false, 700, Dish.Type.MEAT), new Dish("chicken", false, 400, Dish.Type.MEAT), new Dish("french fries", true, 530, Dish.Type.OTHER), new Dish("rice", true, 350, Dish.Type.OTHER), new Dish("season fruit", true, 120, Dish.Type.OTHER), new Dish("pizza", true, 550, Dish.Type.OTHER), new Dish("prawns", false, 300, Dish.Type.FISH), new Dish("salmon", false, 450, Dish.Type.FISH));范例:1、groupingByConcurrent 返回map为ConcurrentMap,按类型分组 public static void testGroupingByConcurrentWithFunction() { System.out.println("testGroupingByConcurrentWithFunction"); ConcurrentMap<Dish.Type, List<Dish>> collect = menu.stream().collect(Collectors.groupingByConcurrent(Dish::getType)); Optional.ofNullable(collect.getClass()).ifPresent(System.out::println); Optional.ofNullable(collect).ifPresent(System.out::println); }输出结果:testGroupingByConcurrentWithFunction class java.util.concurrent.ConcurrentHashMap {MEAT=[Dish{name='pork', vegetarian=false, calories=800, type=MEAT}, Dish{name='beef', vegetarian=false, calories=700, type=MEAT}, Dish{name='chicken', vegetarian=false, calories=400, type=MEAT}], FISH=[Dish{name='prawns', vegetarian=false, calories=300, type=FISH}, Dish{name='salmon', vegetarian=false, calories=450, type=FISH}], OTHER=[Dish{name='french fries', vegetarian=true, calories=530, type=OTHER}, Dish{name='rice', vegetarian=true, calories=350, type=OTHER}, Dish{name='season fruit', vegetarian=true, calories=120, type=OTHER}, Dish{name='pizza', vegetarian=true, calories=550, type=OTHER}]}2、groupingByConcurrent 返回map为ConcurrentMap,按按类型分组,并求平均值 public static void testGroupingByConcurrentWithFunctionAndCollector() { System.out.println("testGroupingByConcurrentWithFunctionAndCollector"); Map<Dish.Type, Double> collect = menu.stream().collect(Collectors.groupingByConcurrent(Dish::getType, Collectors.averagingInt(Dish::getCalories))); Optional.ofNullable(collect.getClass()).ifPresent(System.out::println); Optional.ofNullable(collect).ifPresent(System.out::println); }输出结果:testGroupingByConcurrentWithFunctionAndCollector class java.util.concurrent.ConcurrentHashMap {MEAT=633.3333333333334, FISH=375.0, OTHER=387.5}3、groupingByConcurrent 返回map为ConcurrentMap,按按类型分组,并求平均值,并修改返回类型为ConcurrentSkipListMap(跳表,以空间换时间的数据结构) public static void testGroupingByConcurrentWithFunctionAndSupplierAndCollector() { System.out.println("testGroupingByConcurrentWithFunctionAndSupplierAndCollector"); ConcurrentSkipListMap<Dish.Type, Double> collect = menu.stream().collect(Collectors.groupingByConcurrent(Dish::getType, ConcurrentSkipListMap::new, Collectors.averagingInt(Dish::getCalories))); Optional.ofNullable(collect.getClass()).ifPresent(System.out::println); Optional.ofNullable(collect).ifPresent(System.out::println); }输出结果:testGroupingByConcurrentWithFunctionAndSupplierAndCollector class java.util.concurrent.ConcurrentSkipListMap {MEAT=633.3333333333334, FISH=375.0, OTHER=387.5}4、joining 拼接,不能直接join public static void testJoin() { System.out.println("testJoin"); String collect = menu.stream().map(Dish::getName).collect(Collectors.joining()); Optional.ofNullable(collect).ifPresent(System.out::println); }输出结果:testJoin porkbeefchickenfrench friesriceseason fruitpizzaprawnssalmon5、joining 拼接,通过逗号拼接 public static void testJoinWithDelimiter() { System.out.println("testJoinWithDelimter"); String collect = menu.stream().map(Dish::getName).collect(Collectors.joining(",")); Optional.ofNullable(collect).ifPresent(System.out::println); }输出结果:testJoinWithDelimter pork,beef,chicken,french fries,rice,season fruit,pizza,prawns,salmon6、joining 拼接,通过逗号拼接,拼接后加前缀和后缀 public static void testJoinWithDelimiterAndPrefixAndSuffix() { System.out.println("testJoinWithDelimiterAndPrefixAndSuffix"); String collect = menu.stream().map(Dish::getName).collect(Collectors.joining(",","Names:[","]")); Optional.ofNullable(collect).ifPresent(System.out::println); }输出结果:testJoinWithDelimiterAndPrefixAndSuffix Names:[pork,beef,chicken,french fries,rice,season fruit,pizza,prawns,salmon]7、mapping 拼接,能直接join public static void testMapping() { System.out.println("testMapping"); String collect = menu.stream().collect(Collectors.mapping(Dish::getName, Collectors.joining(","))); Optional.ofNullable(collect).ifPresent(System.out::println); }输出结果:testMapping pork,beef,chicken,french fries,rice,season fruit,pizza,prawns,salmon8、maxBy 查找最大的一个Dish public static void testMaxBy() { System.out.println("testMaxBy"); Optional<Dish> collect = menu.stream().collect(Collectors.maxBy(Comparator.comparing(Dish::getCalories))); collect.ifPresent(System.out::println); }输出结果:testMaxBy Dish{name='pork', vegetarian=false, calories=800, type=MEAT}9、minBy 查找最小的一个Dish public static void testMinBy() { System.out.println("testMinBy"); Optional<Dish> collect = menu.stream().collect(Collectors.minBy(Comparator.comparing(Dish::getCalories))); collect.ifPresent(System.out::println); }输出结果:testMinBy Dish{name='season fruit', vegetarian=true, calories=120, type=OTHER}
2022年09月18日
136 阅读
0 评论
3 点赞
2022-09-18
JAVA8-Collectors API:averaging、collectingAndThen、counting、groupingBy
JAVA8-Collectors API:averaging、collectingAndThen、counting、groupingBy前置数据 public static final List<Dish> menu = Arrays.asList( new Dish("pork", false, 800, Dish.Type.MEAT), new Dish("beef", false, 700, Dish.Type.MEAT), new Dish("chicken", false, 400, Dish.Type.MEAT), new Dish("french fries", true, 530, Dish.Type.OTHER), new Dish("rice", true, 350, Dish.Type.OTHER), new Dish("season fruit", true, 120, Dish.Type.OTHER), new Dish("pizza", true, 550, Dish.Type.OTHER), new Dish("prawns", false, 300, Dish.Type.FISH), new Dish("salmon", false, 450, Dish.Type.FISH));范例:1、averagingDouble 求平均值 public static void testAveragingDouble(){ System.out.println("testAveragingDouble"); //用reduce聚合求和 Optional.ofNullable(menu.stream().map(Dish::getCalories).reduce(Integer::sum)).get().ifPresent(System.out::println); //用collectors averagingDouble求平均值 Optional.ofNullable(menu.stream().collect(averagingDouble(Dish::getCalories))).ifPresent(System.out::println); }输出结果:testAveragingDouble 4200 466.66666666666672、averagingInt 求平均值 public static void testAveragingInt(){ System.out.println("testAveragingInt"); //用collectors averagingDouble求平均值 Optional.ofNullable(menu.stream().collect(averagingInt(Dish::getCalories))).ifPresent(System.out::println); }输出结果:testAveragingInt 466.66666666666673、averagingLong 求平均值 public static void testAveragingLong(){ System.out.println("testAveragingLong"); //用collectors averagingLong求平均值 Optional.ofNullable(menu.stream().collect(averagingLong(Dish::getCalories))).ifPresent(System.out::println); }输出结果:testAveragingLong 466.66666666666674、collectingAndThen(收集数据,处理数据),将搜集结果,再做处理。4.1、CollectingAndThen 求平均数后,拼接一句话 Optional.ofNullable(menu.stream().collect(Collectors.collectingAndThen(averagingInt(Dish::getCalories),a->"This is Calories eques = "+a))).ifPresent(System.out::println);输出结果:testCollectingAndThen This is Calories eques = 466.66666666666674.2、获取MEAT类,之后再往里面加入其它类型 List<Dish> collect = menu.stream().filter(m -> Dish.Type.MEAT.equals(m.getType())).collect(toList()); collect.add(new Dish("回锅肉", true, 550, Dish.Type.OTHER)); collect.stream().forEach(System.out::println);输出结果:Dish{name='pork', vegetarian=false, calories=800, type=MEAT} Dish{name='beef', vegetarian=false, calories=700, type=MEAT} Dish{name='chicken', vegetarian=false, calories=400, type=MEAT} Dish{name='回锅肉', vegetarian=true, calories=550, type=OTHER}4.3、CollectingAndThen 如果想将收集结果设置为不可修改 List<Dish> meatCollect = menu.stream().filter(m -> Dish.Type.MEAT.equals(m.getType())).collect(collectingAndThen(toList(), Collections::unmodifiableList)); //修改时就会报错:Exception in thread "main" java.lang.UnsupportedOperationException meatCollect.add(new Dish("回锅肉", true, 550, Dish.Type.OTHER)); collect.stream().forEach(System.out::println);输出结果:Exception in thread "main" java.lang.UnsupportedOperationException5、counting 统计 返回Long类型 public static void testCounting(){ System.out.println("testCounting"); Optional.ofNullable(menu.stream().collect(Collectors.counting())).ifPresent(System.out::println); }输出结果:testCounting 96、groupingBy 分组public static void testGroupingByFunction(){ System.out.println("testGroupingByFunction"); Optional.of(menu.stream().collect(Collectors.groupingBy(Dish::getType))).ifPresent(System.out::println); }输出结果:testGroupingByFunction {OTHER=[Dish{name='french fries', vegetarian=true, calories=530, type=OTHER}, Dish{name='rice', vegetarian=true, calories=350, type=OTHER}, Dish{name='season fruit', vegetarian=true, calories=120, type=OTHER}, Dish{name='pizza', vegetarian=true, calories=550, type=OTHER}], MEAT=[Dish{name='pork', vegetarian=false, calories=800, type=MEAT}, Dish{name='beef', vegetarian=false, calories=700, type=MEAT}, Dish{name='chicken', vegetarian=false, calories=400, type=MEAT}], FISH=[Dish{name='prawns', vegetarian=false, calories=300, type=FISH}, Dish{name='salmon', vegetarian=false, calories=450, type=FISH}]} 7、groupingBy 分组后统计 public static void testGroupingByFunctionAndCollector(){ System.out.println("testGroupingByFunctionAndCollector"); Optional.of(menu.stream().collect(Collectors.groupingBy(Dish::getType,counting()))).ifPresent(System.out::println); }输出结果:testGroupingByFunctionAndCollector {OTHER=4, MEAT=3, FISH=2}8、groupingBy 分组后求平均值,默认是HashMap,通过groupingBy修改返回类型 public static void testGroupingByFunctionAndSuppilerAndCollector(){ System.out.println("testGroupingByFunctionAndSuppilerAndCollector"); Map<Dish.Type, Double> map = Optional.of(menu.stream().collect(groupingBy(Dish::getType, averagingInt(Dish::getCalories)))).get(); //默认是返回HashMap类型 Optional.ofNullable(map.getClass()).ifPresent(System.out::println); TreeMap<Dish.Type, Double> newMap = Optional.of(menu.stream().collect(groupingBy(Dish::getType, TreeMap::new, averagingInt(Dish::getCalories)))).get(); //groupingBy:修改返回类型为TreeMap Optional.ofNullable(newMap.getClass()).ifPresent(System.out::println); }输出结果:testGroupingByFunctionAndSuppilerAndCollector class java.util.HashMap class java.util.TreeMap
2022年09月18日
168 阅读
0 评论
5 点赞
2022-09-15
JAVA8-Optional API
Optional API一、Optional创建方式前置条件:Insurance对象:public class Insurance { private String name; public String getName() { return name; } }1、empty特点:使用get()方法时会抛出异常:No value present范例:Optional<Insurance> emptyOptional = Optional.<Insurance>empty();使用get()获取结果:抛出异常emptyOptional.get();输出结果:Exception in thread "main" java.util.NoSuchElementException: No value present at java.base/java.util.Optional.get(Optional.java:148)2、of特点:使用get(),不会抛异常范例:Optional<Insurance> ofInsurance = Optional.of(new Insurance()); ofInsurance.get();3、ofNullable特点:上面两者综合,为null是掉empty,不为空调of。3.1、为null时,调get()抛出异常范例: Optional<Insurance> ofNullableOptionalNull = Optional.ofNullable(null); ofNullableOptionalNull.get();输出结果:Exception in thread "main" java.util.NoSuchElementException: No value present3.2、不为null时,调get()不报错范例: Optional<Insurance> ofNullableOptionalNotNull = Optional.ofNullable(new Insurance()); ofNullableOptionalNotNull.get();二、orElseGet、orElse、orElseThrow不管那种方式创建,都适用这几个方法。1、orElseGet说明:不为null就返回值,为null返回一个构造的对象supplier范例:Insurance orElseGetInsurance = ofNullableOptionalNull.orElseGet(Insurance::new); System.out.println(orElseGetInsurance);2、orElse说明:不为null就返回值,否则返回一个引用范例:Insurance orElseInsurance = ofNullableOptionalNull.orElse(new Insurance()); System.out.println(orElseInsurance);3、orElseThrow说明:不为null就返回值,否则返回一个异常对象范例:Insurance orElseThrowInsurance = ofNullableOptionalNull.orElseThrow(RuntimeException::new);输出结果:抛出运行时异常Exception in thread "main" java.lang.RuntimeException at java.base/java.util.Optional.orElseThrow(Optional.java:408)4、orElseThrow说明:不为null就返回值,否则返回一个自定义异常对象范例:ofNullableOptionalNull.orElseThrow(() -> new RuntimeException("yanxizhu Exception"));输出结果:抛出自定义异常Exception in thread "main" java.lang.RuntimeException: yanxizhu Exception三、filter先判断传入predicate是否null,让然后判断predicate传入字段是否存在,不存在:返回this。存在:判断predicate条件是否成立,成立:返回this,不成立:返回空。源码: public Optional<T> filter(Predicate<? super T> predicate) { //1、判断传入参数predicate是否为null Objects.requireNonNull(predicate); //2、判断t.getName是否存在, if (!isPresent()) { //2.1、不存在,返回predicate return this; } else { //2.2、存在:判断predicate条件是否成立,成立返回this,不成立返回empty() return predicate.test(value) ? this : empty(); } }requireNonNull()源码:判断传入T是否为null public static <T> T requireNonNull(T obj) { if (obj == null) throw new NullPointerException(); return obj; }范例:使用前创建Optional对象:1、创建Optinal,get()会抛出异常 Optional<Insurance> emptyOptional = Optional.<Insurance>empty(); 2、of创建,get(),不会抛异常 Optional<Insurance> ofInsurance = Optional.of(new Insurance()); 3、ofNullable:上面两者综合 3.1、为null时,调get()抛出异常:No value present Optional<Insurance> ofNullableOptionalNull = Optional.ofNullable(null); 3.2、不为null时,调get()不报错 Optional<Insurance> ofNullableOptionalNotNull = Optional.ofNullable(new Insurance());使用demo1: Optional<Insurance> insurance = emptyOptional.filter(t -> t.getName() == null); Optional<Insurance> insurance = emptyOptional.filter(t -> t.getName() != null); insurance.get();结果://调用get(),都会报错:No value present: 因为name字段不存在使用demo2:范例1:Optional<Insurance> insurance = ofInsurance.filter(t -> t.getName() == null); insurance.get(); 结果://不报错,因为name字段存在,且满足name==null范例2:Optional<Insurance> insurance = ofInsurance.filter(t -> t.getName() != null); insurance.get(); 结果://报错,因为name虽然存在,但是name!=null,不成立,会返回empty空,使用get()时就会抛出异常:No value present使用demo3: Optional<Insurance> insurance = ofNullableOptionalNull.filter(t -> t.getName() != null); Optional<Insurance> insurance = ofNullableOptionalNull.filter(t -> t.getName() == null); insurance.get();结果://都报错:ofNullableOptional,会走empty创建的Optional,字段不存在,直接get获取值为空,报错:No value present使用demo4:范例1:Optional<Insurance> insurance = ofNullableOptionalNotNull.filter(t -> t.getName() == null); insurance.get(); 结果://不会报错,走of方法创建的Optional,name字段存在,且name==null,所以不报错范例2:Optional<Insurance> insurance = ofNullableOptionalNotNull.filter(t -> t.getName() != null); insurance.get(); 结果://报错,因为虽然name存在,但是name!=null不成立,返回empty,get()就报错了filter,总结:使用时不确定是否为empty,所以直接使用ofNullableOptional创建Optional.四、map使用任何方式创建的Opional,map会将结果再包装成Optionla。范例: //map使用 Optional<String> stringOptional = ofNullableOptionalNotNull.map(t -> t.getName()); //有值则返回,没值则返回给定值 System.out.println(stringOptional.orElse("-1")); //判断值是否存在 System.out.println(stringOptional.isPresent()); //存在值,则打印输出,没有值,不打印。 stringOptional.ifPresent(System.out::println);五、flatMap不会将结果再包装成Optional范例:前置条件 @Data public class Cat { private Optional<Eat> eat; } @Data public class Eat { private String foodName; private Integer weight; }使用: //创建Optional对象 Optional<Cat> cat = Optional.ofNullable(new Cat()); //map将结果包装成Optional Optional<Optional<Eat>> eat = cat.map(c -> c.getEat()); //flatMap不会 Optional<Eat> eat1 = cat.flatMap(c -> c.getEat());map与flatMap区别:map将结果包装成Optional,flatMap不会。六、实列实列1、获取值之前判断值是否为空 public static String getInsuranceName(Insurance insurance) { Optional<String> optional = Optional.ofNullable(insurance).map(Insurance::getName); return optional.orElse("-1"); }实列2、根据用户拿到保险名字package com.example.study.java8.optional; import java.util.Optional; /** * 根据用户拿到保险名字 */ public class NullPointerException { public static void main(String[] args) { //肯定会包空指针异常 // String insuranceNameByPerson = getInsuranceNameByPerson(new Person()); //原始写法,做了空指针判断 String insuranceNameByCheckPerson = getInsuranceNameByCheckPerson(new Person()); // System.out.println(insuranceNameByCheckPerson); //Opional写法 String insuranceNameByOptional = getInsuranceNameByOptional(null); System.out.println(insuranceNameByOptional); } //Opional写法 public static String getInsuranceNameByOptional(Person person) { return Optional.ofNullable(person) .map(Person::getCar) .map(Car::getInsurance) .map(Insurance::getName) .orElse("-1"); } //flatMap不会把结果包装成Optional,map会。 //原始写法判断 public static String getInsuranceNameByCheckPerson(Person person) { if(null != person) { if(null != person.getCar()) { Car car = person.getCar(); if (null != car) { Insurance insurance = car.getInsurance(); if (null != insurance) { return insurance.getName(); } } } } return "UNKNOWN"; } public static String getInsuranceNameByPerson(Person person){ return person.getCar().getInsurance().getName(); } }
2022年09月15日
150 阅读
0 评论
3 点赞
1
2
3
...
6