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

public class MyCustomException extends RuntimeException { public MyCustomException(String message) { super(message); } } class Test { public static void main(String[] args) { Test test = new Test(); try { test.reserveSeats(8); } catch (MyCustomException e) { System.out.println(“Exception occurred with message: ” + e.getMessage()); } } private void reserveSeats(int numberOfSeats) { if (numberOfSeats > 5) { throw…

Read More Custom Exceptions in Java

throw new Exception(); class Test { public static void main(String[] args) { Test test = new Test(); test.reserveSeats(8); } private void reserveSeats(int numberOfSeats) { if (numberOfSeats > 5) { throw new IllegalArgumentException(“No more than 5 seats can be reserved.”); } else { System.out.println(“Reservation successful!”); } } } Output: Exception in thread “main” java.lang.IllegalArgumentException: No more…

Read More Difference Between throw and throws in Java

void methodName() throws Exception class Test { public static void main(String[] args) { } private void validateStatus(String status) { if (status.equals(“invalid”)) { throw new IOException(“Status of the file is invalid, the stream is closed!”); } } } class Test { public static void main(String[] args) { Test test = new Test(); test.validateStatus(“valid”); // OK test.validateStatus(“invalid”);…

Read More Java throws Keyword

throw new Exception(); class Test { public static void main(String[] args) { reserveSeat(new User(“Steve”, “Johnson”, false, 32)); // OK reserveSeat(new User(“Mike”, “Lawson”, true, 16)); // Exception will occur } private static void reserveSeat(User user) { if (user.getAge() < 18) { throw new IllegalArgumentException(“The user is under 18 years old!”); } } } class User {…

Read More Throw an Exception in Java

try { //Statements that may cause an exception } catch { //Handling exception } finally { //Statements to be executed } class Test { public static void main(String[] args) { try { System.out.println(20 / 0); // this will throw ArithmeticException } catch (ArithmeticException ae) { System.out.println(“ArithmeticException occurred!”); } finally { System.out.println(“Finally block…”); } } }…

Read More Java finally block

Java try-catch blocks are an essential part of Java programming, used for handling exceptions that might occur during program execution. When used correctly, try-catch blocks can help you write more robust and error-free code. In this tutorial, I will explain what Java try-catch blocks are, how they work, and how to use them effectively. I’ll…

Read More Java Try-Catch Blocks: A Comprehensive Guide

class Test { public static void main(String[] args) { long startTime = System.currentTimeMillis(); StringBuffer stringBuffer = new StringBuffer(“Java”); for (int i = 0; i < 8000000; i++) { stringBuffer.append(“Programming”); } System.out.println(“Time taken by StringBuffer: ” + (System.currentTimeMillis() – startTime) + “ms”); startTime = System.currentTimeMillis(); StringBuilder stringBuilder = new StringBuilder(“Java”); for (int i = 0; i…

Read More Difference between StringBuilder and StringBuffer classes