Java 8 – 收集器Collectors_实战

Java 8 – 收集器Collectors_实战文章目录

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全家桶1年46,售后保障稳定

文章目录


在这里插入图片描述


Code

public class CollectorsAction { 

public  static 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));
public static void main(String[] args) { 

testAveragingDouble();
testAveragingInt();
testAveragingLong();
testCollectingAndThen();
testCounting();
testGroupingByFunction();
testGroupingByFunctionAndCollector();
testGroupingByFunctionAndSupplierAndCollector();
testSummarizingInt();
testGroupingByConcurrentWithFunction();
testGroupingByConcurrentWithFunctionAndCollector();
testGroupingByConcurrentWithFunctionAndSupplierAndCollector();
testJoining();
testJoiningWithDelimiter();
testJoiningWithDelimiterAndPrefixAndSuffix();
testMapping();
testMaxBy();
testMinBy();
testPartitioningByWithPredicate();
testPartitioningByWithPredicateAndCollector();
testReducingBinaryOperator();
testReducingBinaryOperatorAndIdentiy();
testReducingBinaryOperatorAndIdentiyAndFunction();
testSummarizingDouble();
testSummarizingLong();
testSummarizingInt();
}
private static void testAveragingDouble() { 

System.out.println("testAveragingDouble");
Optional.ofNullable(menu.stream().collect(Collectors.averagingDouble(Dish::getCalories)))
.ifPresent(System.out::println);
}
private static void testAveragingInt() { 

System.out.println("testAveragingInt");
Optional.ofNullable(menu.stream().collect(Collectors.averagingInt(Dish::getCalories)))
.ifPresent(System.out::println);
}
private static void testAveragingLong() { 

System.out.println("testAveragingLong");
Optional.ofNullable(menu.stream().collect(Collectors.averagingLong(Dish::getCalories)))
.ifPresent(System.out::println);
}
private static void testCollectingAndThen() { 

System.out.println("testCollectingAndThen");
Optional.ofNullable(menu.stream().collect(Collectors.collectingAndThen(Collectors.averagingInt(Dish::getCalories), a -> "The Average Calories is->" + a)))
.ifPresent(System.out::println);
/* List<Dish> list = menu.stream().filter(d -> d.getType().equals(Dish.Type.MEAT)) .collect(Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList)); list.add(new Dish("", false, 100, Dish.Type.OTHER)); System.out.println(list);*/
}
private static void testCounting() { 

System.out.println("testCounting");
Optional.of(menu.stream().collect(Collectors.counting())).ifPresent(System.out::println);
}
private static void testGroupingByFunction() { 

System.out.println("testGroupingByFunction");
Optional.of(menu.stream().collect(Collectors.groupingBy(Dish::getType)))
.ifPresent(System.out::println);
}
private static void testGroupingByFunctionAndCollector() { 

System.out.println("testGroupingByFunctionAndCollector");
Optional.of(menu.stream().collect(Collectors.groupingBy(Dish::getType, Collectors.averagingInt(Dish::getCalories))))
.ifPresent(System.out::println);
}
private static void testGroupingByFunctionAndSupplierAndCollector() { 

System.out.println("testGroupingByFunctionAndSupplierAndCollector");
Map<Dish.Type, Double> map = menu.stream().collect(Collectors.groupingBy(Dish::getType, TreeMap::new, Collectors.averagingInt(Dish::getCalories)));
Optional.of(map.getClass()).ifPresent(System.out::println);
Optional.of(map).ifPresent(System.out::println);
}
private static void testSummarizingInt() { 

System.out.println("testSummarizingInt");
IntSummaryStatistics result = menu.stream().collect(Collectors.summarizingInt(Dish::getCalories));
Optional.of(result).ifPresent(System.out::println);
}
private 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);
}
private static void testGroupingByConcurrentWithFunctionAndCollector() { 

System.out.println("testGroupingByConcurrentWithFunctionAndCollector");
ConcurrentMap<Dish.Type, Double> collect = menu.stream()
.collect(Collectors.groupingByConcurrent(Dish::getType, Collectors.averagingInt(Dish::getCalories)));
Optional.ofNullable(collect).ifPresent(System.out::println);
}
private static void testGroupingByConcurrentWithFunctionAndSupplierAndCollector() { 

System.out.println("testGroupingByConcurrentWithFunctionAndSupplierAndCollector");
ConcurrentMap<Dish.Type, Double> collect = menu.stream()
.collect(Collectors.groupingByConcurrent(Dish::getType, ConcurrentSkipListMap::new, Collectors.averagingInt(Dish::getCalories)));
Optional.of(collect.getClass()).ifPresent(System.out::println);
Optional.ofNullable(collect).ifPresent(System.out::println);
}
private static void testJoining() { 

System.out.println("testJoining");
Optional.of(menu.stream().map(Dish::getName).collect(Collectors.joining()))
.ifPresent(System.out::println);
}
private static void testJoiningWithDelimiter() { 

System.out.println("testJoiningWithDelimiter");
Optional.of(menu.stream().map(Dish::getName).collect(Collectors.joining(",")))
.ifPresent(System.out::println);
}
private static void testJoiningWithDelimiterAndPrefixAndSuffix() { 

System.out.println("testJoiningWithDelimiterAndPrefixAndSuffix");
Optional.of(menu.stream().map(Dish::getName).collect(Collectors.joining(",", "Names[", "]")))
.ifPresent(System.out::println);
}
private static void testMapping() { 

System.out.println("testMapping");
Optional.of(menu.stream().collect(Collectors.mapping(Dish::getName, Collectors.joining(","))))
.ifPresent(System.out::println);
}
private static void testMaxBy() { 

System.out.println("testMaxBy");
menu.stream().collect(Collectors.maxBy(Comparator.comparingInt(Dish::getCalories)))
.ifPresent(System.out::println);
}
private static void testMinBy() { 

System.out.println("testMinBy");
menu.stream().collect(Collectors.minBy(Comparator.comparingInt(Dish::getCalories)))
.ifPresent(System.out::println);
}
private static void testPartitioningByWithPredicate() { 

System.out.println("testPartitioningByWithPredicate");
Map<Boolean, List<Dish>> collect = menu.stream().collect(Collectors.partitioningBy(Dish::isVegetarian));
Optional.of(collect).ifPresent(System.out::println);
}
private static void testPartitioningByWithPredicateAndCollector() { 

System.out.println("testPartitioningByWithPredicateAndCollector");
Map<Boolean, Double> collect = menu.stream()
.collect(Collectors.partitioningBy(Dish::isVegetarian, Collectors.averagingInt(Dish::getCalories)));
Optional.of(collect).ifPresent(System.out::println);
}
private static void testReducingBinaryOperator() { 

System.out.println("testReducingBinaryOperator");
menu.stream().collect(
Collectors.reducing(
BinaryOperator.maxBy(
Comparator.comparingInt(Dish::getCalories)
)
)
).ifPresent(System.out::println);
}
private static void testReducingBinaryOperatorAndIdentiy() { 

System.out.println("testReducingBinaryOperatorAndIdentiy");
Integer result = menu.stream()
.map(Dish::getCalories).collect(Collectors.reducing(0, (d1, d2) -> d1 + d2));
System.out.println(result);
}
private static void testReducingBinaryOperatorAndIdentiyAndFunction() { 

System.out.println("testReducingBinaryOperatorAndIdentiyAndFunction");
Integer result = menu.stream().collect(Collectors.reducing(0, Dish::getCalories, (d1, d2) -> d1 + d2));
System.out.println(result);
}
private static void testSummarizingDouble() { 

System.out.println("testSummarizingDouble");
Optional.of(menu.stream().collect(Collectors.summarizingDouble(Dish::getCalories)))
.ifPresent(System.out::println);
}
private static void testSummarizingLong() { 

System.out.println("testSummarizingLong");
Optional.of(menu.stream().collect(Collectors.summarizingLong(Dish::getCalories)))
.ifPresent(System.out::println);
}
private static void testSummarizingInt() { 

System.out.println("testSummarizingLong");
Optional.of(menu.stream().collect(Collectors.summarizingInt(Dish::getCalories)))
.ifPresent(System.out::println);
}
}

