Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

155
Visualizações
java use function as parameter and only care about the return value of it

How to implement this function below

public T myFunction(Function<T> func)  //T is the return type
{
  //...some code
  return func();
}

And I can use the function like below, and don't need to declare parameters a, b, c in myFunction

myType result = myFunction(() -> doSomething(a, b , c))
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

The right functional interface to use then is Supplier<T>. It doesn't have an "input" type parameter:

public <T> T myFunction(Supplier<T> func) {
    // myFunction logic 
    return func.get();
}

And the invocation will be just as you have it, assuming doSomething() has myType as return type.

over 4 years ago · Santiago Trujillo Relatório

0

Unfortunately, Java is not a "Functional" language. So when you define a method like myFunction(Function<T> func) you specified, myFunction requires an Object that extends the Interface Function. Function in java declares a single method R apply(T t) in this case, Function takes a single argument. There are additional Interfaces that are part of the Java SDK such as BiFunction which has an apply method that takes two arguments. If you need a method that will take a "Function" requiring three parameters you will have to declare an Interface with those specifics. Or you could use something like. Apache Commons TriFunction

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda