When pass an object to a function, then there is a scenario which always arises. That, the passed object could be null. And, we have to write logic to react to that null-condition. And, generally that logic is hard-coded in the function. And, there are times as a user I feel that, instead of throwing an exception […]
java.util.function.Function implementation using modern java.
In this blog, I will show you the different ways to implement java.util.function.Function interface in modern java. You can watch me code these examples on youtube. First implementation is using implementation keyword in a regular class private class FuncImpl implements Function<Person, Integer>{ @Override public Integer apply(final Person person){ return person.getAge(); } } Second way uses anonymous class for implementation. […]
Convert infix equation to postfix in java
We know stack is first in and last out. The first to go inside a stack is going to be the last to come out of it. So, the aloglrithm to convert a infix to postfix is simple. We print all the operands. Operands are something which are not mathematical symbols and does not represent […]
Wait, notify and notifyall. What is the difference between notify and notifyall.
Q1 :- How does Java maintains a record of threads waiting on an object? Ans :- Each object has a waiting set. Every time a thread calls wait on an object, that thread gets added in that waiting-set. So, a Set datastructure is used to maintain the record of waiting threads in java. Q2 :- What is the difference […]
Using anonymous class to implement runnable interface in java
You can read about the other ways for implementing runnable interface – using a java class, and using lambda. In this part, we will use anonymous class for doing this. In my opinion, at the time of writing this article, the anonymous class is pretty useless now. If you need an anonymus class then use […]
How to implement java.util.function.DoubleToLongFunction
java.util.function.DoubleToLongFunction is a primitive specialization of Other Functional Interfaces. What it means is , we can get the same functionality using other functional interfaces, but they are going to be slow. Because of boxing and unboxing. If you are dealing with Double and Long wrapper objects then use other functional interface. So, this functional interface is […]
Using java lambda to provide implementation for Runnable interface.
There are many ways to create a thread. One of those ways is implementin Runnable interface. Again there are several ways to provide an implementation for it. One of those ways is, using lambda to provide an implementation. If you don’t know anything about lambda, then you can read it here. You are gonna need […]
Implement runnable interface using a class in java.
Runnable is a Functional Interface. It has one abstract method called run. To make use of this class, we need to provide an implementation for this method. In practice, compiler only needs a non-abstract method. You can provide a empty body and that will be enough. The implementation need not be meaningful. Here is a simple class, […]
using Anonymous class to extend thread class
In this article, I wrote about extending a Thread class to create a Thread in java. We can also use anonymous class to create a Thread. So, this article will cover the part, where we use anonymous class to provide the thread implementation. If you don’t know, when we declare an anonymous class, it is […]
Extending Thread Class to create threads in java.
To create Thread, we basically need to provide an implementation for run function. We can do it in two ways. Either we can extend Thread class and override it’s run method or we can implement Runnable interface and provide an implementation for it’s abstract run method. In this article, I am extending Thread class. public class SimpleThread extends Thread { } We have […]