Home > Java > Lamba function not working in java groupingBy method (via Collectors interface)

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

PHPz
Release: 2024-02-11 08:15:07
forward
652 people have browsed it

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;
    }
Copy after login

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() ) ) );
Copy after login

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!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template