search
HomeJavaLamba function not working in java groupingBy method (via Collectors interface)

php editor Strawberry will introduce to you the role of Lamba function in Java's groupingBy method in this article. groupingBy is a method for grouping implemented through the Collectors interface. However, the Lamba function does not work in this method. Next, we will detail why the Lamba function cannot be used in the groupingBy method and provide some alternatives. Let’s explore together!

Question content

I created my first map where the keys are the string slots of the PE classes and the values ​​are the number of participants per slot. Now I want to create a second map with the keys and values ​​basically reversed, so the key will be the number of participants and the value will be the list of slots with that number of participants. I wrote this code but I don't know why it's complaining about my lambda function in the second java stream... I literally spent an hour thinking about it and I'd really appreciate any help! Thanks in advance, here's the code:

public SortedMap<Integer, List<String>> slotsPerNofParticipants(String gymnname) throws FitException{
        // map with key = Slot   value = number of participants for the slot
        Map<String, Long> map = this.reservationsColl.stream().filter(r->r.getGym().getName().equals(gymnname))
        .collect(Collectors.groupingBy(r->r.getDayslotStringFormat(),
                Collectors.counting() ));
        // map with key = number of participants  value = list of slot with that number of participants
        Map<Integer,List<String>> res = map.entrySet().stream()
                .collect(Collectors.groupingBy(
                        e->e.getValue.intValue(),
                        Collectors.mapping(e->e.getKey, Collectors.toList() ) ) );
        
        return null;
    }

As you can see, I'm trying to convert the value of the first map (long) to an integer type key using a lambda function... I don't know why it's complaining about the lambda function... The error message is: "getkey cannot be parsed or is not a field" "getvalue cannot be parsed or is not a field"

Solution

There is a problem here. You have some typos.

  • getvalue should be getvalue()
  • getkey should be getkey()

UncorrectedCode

Map<Integer,List<String>> res = map.entrySet().stream()
                .collect(Collectors.groupingBy(
                        e->e.getValue.intValue(),
                        Collectors.mapping(e->e.getKey, Collectors.toList() ) ) );

Any other questions caused by objects should include a declaration of the class and some sample data.

should include input and expected output.

The above is the detailed content of Lamba function not working in java groupingBy method (via Collectors interface). For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:stackoverflow. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software