Java ThreadLocal Interview questions & answers in detail with code focusing more on when to use it and how to not abuse it. Q1. What is a ThreadLocal class? When to use it? How to not abuse it? A1. … Read more ›...
Java ThreadLocal Interview questions & answers in detail with code focusing more on when to use it and how to not abuse it. Q1. What is a ThreadLocal class? When to use it? How to not abuse it? A1. … Read more ›...
Tip #1: Tightly encapsulate your classes. A class generally contains data as well as methods, and is responsible for the integrity of its own data. The standard way to protect the data is to make it private, so that no other class can get direct access to it, … Read more...
Q1. Explain “pull” vs “push” paradigms (aka imperative/interactive vs reactive) with respect to processing data in programming? A1. Interactive/imperative Programming (Pull): is all about asking for something and getting it in return. … Read more ›...
Q01. What is a monad? A01. Monad is a wrapper (or a context) that flattens the result so that it can be further processed by mapping over each element. Everything that’s a monad is a functor with extra operation to flatten the results with “flatMap” … Read more ›...
Basic level Java Executor framework Interview Q&As with Future & CompletableFuture (introduced in Java 8), which you would be using very often in Java apps. Q01. What is an Executor Framework? A01.In Java 5, Executor framework was introduced with the java.util.concurrent. … Read more ›...
This extends Java FP – CompletableFuture monadic chaining with examples – part 1. f and g are processed asynchronously, and then combined A “Supplier” returns a result by taking nothing as an argument.
|
1 2 |
Supplier: () -> T |
“CompletableFuture.supplyAsync” executes a “Supplier” without blocking. This means executes it asynchronously. … Read more ›...
Q1 If Java did not have a Stack implementation, how would you go about implementing your own? A1 1) Determine the backing data structure (e.g. array, linked list, etc). 2) Determine the methods that need to be implemented like pop( ), … Read more ›...
A trivial coding example (i.e. a Calculator) tackled using the following programming paradigms in Java not only to perform well in coding interviews, but also to learn these programming paradigms.
Approach 1: Procedural Programming
Approaches 2 – 4: Object Oriented Programming
Approach 5: Functional Programming (Java 8)
Approach 1: Procedural
|
1 2 3 |
public interface Calculate { abstract int calculate(int operand1, int oerand2, Operator operator); } |
|
1 2 3 |
public enum Operator { ADD, SUBTRACT, DIVIDE, MULTIPLY; } |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
public class CalculateImpl implements Calculate { @Override public int calculate(int operand1, int operand2, Operator operator) { switch (operator) { case ADD: return operand1 + operand2; case SUBTRACT: return operand1 - operand2; case MULTIPLY: return operand1 * operand2; case DIVIDE: return operand1 / operand2; } throw new RuntimeException(operator + "is unsupported"); } } |
|
1 2 3 4 5 6 7 8 9 10 11 |
public class CalculatorTest { public static void main(String[] args) { Calculate calc = new CalculateImpl(); int result = calc.calculate(5,6,Operator.ADD); result = calc.calculate(result,6,Operator.MULTIPLY); result = calc.calculate(result,1,Operator.SUBTRACT); result = calc.calculate(result,5,Operator.DIVIDE); System.out.println("result=" + result); } } |
Output: result=13
Approach 2: OOP
|
1 2 3 |
public interface MathCommand<E> { abstract E execute(E operand1, E operand2); } |
|
1 2 3 4 5 6 7 |
public class AddCommand implements MathCommand<Integer>{ @Override public Integer execute(Integer operand1, Integer operand2) { return operand1 + operand2; } } |
|
1 2 3 4 5 6 7 |
public class SubtractCommand implements MathCommand<Integer>{ @Override public Integer execute(Integer operand1, Integer operand2) { return operand1 - operand2; } } |
|
1 2 3 4 5 6 7 |
public class MultiplyCommand implements MathCommand<Integer>{ @Override public Integer execute(Integer operand1, Integer operand2) { return operand1 * operand2; } } |
|
1 2 3 4 5 6 7 |
public class DivideCommand implements MathCommand<Integer>{ @Override public Integer execute(Integer operand1, Integer operand2) { return operand1 / operand2; } } |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public class CalculatorTest2 { public static void main(String[] args) { MathCommand<Integer> command = new AddCommand(); Integer result = command.execute(5, 6); command = new MultiplyCommand(); result = command.execute(result, 6); command = new SubtractCommand(); result = command.execute(result, 1); command = new DivideCommand(); result = command.execute(result, 5); System.out.println("result=" + result); } } |
When you have more mathematical operations,
…
handle(…) returns a result or an exception A dummy exception is thrown from “f” to demonstrate an exceptional scenario. The “java.util.concurrent.ExecutionException” potentially thrown by f.get(), g.get(), and h.get() is a checked exception. Hence catching it and re throwing as an unchecked (i.e. RuntimeException) exception.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
package com.fp.eg7; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; public class CompletableFutureChainingMain { public static void main(String[] args) throws InterruptedException, ExecutionException { CompletableFuture<String> f = CompletableFuture.supplyAsync(() -> { // non-blocking delay(6); if(1==1) { throw new RuntimeException("Dummy error for testing..."); } return "Hello"; }); CompletableFuture<String> g = CompletableFuture.supplyAsync(() -> { // non-blocking delay(6); return " World"; }); CompletableFuture<String> h = CompletableFuture.supplyAsync(() -> { // non-blocking delay(6); return " !!!!!!"; }); //combine the results CompletableFuture<String> j = CompletableFuture.supplyAsync(() -> { try { return f.get() + g.get() + h.get(); // blocking calls } catch (Exception e) { throw new RuntimeException(e); } }).handle((r,e) -> r != null ? r : e.getMessage() ); System.out.println("Reached here..."); // printed immediately System.out.println(j.get()); // blocked } private static final void delay(long delayBy) { try { TimeUnit.SECONDS.sleep(delayBy); } catch (InterruptedException e) { e.printStackTrace(); } } } |
Output “Reached here…” … Read...
Java 5 introduced the concurrent package for more efficient multi-threading.
Q. What is the difference between Future and FutureTask in asynchronous processing?
A. Future is the interface and FutureTask is the base implementation of the Future with methods to start and cancel a computation.
…
List, Set, Map, and Queue(access the ends FIFO or LIFO) are the basic Java data structure interfaces for which there are different implementations to cater for different usage patterns. Java data structure interview questions are very popular, … Read more ›...
Q1. What is the purpose of a Map interface in Java collection API? A1. A map is a set of associations between pairs of objects. One is known as a “key” and the other is known as a “value”. It is used very frequently in programming in case where for...
Q1. What is the difference between “ExecutorService” and “Fork/Join Framework“?
A1. The Fork/Join framework uses a special kind of thread pool known as the ForkJoinPool, which is a specialized implementation of ExecutorService implementing the 1) work-stealing algorithm,
…