java

@FunctionalInterface interface MyInterface { Message send(String message); } class Message { public Message(String message) { System.out.println(message); } } class Test { public static void main(String[] args) { MyInterface myInterface = Message::new; myInterface.send(“Hello!”); } } Output: Hello! class Message { public Message(String message) { System.out.println(message); } public Message(String message1, String message2) { System.out.println(message1 + message2); }…

Read More Constructor Reference in Java

@FunctionalInterface interface Drawable { void draw(); } class Test { public void drawCircle() { System.out.println(“Drawing circle…”); } public static void main(String[] args) { Test test = new Test(); Drawable drawable = () -> test.drawCircle(); drawable.draw(); } } Output: Drawing circle… @FunctionalInterface interface Drawable { void draw(); } class Test { public void drawCircle() { System.out.println(“Drawing…

Read More Method Reference in Java

@FunctionalInterface public interface Supplier<T> { /** * Gets a result. * * @return a result */ T get(); } class Test { public static void main(String[] args) { Supplier<String> supplier = () -> “Java”; System.out.println(supplier.get()); } } Output: Java class User { private String name; private String username; private String membershipType; private String address; public…

Read More Supplier Functional Interface in Java

@FunctionalInterface public interface BinaryOperator<T> extends BiFunction<T,T,T> { /** * Returns a {@link BinaryOperator} which returns the lesser of two elements * according to the specified {@code Comparator}. * * @param <T> the type of the input arguments of the comparator * @param comparator a {@code Comparator} for comparing the two values * @return a {@code…

Read More BinaryOperator Functional Interface in Java

@FunctionalInterface public interface UnaryOperator<T> extends Function<T, T> { /** * Returns a unary operator that always returns its input argument. * * @param <T> the type of the input and output of the operator * @return a unary operator that always returns its input argument */ static <T> UnaryOperator<T> identity() { return t -> t;…

Read More UnaryOperator Functional Interface in Java

@FunctionalInterface public interface BiPredicate<T, U> { /** * Evaluates this predicate on the given arguments. * * @param t the first input argument * @param u the second input argument * @return {@code true} if the input arguments match the predicate, * otherwise {@code false} */ boolean test(T t, U u); /** * Returns a…

Read More BiPredicate Functional Interface in Java

@FunctionalInterface public interface Predicate<T> { /** * Evaluates this predicate on the given argument. * * @param t the input argument * @return {@code true} if the input argument matches the predicate, * otherwise {@code false} */ boolean test(T t); /** * Returns a composed predicate that represents a short-circuiting logical * AND of this…

Read More Predicate Functional Interface in Java

@FunctionalInterface public interface BiConsumer<T, U> { /** * Performs this operation on the given arguments. * * @param t the first input argument * @param u the second input argument */ void accept(T t, U u); /** * Returns a composed {@code BiConsumer} that performs, in sequence, this * operation followed by the {@code after}…

Read More BiConsumer Functional Interface in Java

@FunctionalInterface public interface Consumer<T> { /** * Performs this operation on the given argument. * * @param t the input argument */ void accept(T t); /** * Returns a composed {@code Consumer} that performs, in sequence, this * operation followed by the {@code after} operation. If performing either * operation throws an exception, it is…

Read More Consumer Functional Interface in Java

Java has evolved over the years to support functional programming, which is a programming paradigm that emphasizes writing code in terms of functions. In Java, functional programming is made possible with the help of functional interfaces, which are interfaces that define a single abstract method. Functional interfaces can be implemented using lambda expressions or method…

Read More Master functional interfaces in Java for efficient code

Lambda expressions are one of the most significant additions to the Java programming language in recent years. They were introduced in Java 8 and are a way to write more concise and expressive code. Lambda expressions allow you to define and pass around blocks of code, known as functional interfaces, making it easier to write…

Read More Master Lambda Expressions and Enhance Your Java Programming

class Test { public static void main(String[] args) { List<Integer> integers = new ArrayList<>(Arrays.asList(1, 2, 3, 3, 4, 5, 6, 7, 7, 8, 9, 9, 10)); List<Integer> resultList = new ArrayList<>(); for (Integer integer : integers) { if (!resultList.contains(integer)) { resultList.add(integer); } } System.out.println(resultList); } } Output: [1, 2, 3, 4, 5, 6, 7, 8,…

Read More Imperative VS Declarative Programming Part 2