首页 > 编程语言 >Collectors groupingBy() method in Java with Examples

Collectors groupingBy() method in Java with Examples

时间:2022-11-27 03:22:15浏览次数:62  
标签:Java groupingBy Collectors java Examples input method

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: 

  <iframe data-google-container-id="2" data-is-safeframe="true" data-load-complete="true" frameborder="0" height="250" id="google_ads_iframe_/27823234/GFG_Desktop_PostContent_336x280_0" marginheight="0" marginwidth="0" scrolling="no" src="https://1a836bd7e66e1e79d593f05cf2234024.safeframe.googlesyndication.com/safeframe/1-0-40/html/container.html" title="3rd party ad content" width="300"></iframe>
  • 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}

 

标签:Java,groupingBy,Collectors,java,Examples,input,method
From: https://www.cnblogs.com/kungfupanda/p/16928907.html

相关文章

  • Java 8 | Collectors counting() with Examples
    https://www.geeksforgeeks.org/java-8-collectors-counting-with-examples/Collectorscounting() methodisusedtocountthenumberofelementspassedinthestr......
  • Stream In Java
    https://www.geeksforgeeks.org/stream-in-java/ IntroducedinJava8,theStreamAPIisusedtoprocesscollectionsofobjects.Astreamisasequenceofobje......
  • Comparable vs Comparator in Java
    https://www.geeksforgeeks.org/comparable-vs-comparator-in-java/ Javaprovidestwointerfacestosortobjectsusingdatamembersoftheclass:  Comparabl......
  • equals() and hashCode() methods in Java
    https://www.geeksforgeeks.org/equals-hashcode-methods-java/ Java.lang.objecthastwoveryimportantmethodsdefined:publicbooleanequals(Objectobj)andp......
  • enum in Java
    https://www.geeksforgeeks.org/enum-in-java/Enumerationsservethepurposeofrepresentingagroupofnamedconstantsinaprogramminglanguage.Forexample,t......
  • Generics in Java
    https://www.geeksforgeeks.org/generics-in-java/ Generics means parameterizedtypes.Theideaistoallowtype(Integer,String,…etc.,anduser-defined......
  • java发送邮件
    一邮件协议收发邮件具有与HTTP协议相同的邮件传输协议.SMTP:(SimpleMailTransferProtocol,简单邮件传输协议)发邮件协议POP3:(PostOfficeProtocolVersion3,邮局协议第......
  • Java中YYYY-MM-dd在跨年时出现的bug
    先看一张图:Bug的产生原因:日期格式化时候,把yyyy-MM-dd写成了YYYY-MM-dd Bug分析:当时间是2019-08-31时,publicclassDateTest{publicstaticvoidmain(Strin......
  • java中乐观锁CAS的实现探究——AtomicInteger
    CASCAS——compareandswap,比较并交换。是一种乐观锁的实现方式。更新操作时,在每次更新之前,都先比较一下被更新的目标T,值是不是等于读取到的旧值O,如果相等,则认为在读取......
  • JAVA网络编程TCP实现聊天功能,附在IDEA中同时运行2个或以上相同的java程序
    在IDEA中同时运行2个或以上相同的java程序在日常编写测试代码时,有时候会需要在idea上同时运行两个及以上相同的java程序,如:想运行两个CLIENTLOGIN测试聊天室效果。1.点击E......