Sitemap

JAVA DAY-4

2 min readDec 18, 2025

--

All Questions to run

1. Function<T, R>

Definition

@FunctionalInterface
public interface Function<T, R> {
R apply(T t);
}

🔹 Takes one input and returns one output

Example 1: Verify name starts with ‘R’

Function<String, Boolean> fn = str -> str.startsWith("R");
System.out.println(fn.apply("Ravi")); // true

Example 2: Retrieve name & salary from Employee

record Employee(Integer id, String name, Double salary) {}
Employee e1 = new Employee(101, "Scott", 80000.0);Function<Employee, String> f1 = emp -> emp.name();
Function<Employee, Double> f2 = emp -> emp.salary();
System.out.println("Name: " + f1.apply(e1));
System.out.println("Salary: " + f2.apply(e1));

2. Supplier

Definition

@FunctionalInterface
public interface Supplier<T> {
T get();
}

🔹 Takes no input and returns a value

Example 1: Simple Supplier

Supplier<String> s1 = () -> "Java";
System.out.println(s1.get());

Example 2: Supplier with Object

record Player(Integer id, String name) {}
Supplier<Player> s2 = () -> new Player(18, "Virat");
System.out.println(s2.get());

Example 3: Supplier with User Input

record Product(Integer id, String name, Double price) {}
Supplier<Product> s3 = () -> new Product(1, "Laptop", 90000.0);
System.out.println(s3.get());

3. Custom Functional Interface

Example

@FunctionalInterface
interface MyInterface<T, U, V, R> {
R myApply(T a, U b, V c);
}
MyInterface<Integer, Integer, Integer, String> fn = (a, b, c) -> a + b + c + "";
System.out.println(fn.myApply(100, 200, 300));

4. BiPredicate<T, U>

Definition

@FunctionalInterface
public interface BiPredicate<T, U> {
boolean test(T t, U u);
}

Example 1: Name length check

BiPredicate<String, Integer> bp = (str, len) -> str.length() == len;
System.out.println(bp.test("Ravi", 4));

Example 2: Sum is even

BiPredicate<Integer, Integer> isEven = (a, b) -> (a + b) % 2 == 0;
System.out.println(isEven.test(2, 3));

5. BiConsumer<T, U>

Definition

@FunctionalInterface
public interface BiConsumer<T, U> {
void accept(T t, U u);
}

Example

BiConsumer<Integer, String> bc = (num, str) -> {
num += 2;
str = str.toUpperCase();
System.out.println(num + " " + str);
};
bc.accept(10, "nit");

6. BiFunction<T, U, R>

Definition

@FunctionalInterface
public interface BiFunction<T, U, R> {
R apply(T t, U u);
}

Example 1: Concatenate Strings

BiFunction<String, String, String> bf = (a, b) -> a + b;
System.out.println(bf.apply("Hello", "Java"));

Example 2: Total length

BiFunction<String, String, Integer> len = (a, b) -> a.length() + b.length();
System.out.println(len.apply("Hello", "Java"));

7. UnaryOperator

Definition

@FunctionalInterface
public interface UnaryOperator<T> extends Function<T, T> {}

Example

UnaryOperator<Integer> square = x -> x * x;
System.out.println(square.apply(5));

8. BinaryOperator

Definition

@FunctionalInterface
public interface BinaryOperator<T> extends BiFunction<T, T, T> {}

Example

BinaryOperator<Integer> add = (a, b) -> a + b;
System.out.println(add.apply(3, 5));

9. Important Interview Rule

❌ We cannot override Object class methods (like hashCode()) as default methods inside interfaces.

Get Aman Verma’s stories in your inbox

Join Medium for free to get updates from this writer.

Reason: Causes ambiguity with Object class methods.

--

--

Aman Verma
Aman Verma

Written by Aman Verma

📚#Software Engineer || Ai Technolgy || #ReadingIsLife || #techylife https://blog-project-one-silk.vercel.app/