Jetbrains全家桶1年46,售后保障稳定

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/234940.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

  • deepfakes视频的网站_deepfakes视频的网站[通俗易懂]

    deepfakes视频的网站_deepfakes视频的网站[通俗易懂]{“optioninfo”:{“dynamic”:”true”,”static”:”true”},”simplifiedDisplay”:”newEdition”,”newCard”:[{“link”:”https://www.aliyun.com/product/live”,”icon”:”live”,”title”:”视频直播LIVE”,”des”:”视频直播(ApsaraVideoLive…

  • 服务器为什么要封海外,UDP攻击是什么「建议收藏」

    服务器为什么要封海外,UDP攻击是什么「建议收藏」为什么要封海外:总所周知,目前国内的大攻击大多都来自海外,因为国外的攻击成本比国内会低很多,一旦发起了攻击,并不容易找到攻击的源头。国外的家用带宽能达到千M口,咱们国内的百M口,相当于一只外国肉鸡能顶我们国内好几台肉鸡,那这个量是不得了的,而且国内的网站几乎很少有国外用户访问,目前封海外是国内的一大趋势。UDP攻击是什么:UDP攻击全称:UDP淹没攻击(UDPFloodAttack)。UDP淹没攻击是导致主机拒绝服务的一种攻击,属于带宽类攻击。UDP是一种无连接的协议,不需要用任何程序建立连接..

  • Do we need other languages other than C and C++?

    Do we need other languages other than C and C++?

  • 网络传真机——让传真,超越纸的限制[通俗易懂]

    网络传真机——让传真,超越纸的限制[通俗易懂]网络传真机,离你不再遥远!网络传真-真实号码版,超低价来袭,江苏电信倾力投资,立足江苏,面向全国!13万靓号任君选择。 (1)传真发送功能 点击写传真可进行单个号码发送传真也可多个号码…

  • 华为的JAVA面试题及答案(部分)

    华为的JAVA面试题及答案(部分)

    2021年11月17日
  • metasploit指令_msfconsole下载

    metasploit指令_msfconsole下载在MSF里面msfconsole可以说是最流行的一个接口程序。很多人一开始碰到msfconsole的时候就害怕了。那么多复杂的命令语句需要学习,但是msfconsole真的是一个强大的接口程序。Msfconsole提供了一个一体化的集中控制台。通过msfconsole,你可以访问和使用所有的metasploit的插件,payload,利用模块,post模块等等。Msfconsole还有第三方程序的…

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号