i have question about java function signature

Eric.L

I found that function interface and getMethod seem to be replaceable, What makes it work?

public class App {

    public static void main(String[] args) {

        Map<String, String> collect = Stream.of(new App(), new App(), new App())
                .collect(Collectors.toMap(App::getString, (app) -> "aaa"));

    }

    public String getString() {
        return "str";
    }
}

But when I use lambda to replace getMethod, it fails . Why this does not work

        Map<String, String> collect = Stream.of(new App(), new App(), new App())
                .collect(Collectors.toMap(() -> "str", (app) -> "aaa"));
Sweeper

Collectors.toMap requires a Function<? super T, ? extends K> as its first parameter, where T is the type of elements in the stream, and K is the key type of the map you want.

In this case, you have a stream of Apps and you want a Map<String, String>, so T is App and K is String. In other words, you need a function that accepts an App, and returns a String.

App::getString is such a Function<? super T, ? extends K>. You might be wondering why it accepts a App when getString accepts no parameters. Notice how getString is an instance method, and you are referring to it without an instance! A method reference of the form ClassName::instanceMethodName implicitly accepts an extra parameter of type ClassName, because you need an instance of that class to call it!

On the other hand, your lambda is not such a function. It accepts no parameters, as indicated by the empty brackets at the start (()). Your lambda expression would be represented by the Supplier<String> functional interface, not the Function<App, String> that you need.

To use a lambda expression here, simply do what you did to the second parameter of toMap, and add a lambda parameter:

.collect(Collectors.toMap((app) -> "str", (app) -> "aaa"));
//                         ^^^

Note that this is required even if you don't use app in the lambda expression.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

I have a question about printf() function

I have question about the Polymorphism in Java

I have a question about percentage change in java

I have a question about java polymorphism

Solidity Function Signature - question about part of signature

I have a question about the paramether of GetPriorityClass Function (and [in] attribute)

I Have A Question About While Loops and Writing One (In A Function) - Beginner

Hello, Im new to JavaScript and I have a question about isNan() function

Beginner's question about function signature

Hey, I have a question about assertions and mocking in java

I have a question about regexp pattern matching for java

Hello I am new to Scala, I have a question about an anonymous function problem

I have a strange question about array php

I have a question about ATR Bands in FineScript

I have a theoretical question about computer organization

I have a question about the effect of a sender channel

I have a question about webrtc implementation

I have a question about onChange event in React

I have question about group by how to use it?

I have a question about c with send a signal

I have a question about logstash grok filter

i have a question about nullable variable in kotlin

I have a question about Bubble Process in Javascript

I have question about component of Github project?

I have a question about postgreSQL cluster

I have a question about react array clearing

I have a question about python (for-loop)

I have a question about initializing the provider inside a function outside the 'Widget build(BuildContext context) {}.'

Question about syntax of a C function I found