Search results for: 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

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

public int compareTo(Object obj) class Test { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add(“Python”); list.add(“Java”); list.add(“JavaScript”); list.add(“Ruby”); list.add(“Kotlin”); System.out.println(“Before sorting:”); System.out.println(list); // sorting the list Collections.sort(list); System.out.println(“After sorting:”); System.out.println(list); } } Output: Before sorting: [Python, Java, JavaScript, Ruby, Kotlin] After sorting: [Java, JavaScript, Kotlin, Python, Ruby] Sorting the array of…

Read More Comparable in Java

Queue queue1 = new LinkedList(); Queue queue2 = new PriorityQueue(); class Test { public static void main(String[] args) { Queue<String> queue = new LinkedList(); queue.offer(“Steve”); queue.offer(“Megan”); queue.offer(“Ryan”); queue.offer(“Melissa”); System.out.println(queue); } } Output: [Steve, Megan, Ryan, Melissa]   We can also use the add(Object o) method to add elements to the Queue. In this example, we will…

Read More Queue in Java

HashMap<K, V> map = new HashMap<>(); Map<Integer, String> map = new HashMap<>(); class Test { public static void main(String[] args) { Map<Integer, String> map = new HashMap<>(); map.put(1, “Java”); map.put(7, “Python”); map.put(10, “Ruby”); map.put(3, “Kotlin”); System.out.println(map); } } Output: {1=Java, 3=Kotlin, 7=Python, 10=Ruby} class Test { public static void main(String[] args) { Map<Integer, String> map…

Read More Map in Java