Predicates Flashcards
What are lambdas typically used for in java ?
Lambdas are typically used in java to provide an instance of a functional interface
Provide a definition of generics in Java, and illustrate it with an example of a generic interface. Then, demonstrate the usage of this generic interface in the main method with two different reference types !
Generics in java are a way for creating interfaces, classes and methods that work with any data type.
EXAMPLE :
package TEST;
**public interface CheckTrait<T>**{
boolean test(T a);
}</T>
Application
public static void main(String[] args) {
List<Animal> animals=new ArrayList<Animal>(); animals.add(new Animal("Lion",true,false)); animals.add(new Animal("Shark",false,true)); animals.add(new Animal("Tiger",true,false)); animals.add(new Animal("Dog",true,false)); animals.add(new Animal("Alligator",false,true)); **CheckTrait<String> c=s->s.startsWith("a");** System.out.println("Using Lambda expression for hoppers"); print(animals, a->a.canHop()); System.out.println("Using Lambda expression for swimmers"); print(animals, a->a.canSwim()); System.out.println("Using Generic Interface"); System.out.println(c.test("osterisk")); }
What is a placeholder type in the context of generics (give an example) !
A placeholder type in Java is a generic type used in generic interfaces, classes, and methods to enable them to work with different reference types
EXAMPLE :
T is a placeholder type enabling the CheckTrait interface to work with any data type in the following example “public **interface CheckTrait<T>**{
boolean test(T a);
}</T>
Give a particularity of placeholders when it comes to using lambda expressions with functional interfaces with giving a clear example !
When instantiating a generic functional interface, you typically specify the parameter type between angle brackets to match the type expected by the generic functional interface
in the following line :
**CheckTrait<String> c=s->s.startsWith("a");**</String>
we specified the type String between angle brackets in order for the following generic interface to be expecting a String in the placeholder type :
**public interface CheckTrait<T>**{
boolean test(T a);
}</T>
Give an example where a generic class and method are declared and used !
EXAMPLE :
package TEST;
**public class Box<T>**{
private T item;</T>
public void setItem(T item){ this.item=item; } **public T getItem()**{ return this.item; } **public <T> T findFirst(T[] liste)**{ for(T item:liste){ return item; } return null; } **public static void main(String[] args)** { Box<String> B=new Box<>(); B.setItem("TEST"); String s1=B.getItem(); System.out.println(s1); Integer[] liste={55,30,22}; Integer s2=new Box<String>().findFirst(liste); System.out.println(s2); } }
KEY RULE :
In Java, generics work only with reference types
Give the definition of a Predicate in java with specifying which package it belongs to and what common problem it resolves !
A Predicate is a generic functional interface from the java.util.function package that takes an argument of type T and return a boolean
The common problem solved by Predicates is having to create a functional interface each time we want to work with a different reference type
Using the first example which showcase the status quo before Predicates, demonstrate how predicate is important and how it is used !
package TEST;
import java.util.*;
import java.util.function.Predicate;
public class TraditionalSearch{
static void print(List<Animal> animals,Predicate<Animal> check){ for(Animal a:animals){ if(check.test(a)) System.out.println(a.toString()); } } **public static void main(String[] args)** { List<Animal> animals=new ArrayList<Animal>(); animals.add(new Animal("Lion",true,false)); animals.add(new Animal("Shark",false,true)); animals.add(new Animal("Tiger",true,false)); animals.add(new Animal("Dog",true,false)); animals.add(new Animal("Alligator",false,true)); System.out.println("Using Predicate for swimmers"); print(animals, a->a.canSwim()); System.out.println("---------------------------------------------"); System.out.println("Using Predicate for hoppers"); print(animals, a->a.canHop()); } }
The provided example demonstrates the utility of Predicate by showing how it can be used to work with different data types without needing to create multiple functional interfaces
KEY TAKEAWAY:
The Predicate interface in java looks like the following :
**public interface Predicate<T>**{
boolean test(T t);
}</T>
Remind me with an example of the one method that you need to know for the exam and that takes a Predicate as an argument as well as which class it belongs to !
The removeIf method that belongs to the ArrayList java class takes a Predicate as an argument and removes all elements from the list that satisfy the given Predicate
EXAMPLE :
package TEST;
import java.util.*;
import java.util.function.Predicate;
public class TEST {
**public static void main(String[] args)** { ArrayList<String> liste=new ArrayList<>(); Predicate<String> p=s->s.startsWith("C"); liste.add("alibaba"); liste.add("Carpe diem"); liste.add("Relentless"); liste.add("africa"); liste.add("Coca"); System.out.println(liste);/*[alibaba, Carpe diem, Relentless, africa, Coca]*/ liste.removeIf(s->s.startsWith("a")); System.out.println(liste);/*[Carpe diem, Relentless, Coca]*/ liste.removeIf(p); System.out.println(liste);/*[Relentless]*/ } }