Posts

How to Join strings – Java8

Image
As a programmer quite often we hit the need to join multiple string values separated by a delimiter. For example join all values from a String[] or List separated by comma ( , ). It’s very common scenario and I am sure most of have already done this. Lets how it’s usually done before Java 8 and what is smart way to Join multiple strings in Java 8. We have following array of strings and we need to join all string values from this array into single string separated by pipe ( | ) character String[] quote = {"Java8", "is", "awesome"}; Before Java 8 Before Java 8 usual way to do is, loop through each element of the array and append to another String variable and append separator after each element. May be you are already thinking about general issue with this approach which is unnecessary separator after the last element. To deal with it, either we can add condition within loop to not append separator after last element or remove the last character (

Lambda Expression v/s Anonymous Inner Class - Java 8

Image
Lambda Expression is new feature of Java 8. Before Java 8 Anonymous Inner Class was very useful to provide method local implementation of any interface like ActionListner or EventListener etc. With Java 8 if the interface is a Functional Interface we can use Lambda Expression to provide implementation. Both of these can implement an Interface but there are subtle differences between them. Following are to the point differences between them - Anonymous Inner class can implement any interface but Lambda can implement only Functional Interface , the interface with only one abstract method. Anonymous Inner Class can have state variables but Lambda Expression cannot. Lambda Expression is stateless. Lambda Expression can only access final or effectively final variables of containing method. Scope - Anonymous Inner Class creates a new scope whereas Lambda Expression does not. Lambda executes always within scope of surrounding context. To explain it better, in Anonymous Inner Clas

Lambda Expression Basics and Syntax - Java8

Image
In simple term, a Lambda Expression is implementation of a Functional Interface. Lambda Expression is a way to provide implementation of a functional interface without creating a Class or Anonymous Inner Class. Note that Lambda can implement only a functional interface. Read more about Functional Interface Here is a functional interface named Processor which has single abstract method called process And let say there is processValue method, which takes a value and a Processor object as input, apply processor on the value and return the result. private int processValue(int value, Processor processor) { return processor.process(value); } Call processValue method using Anonymous Inner Inner Class as implementation of Processor interface. processValue(10, new Processor() { @Override public int process(int input) { return input * 2; } }); It’s easily noticeable the unnecessary verbose code that we had to write for anonymous inner class. Only important line which is d

Functional Interface - Java8

Image
Before Java 8, an interface could only declare one or more methods also known as Abstract Method (method with no implementation, just the signature). Starting with Java 8 an interface can also have implementation of one or more methods (knows as Interface Default Method) and static methods along with abstract methods. Interface Default Methods are marked default keyword. So the question is, what is Functional Interface? An interface with Single Abstract Method (SAM) is called Functional Interface. Which means -  An interface with Single Abstract Method is a Functional Interface An interface with Single Abstract Method and zero or more default methods and zero or more static method is also a valid Functional Interface. Below is a functional interface with Single Abstract Method Below is a functional interface with Single Abstract Method along with default method and static method. You must have noticed the @FunctionalInterface annotation. What does it mean?

Java Thread Local Storage explained in easiest practical way

Image
One of the cool feature of Java that I like is ThreadLocal storage. ThreadLocal storage is explained in easiest way with practical example in this blog. ThreadLocal is a class in java.lang package which provides a way to store and access data of any type per thread, which means each thread will have it's separate instance of the data. During execution of a thread, one class can set data in ThreadLocal and other classes further down in the execution layer can access/read the data, as long as execution in within same thread. It is important to know that ThreadLocal data set by one thread is not visible or accessible in another thread. That the core concept of ThreadLocal storage. The way to use ThreadLocal is declare a static property of type ThreadLocal where T is type of the data we want to store. It's static so that it easily accessible in classes wherever needed. You can declare it public, private or whatever make sense in your usecase. Usually declaring it as

How to enable JPA eclipselink logging in WAS Liberty

Image
When this goes well, you are happy but every developer hits situation when you need more logs and details to troubleshoot issue and determine root case. It happens many times when you work with JPA and thing don't goes as you expect. You really need to look under the hood what JPA is going, what SQL statements it's generating and executing. Environment: WAS Liberty 17.0.01, JAVA EE 7, JPA 2.1, EclipseLink 2.6.3 In above environment, to enable JPA logging including SQL statements, bind parameters and transaction details do following - Add following tow properties in persistence.xml <property name="eclipselink.logging.level" value="ALL"/> <property name="eclipselink.logging.parameters" value="true"/> Add following logging tag in server.xml <logging traceSpecification="eclipselink=all" maxFileSize="20" maxFiles="10"/> Redeploy application and recycle server. All done! T

EhCache3 as JCache (JSR-107) Implementation with Cache Statistics

Image
If you are reading this you already know that caching is mechanism to store static data In-Memory for faster access and avoid expensive database calls or any kind of service calls to get the data. Read my other post to learn InMemory Caching using EhCache3 . There are many ways to implement cache in java based applications, starting from very simple Map based caching to Apache Common JCS , EhCache , Apache Ignite , Hazelcast , Oracle Coherence and many more. With so many implementations available, question is which one we should go for?  If you have to choose one from multiple options best bet is to look for standard. Standard because many people have put their mind together in it, better support, examples and guide available for reference plus expect that further upgrades/changes will not breaking changes. So for InMemory cache Standard Specification is JSR-107 .  You can read about it in more detail at JSR 107: JCACHE - Java Temporary Caching API . JCache specifies API