DEV Community

1

Interfaces funcionais predefinidas

  • Os exemplos anteriores usaram interfaces funcionais definidas manualmente para ilustrar os conceitos básicos.

  • No entanto, o JDK 8 introduziu o pacote java.util.function, que fornece interfaces funcionais predefinidas para facilitar o uso.

Pacote java.util.function

  • Oferece diversas interfaces funcionais prontas para uso.

  • Reduz a necessidade de criar interfaces personalizadas.

Benefícios

  • Simplifica o desenvolvimento.

  • Padroniza o uso de interfaces funcionais em projetos.

  • Facilita a integração com APIs modernas do Java.

Image description

Uso da Interface Predicate

  • Define um método abstrato chamado test(T val).

  • Retorna true se o valor atender a uma condição ou restrição específica.

Exemplo de Uso

  • Implementa uma expressão lambda para verificar se um número é par.

  • A expressão lambda é atribuída a um objeto do tipo Predicate.

Funcionamento do Método test

  • Avalia o valor fornecido como argumento.

  • Retorna true se o número for par, caso contrário, retorna false.

Benefício

  • Permite usar condições dinâmicas de forma simples e reutilizável em expressões lambda.

// Usa a interface funcional interna Predicate.
// Importa a interface Predicate.
import java.util.function.Predicate;
class UsePredicateInterface {
public static void main(String args[])
{
// Esta expressão lambda usa Predicate<Integer> para
// determinar se um número é par.
Predicate<Integer> isEven = (n) -> (n %2) == 0;
if(isEven.test(4)) System.out.println("4 is even");
if(!isEven.test(5)) System.out.println("5 is odd");
}
}

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay