Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Challenge: Implement a Custom Predicate | Getting Started with Lambda Expressions
Lambda Expressions in Java

bookChallenge: Implement a Custom Predicate

Imagine you are building a product search feature for an online store. You want to help users quickly find products by filtering a list of product names. For instance, you might want to find all products that start with the letter "S". Instead of hardcoding the filtering logic, you decide to create a custom predicate interface that allows you to pass any filtering condition as a lambda expression. This makes your code flexible and reusable for different search criteria.

Aufgabe

Swipe to start coding

Implement a method to filter a list of strings using a custom predicate. The method should apply the predicate to each string and return a new list containing only the strings that match the predicate.

  • Iterate through each element in the items list.
  • For each element, check if it satisfies the predicate.
  • If the element satisfies the predicate, add it to a new list.
  • Return the new list containing all matching elements.

Lösung

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 6
single

single

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Suggested prompts:

Can you show me how to define a custom predicate interface in code?

How would I use a lambda expression with this predicate interface to filter products?

Can you give an example of filtering products that start with a specific letter?

close

bookChallenge: Implement a Custom Predicate

Swipe um das Menü anzuzeigen

Imagine you are building a product search feature for an online store. You want to help users quickly find products by filtering a list of product names. For instance, you might want to find all products that start with the letter "S". Instead of hardcoding the filtering logic, you decide to create a custom predicate interface that allows you to pass any filtering condition as a lambda expression. This makes your code flexible and reusable for different search criteria.

Aufgabe

Swipe to start coding

Implement a method to filter a list of strings using a custom predicate. The method should apply the predicate to each string and return a new list containing only the strings that match the predicate.

  • Iterate through each element in the items list.
  • For each element, check if it satisfies the predicate.
  • If the element satisfies the predicate, add it to a new list.
  • Return the new list containing all matching elements.

Lösung

Switch to desktopWechseln Sie zum Desktop, um in der realen Welt zu übenFahren Sie dort fort, wo Sie sind, indem Sie eine der folgenden Optionen verwenden
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 6
single

single

some-alt