Collectors groupingBy() method in Java with Examples
JAVA in with method Examples collectors
2023-09-27 14:26:38 时间
https://www.geeksforgeeks.org/collectors-groupingby-method-in-java-with-examples/
The groupingBy() method of Collectors class in Java are used for grouping objects by some property and storing results in a Map instance. In order to use it, we always need to specify a property by which the grouping would be performed. This method provides similar functionality to SQL’s GROUP BY clause.
Syntax:
public static Collector<T, ?, Map<K, List>> groupingBy(Function classifier)
Type Parameter: This method takes two type parameters:
- T- It is the type of the input elements.
- K- It is the type the input elements to be converted.
Parameters: This method accepts two mandatory parameters:
- Function- It is the property which is to be applied to the input elements.
- Classifier- It is used to map input elements into the destination map.
Return value: It returns a collector as a map.
Below is the program implementation of groupingBy() method:
Program 1:
- Java
// Java program to demonstrate // Collectors groupingBy() method import java.util.*; import java.util.function.Function; import java.util.stream.Collectors; public class GFG { public static void main(String[] args) { // Get the List List<String> g = Arrays.asList( "geeks" , "for" , "geeks" ); // Collect the list as map // by groupingBy() method Map<String, Long> result = g.stream().collect( Collectors.groupingBy( Function.identity(), Collectors.counting())); // Print the result System.out.println(result); } } |
Output:
{geeks=2, for=1}
相关文章
- Jenkins报错Caused: java.io.IOException: Cannot run program "sh" (in directory "D:JenkinsJenkins_homeworkspacejmeter_test"): CreateProcess error=2, 系统找不到指定的文件。
- Elasticsearch启动报错:Exception in thread “main“ java.nio.file.AccessDeniedException:
- Java -- 异常的捕获及处理 -- Java的异常处理机制
- 【LeetCode-面试算法经典-Java实现】【033-Search in Rotated Sorted Array(在旋转数组中搜索)】
- Java Script 节点的删除
- 【Java】题集
- JAVA-错误Several ports (8080, 8009) required by Tomcat v7.0 Server at localhost are already in use.
- Java 处理图片 base64 编码的相互转换
- java Eclipse的使用技巧
- 当传入数据只有一个时mybatis中<if>判断会出现There is no getter for property named 'subjectId' in 'class java.lang.Intege
- Java学习-090-解决 org.apache.ibatis.executor.ExecutorException: No constructor found in xxx
- IDEA 报错 Package 'java.util' is declared in module 'java.base', which is not in the module graph
- Tinking in Java ---Java的NIO和对象序列化
- Java打印输出:java在线8x8x
- java中jar命令打包一个文件夹下的所有文件
- 【JAVA】面试1——HashMap源码的深入 以及 SpringBoot的相关问题
- java 并发官方教程
- Understanding transient variables in Java and how they are practically used in HashMap---reference
- Implementing the skip list data structure in java --reference
- Java压缩图片,并粘贴到excel