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 […]
What are effective final variables in java, and where we can use it.
There is a final keyword, which is used to make a reference value permanent. It means – the value can not be changed once defined. Then there is effective final variable. These variables are not declared final. But, depending on the context they can be considered as final. What do i mean from a context? you should […]
How to use or implement test and negate methods in java.util.function.BiPredicate in java
We have Functional Interfaces in java, which returns a boolean-value after operating on it’s inputs. BiPredicate is one of those Functional interfaces, which returns a boolean value. It has four functions in it’s definition. Three of them are default and one is abstract and it is called test. boolean test( T input1, U input2) :- It returns a […]
How to use and implement apply and andThen functions provided by java.util.function.BinaryOperator in java
java.util.function.BinaryOperator is a Functional Interface, it has only one abstract method. It extends another Functional Interface java.util.function.BiFunction. It inherits apply and andThen methods from BiFunction, and adds two static factory methods maxBy and minBy. BinaryOperator is special case of BiFunction, where all the parameter types and return type are of the same data-type. This is how the method declaration looks like in […]
Learn how to implement java.util.function.BiFunction in java.
I am using java 11, you will need to modify the codes a bit to work in java below version 11. BiFunction is a Functional Interface , It has two methods in it, andThen and apply. The method andThen has a default implementation and apply method is an abstract. andThen :- This method is used to chain multiple BiFunction together. It helps to execute […]
How to use or implement java.util.function.BiConsumer interface in java.
BiConsumer is a Functional Interface . It has two methods, one abstract and other one is default method. void accept(T parameter1, U parameter2) :- It accepts two parameters and does not return anything. default BiConsumer<T, U> andThen(BiConsumer< ? super T, ? super U> applyThisAfterProcessing) :- This function takes one BiConsumer and returns another BiConsumer .The BiConsumer , which is passed in as an argument […]
How to use java.util.function.Consumer functional interface in java – And different implementations using class, lambdas and method reference
Consumer is present in java.util.functions package. It is a Functional Interface . In this article, we will use different ways to provide implementations for this interface. The Consumer implementation will take a String-input and write it to console. The System.out.println method can be replaced with other writers, like it can modified to write the contents to a file or […]
How to cache hashcodes in an immutable class.
We can keep a member variable to store hashcode for the created object. And we will return that variable whenever, a user asks for hashcodes. Here, in this article, we will use a class which is an immutable class. In the below class, I use hashcode instance variable to store the hashcode. Hashcode for a given object […]
Why an Immutable class is declared final
If you read my article – How to create an immutable class – I have mentioned there, to use final keyword to make your class immutable. But.. Why? An immutable class has only one version on all the system. Like String class in Java. You can not create another version of it. The Behaviour of String class is consistent on all […]
Using java streams to create immutable class
In my post – How to create immutable classes – to make lists immutable, I created a new List-Object everytime. This is the class which, we are going to modify in this tutorial. package immutable; import java.util.ArrayList; import java.util.Date; import java.util.List; final public class Person { private int age; private String name; private Date dateOfBirth; private […]