首页
关于
友链
Search
1
wlop 4K 壁纸 4k8k 动态 壁纸
1,470 阅读
2
Nacos持久化MySQL问题-解决方案
932 阅读
3
Docker搭建Typecho博客
752 阅读
4
滑动时间窗口算法
728 阅读
5
Nginx反向代理微服务配置
699 阅读
生活
解决方案
JAVA基础
JVM
多线程
开源框架
数据库
前端
分布式
框架整合
中间件
容器部署
设计模式
数据结构与算法
安全
开发工具
百度网盘
天翼网盘
阿里网盘
登录
Search
标签搜索
java
javase
docker
java8
springboot
thread
spring
分布式
mysql
锁
linux
redis
源码
typecho
centos
git
map
RabbitMQ
lambda
stream
少年
累计撰写
189
篇文章
累计收到
24
条评论
首页
栏目
生活
解决方案
JAVA基础
JVM
多线程
开源框架
数据库
前端
分布式
框架整合
中间件
容器部署
设计模式
数据结构与算法
安全
开发工具
百度网盘
天翼网盘
阿里网盘
页面
关于
友链
搜索到
3
篇与
的结果
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日
137 阅读
0 评论
4 点赞
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日
131 阅读
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日
163 阅读
0 评论
5 点赞