Java8 - Functional interfaces

Functional interfaces
Java8 has added to it's kitty another concept know as functional interfaces

So what exactly is an functional interfaces?
Functional interfaces are interfaces with exactly a single abstract method (SAM). It is represented by the annotation type @FunctionalInterfaces. This annotatio  let's the compiler know that the interfaces should adhere to the contract of SAM.
Note a couple of things here that this interface can also have other default functions but not another abstract method. These interfaces can also be extended by another interfaces and can be called functional interface as long it does not have another abstract method.
Also compiler will treat all SAM interfaces as functional regardless if the annotation is present or not.

So how do we use this interfaces?
These interfaces can be represented using lamda expressions, method reference and constructor reference.(Lamda expressions another important concept I have covered in another blog article)


Java8 - Lamda functions

Java8 - Lamda expressions

What are Lamda expressions?
Lamda expressions is Java's introduction to functional programming.  Lamda expressions can be created and passed around like regular objects and executed when needed.

Why do we have Lamda expressions?
Java is often criticised as verbose, with Lamda expressions there is an attempt to decrease this verbosity. They address the bulkiness of the anonymous inner classes.

With lamda expression you can now represent single method interfaces in a simple and concise way. These single method interfaces are often termed as functional interfaces as in Java8. If you do not know about functional interfaces check my other post on the same.

So how do we create these Lamda expression?
Lamda expressions are composed of 3 parts:
1. Argument list
     It is represented like any other method definition like for example () or (int x, int y) etc 2. Arrow token
    Represented by the -> symbol 3. Body
    If the body consists of a single line then curl…

Am back...Should we start blogging about Java 8 and more

Its been long time since my last post and I thought I should check with readers if you are doing good and found my blog useful.

Did you like the content presentation style? Anything you would like me to change etc.

Its been long and Java has moved on from Java5 to Java8 and now Java10. I would love to start blogging again and pen down my these concepts in a way everyone understands the why, what and how.

Let me know in comments or email me at on what concepts you feel hard to understand and I will be glad to help out and blog about it.

Till then, happy learning.

How does HashMap work in Java

We all know and must have used HashMap, its a Map interface implementation. It stores keys and its corresponding value. Keys cannot contain duplicates and can contain at the most one null key (HashTable does not allow null as key, FYI). It has non synchronized methods unlike HashTable. It does not guarantee the order of retrieval (it changes every time the HashMap is modified) etc etc. Enough of this now..

So how does HashMap work?
Basically when this question is asked it generally means how is the object stored and retrieved from the     HashMap. But of course with the get and put methods in the Map API. Easy isn't it, but that is not what
meets the eye. We need to understand what goes inside to understand the answer to the title of this topic.

So what happens when you put a key/value pair in the HashMap, Firstly, hashCode of the key is retrieved and supplied to hash function to defend against poorly constructed hashCode implementation; to get a new hash value. This value is then …

Immutable classes

An object is said to be mutable when the state of the object can be changed E.x. through the setter methods mostly (of course there are other alternatives to change the state, which we will understand once we step through the guidelines of creating an immutable class). Immutable objects are objects whose state cannot be changed after they are created. Once created their state remains the same till the lifetime of the application (rather every life of the application).

Why would one create an immutable objects?  

Immutability objects find their use in concurrent applications/ multi-threaded applications, such an object is always thread safe which means threads wont see an inconsistent state of such an object,so you don't have to synchronize access to them across threads.They also are good candidate in Hash based collections like HashSet  (they need to override equals and hashCode methods). You can freely share and cache references to immutable objects without having to copy or clone …

Equals and hashcode method

are two of the most important non final methods in the Object class which if not coded properly can lead to problems when used in collection and are difficult to detect and debug. Hence it is important to understand the contract of the two methods to code for the same.
Equals method, defined in the object class checks if the references of the objects are the same, i.e they point to the same memory location. You can override this method to check if the two objects are meaningfully equivalent (though residing in different memory location). The following contract needs to be observed while coding the equals method

Reflexive - o1.equals(o1), an object should be equal to itselfSymmetric -  o1.equals(o2), if and only o2.equals(o1)Transitive -  o1.equals(o2), and o2.equals(o3) implies o1.equals(o3)Consistent - o1.equals(02), returns the same result as long as the objects are not modified.Null Comparison - !o1.equals(null), which means any object is not equal to null, it should return false.Eq…

How to make classes thread safe ?

What is Thread safety?
Thread safety is not making a class which implements Runnable or extends Thread, safe.  Thread safety means that the fields of an object or class always maintain a valid state as observed by other objects or classes when used concurrently by multiple threads.

What are the problems if you don't make a class thread safe?
Consider a scenario, where two threads are working on the same instance of the class. The object may be undergoing some modification in one thread and at the same time another thread may try to view the state of this object. This object when in thread1 could be in some intermediate state (when it is pre-empted) when thread 2 tries to access it. In this situation, thread 2 is viewing dirty state of the object. To make two threads, view a valid state of the object is when we term that an object is Thread safe.

When should you think about thread safety?
Firstly, you should think of thread safety whenever you are working in a multi-threaded applicat…