{ JavaTechNote's }
  • Linkedin
  • Github
  • Facebook
  • Twitter
  • Instagram

About me

Let me introduce myself

A bit about me

I am Dhananjaya Naidu and I’m just like you; a java lover. I feel proud to say that I am a Java Developer and currently located in Bangalore, India.

I enjoy coding in full Java/J2ee stack (Spring, JSF, Hibernate, Struts, Servlets, JSP) and Web Technologies (HTML, CSS, JavaScript, JQuery).

I love to play Cricket, Kabaddi, Valley Ball and any Outdoor Sports. I love the nature and like to travel new places.


Dhananjaya Naidu

Personal info

Dhananjaya Naidu Reddi

Be good, Do good !!!

Birthday: 19 Jun 1988
Website: www.rdnaidu.com
E-mail: hello@rdnaidu.com

Skills & Interests

Java & J2ee


My latest writings

Friday, 11 August 2023

Microservice Architecture Design Patterns

Here I am mentioning some common microservice architecture design patterns:

1. Service Decomposition: 

Break down your application into smaller, independently deployable services that handle specific business functions.

2. API Gateway: 

Implement a single entry point for clients to interact with multiple services, managing routing, load balancing, and authentication.

3. Service Discovery: 

Use a service registry to help services find each other and communicate dynamically in a changing environment.

4. Load Balancing: 

Distribute incoming requests across multiple instances of a service to improve scalability and reliability.

5. Database per Service: 

Assign each microservice its own database to ensure data isolation and independence.

6. Event Sourcing: 

Store changes to an application's state as a sequence of events, allowing for easy audit trails, versioning, and rebuilding state.

7. CQRS (Command Query Responsibility Segregation): 

Separate read and write operations into different services, optimizing for performance and scalability.

8. Asynchronous Communication: 

Use message queues or publish-subscribe systems to enable asynchronous communication between services.

9. Saga Pattern: 

Manage distributed transactions across multiple services by breaking them down into a sequence of smaller steps with compensating actions.

10. Containerization: 

Use technologies like Docker to package applications and dependencies into isolated containers for consistent deployment.

11. Orchestration vs. Choreography: 

Decide between a central orchestrator or decentralized choreography for coordinating interactions between services.

12. Fault Tolerance and Resilience: 

Design services to handle failures gracefully by implementing retries, circuit breakers, and fallback mechanisms.

13. Auto-scaling: 

Automatically adjust the number of instances based on demand to optimize resource utilization.

14. Authentication and Authorization: 

Implement security measures like OAuth, JWT, or API tokens to control access to services and resources.

15. Observability and Monitoring: 

Set up logging, monitoring, and tracing to gain insights into the health and performance of your microservices.

These are just a few of the many design patterns you can apply when building a microservice architecture. Remember that the choice of patterns depends on the specific needs of your application and the challenges you're addressing.

Thursday, 27 July 2023

Examples of converting Java 7 code to Java 8 code

Converting a for loop to a stream

In Java 7, you would use a for loop to iterate over a collection of elements. In Java 8, you can use a stream to do the same thing. Here is an example:
// Java 7
for (String element : collection) {
  // do something with element

// Java 8
collection.stream().forEach(element -> {
  // do something with element

Converting an if statement to a filter

In Java 7, you would use an if statement to filter a collection of elements. In Java 8, you can use a filter to do the same thing. Here is an example:
// Java 7
List<String> filteredList = new ArrayList<>();
for (String element : collection) {
  if (element.length() > 5) {

// Java 8  
List<String> filteredList = collection.stream().filter(element ->
  element.length() > 5).collect(Collectors.toList());

Converting a map to a reduce

In Java 7, you would use a map to create a new collection of elements. In Java 8, you can use a reduce to do the same thing. Here is an example:
// Java 7
Map<String, Integer> map = new HashMap<>();
for (String element : collection) {
  map.put(element, 1);

// Java 8 
Map<String, Integer> map =
  collection.stream().collect(Collectors.toMap(element -> element, element -> 1));
I hope this helps!

Thursday, 29 June 2023

Learning Java8 Stream API

If you are preparing for a Java interview, then this article would be most useful. Since, after the continuous upgradation of Java versions, the interview questions also increased. Here, I have listed the most commonly asked Java 8 Stream API interview questions and answers.

The contents of the article include Interview questions on the following topics with examples:

Stream API

Stream API is one of the hot topics to be chosen by the interviewer in interviews. Let us read and know the most used concepts of stream API questions. Let us see some of the questions and answers to Stream API of Java 8.

1. What is stream API?

  • Java 8 provides a new additional package called java.util.stream This package consists of classes, interfaces and enums to allow functional-style operations on the elements.
  • We can use stream API using the java.util.stream package.
  • We can use a stream to filter, collect, print and convert from one data structure to another.
  • Stream APIs do not store elements. It is functional. Operations performed using stream do not modify its source.

2. What is the difference between Collection and Stream?

  • The main difference between a Collection and Stream is that Collection contains its elements but Stream doesn’t.
  • The stream works on a view where elements are stored by collection or array, but unlike other views, any change made on the stream does not reflect the original collection.

3. What is an Intermediate operation in Stream API?

  • Intermediate operations return a stream as the output and intermediate operations are not executed until a terminal operation is invoked on the stream. This is called lazy evaluation
  • Intermediate operations of stream API process the current data and then return the new stream.
  • When an intermediate operation is executed, it only creates a new stream.

Example: map(), limit(), filter(), skip(), flatMap(), sorted(), distinct(), peek()

4. What is a Terminal operation in Stream API?

  • As the name suggests — terminal means the last operation in the Stream pipeline. Terminal operation traverses the stream and produces a result or a collection but not a new stream.
  • Terminal operations produce the results of the stream after all the intermediate operations are applied, and we can no longer use the stream once the terminal operation is performed. forEach()
  • A stream pipeline consists of a source ( Collection, array, function or I/O channel) it will invoke an intermediate operation in the pipeline lastly the terminal operation is performed which makes the stream pipeline consumed and marked closed.
  • We can have only one terminal operation at the end of the pipeline. If any operation is performed on a closed stream, it will result in java.lang.IllegalStateException; stream has already been operated upon or closed.


  • collect()
  • forEach()
  • forEachOrdered()
  • findAny()
  • findFirst()
  • toArray()
  • reduce()
  • count()
  • min()
  • max()
  • anyMatch()
  • allMatch()
  • noneMatch()

5. What does the map() function do? Why do you use it?

  • The map() function performs map functional operation in java. This means it can transform one type of object into others.
  • Example: consider we have a list of Strings and want to convert a List of Integer, we can use map() by applying a function to convert String to integer eg: parseInt() to map() and it will apply that to all elements of the list and give a list of Integer.

6. What does the filter() method do? When do you use it?

  • The filter method is used to filter elements that satisfy a certain condition that is specified using a Predicate function.
  • A predicate function is nothing but a function that takes an Object and returns a boolean.
  • For example, if you have a List of Integer and you want a list of even integers

7. What does the flatMap() method do?

  • The flatMap() function is an extension of the map function. Apart from transferring one object into another, it also flattens it.
  • Example: Consider you have a list of list data and you want to combine all elements of lists into just one list. In this case, we can use flatMap()

8. What is predicate Interface in Java stream?

  • A Predicate is a functional interface that represents a function, which takes an Object and returns a boolean.
  • It is used in several Stream methods like filter(), which uses Predicate to filter unwanted elements.

9. What does the peek() method do in Stream API?

  • The peek() method of the Stream class allows us to see through a Stream pipeline.
  • The peek() method returns a stream consisting of the elements of the stream after performing the provided action on each element. This is useful when we want to print values after each intermediate operation
  • We can peek through each step and print meaningful messages on the console. It is generally used for debugging issues related to lambda expression and Stream processing.
public class Main {
public static void main(String[] args) {
final List<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));

final List<Integer> ans = list.stream()
.filter(value -> value % 2 == 0)
.peek(value -> System.out.println("Filtered " + value))
.map(value -> value * 10)


Filtered 2
Filtered 4
[20, 40]

10. What is the difference between map() and flatMap() of Stream API?

The key difference between map() vs flatMap() in Java 8:

  • The function you pass to the map() operation returns a single value.
  • The function you pass to flatMap() operation returns a Stream of value.
  • The flatMap() is a combination of map and flat operation.
  • The map() is used for transformation only, but flatMap() is used for both transformation and flattening.

Example for map():

Example for flatMap():

11. Can we convert an Array into a Stream?

  • Yes, you can use Java to transform an array into a stream.
  • The Stream class provides a factory method to make a Stream from an array, such as Stream.of(T…), which accepts a variable parameter, also we can supply an array to it.


12. What is the difference between Stream API and Collection API?

13. What are the Stateful and stateless intermediate operations of Stream API?

  • Stateful operations are skip(), distinct(),limit() and sorted(). Rest all other stream operations are stateless.
  • When an operation requires retaining the information of the elements it has processed so far to process the current element then it is a stateful operation.
  • Example: Distinct operation requires keeping track of all the values it has processed so far, based on this information only it can decide whether the current value is unique or it has been processed before and accordingly either will add the current value to the new stream(which is the output of the distinct operation) or neglect the value and not add it to the new stream.

14. What is the difference between Stream’s findFirst() and findAny()?

  • As the name suggests, the findFirst() method is used to find the first element from the stream whereas the findAny() method is used to find any element from the stream.
  • The findFirst() is pre-deterministic whereas the findAny() is non-deterministic. In programming, Deterministic means the output is based on the input or initial state of the system.

15. What is a parallel stream? How to convert the list into a parallel stream?

  • One of the prominent features of Java 8 is Java Parallel Stream. It is meant for utilizing the various cores of the processor.
  • By default, all stream operations are sequential in Java unless explicitly specified as parallel. Parallel streams are created in Java in two ways.
  1. Calling the parallel() method on a sequential stream.
  2. Calling parallelStream() method on a collection.
  • Parallel streams are useful when we have many independent tasks that can be processed simultaneously to minimize the running time of the program.
  • All the Java code will usually have only one processing stream, where it is sequentially executed. But by using parallel streams, one can separate the Java code into more than one stream, which is executed in parallel on their separate cores, and the result is the combination of the individual results.
  • The order in which they are executed is not in our control. Hence, it is suggested to use a parallel stream when the order of execution of individual items does not affect the final result.


parallel(): parallel() method is called on the existing sequential stream to make it parallel.

public class Main {
public static void main(String[] args) {
Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5);



parallelStream(): parallelStream() is called on Java collections like ListSet, etc to make it a parallel stream.

public class Main {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));



Important Points to Remember About Java Streams

  1. Streams are not data structures but rather a sequence of elements that can be processed in a pipeline of operations.
  2. Streams support two types of operations: intermediate and terminal. Intermediate operations transform or filter the stream, while terminal operations produce a result or side effect.
  3. Streams are designed to be lazy, meaning that the elements are processed on-demand as the terminal operation is executed.
  4. It’s important to close streams that are opened from I/O channels or resources by using the close() method or by utilizing try-with-resources to ensure proper resource management.
  5. Java streams are not suitable for all scenarios. In some cases, traditional iteration using loops may be more appropriate and efficient.

Happy Reading ☺

Wednesday, 22 July 2020

GoF Design Patterns in Java's core libraries

GoF Design Patterns
You can find an overview of a lot of design patterns in Wikipedia. It also mentions which patterns are mentioned by GoF. I'll sum them up here and try to assign as many pattern implementations as possible, found in both the Java SE and Java EE APIs.

Creational patterns

Abstract factory 

(recognizeable by creational methods returning the factory itself which in turn can be used to create another abstract/interface type)


(recognizeable by creational methods returning the instance itself)

Factory method 

(recognizeable by creational methods returning an implementation of an abstract/interface type)


(recognizeable by creational methods returning a different instance of itself with the same properties)


(recognizeable by creational methods returning the same instance (usually of itself) everytime)

Structural patterns


(recognizeable by creational methods taking an instance of different abstract/interface type and returning an implementation of own/another abstract/interface type which decorates/overrides the given instance)


(recognizeable by creational methods taking an instance of different abstract/interface type and returning an implementation of own abstract/interface type which delegates/uses the given instance)
  • None comes to mind yet. A fictive example would be new LinkedHashMap(LinkedHashSet<K>, List<V>) which returns an unmodifiable linked map which doesn't clone the items, but uses them. The java.util.Collections#newSetFromMap() and singletonXXX() methods however


(recognizeable by behavioral methods taking an instance of same abstract/interface type into a tree structure)


(recognizeable by creational methods taking an instance of same abstract/interface type which adds additional behaviour)


(recognizeable by behavioral methods which internally uses instances of different independent abstract/interface types)


(recognizeable by creational methods returning a cached instance, a bit the "multiton" idea)


(recognizeable by creational methods which returns an implementation of given abstract/interface type which in turn delegates/uses a different implementation of given abstract/interface type)

Behavioral patterns

Chain of responsibility 

(recognizeable by behavioral methods which (indirectly) invokes the same method in another implementation of same abstract/interface type in a queue)


(recognizeable by behavioral methods in an abstract/interface type which invokes a method in an implementation of a different abstract/interface type which has been encapsulated by the command implementation during its creation)


(recognizeable by behavioral methods returning a structurally different instance/type of the given instance/type; note that parsing/formatting is not part of the pattern, determining the pattern and how to apply it is)


(recognizeable by behavioral methods sequentially returning instances of a different type from a queue)


(recognizeable by behavioral methods taking an instance of different abstract/interface type (usually using the command pattern) which delegates/uses the given instance)


(recognizeable by behavioral methods which internally changes the state of the whole instance)

Observer (or Publish/Subscribe) 

(recognizeable by behavioral methods which invokes a method on an instance of another abstract/interface type, depending on own state)


(recognizeable by behavioral methods which changes its behaviour depending on the instance's state which can be controlled externally)


(recognizeable by behavioral methods in an abstract/interface type which invokes a method in an implementation of a different abstract/interface type which has been passed-in as method argument into the strategy implementation)

Template method 

(recognizeable by behavioral methods which already have a "default" behaviour defined by an abstract type)


(recognizeable by two different abstract/interface types which has methods defined which takes each the otherabstract/interface type; the one actually calls the method of the other and the other executes the desired strategy on it)


What can I do


Social media Branding is far and away the best technique a company has to boost engagement with its customer base. Even a minimum of involvement, such as making one post a day.

Web Design

Web design is the process of creating websites. It encompasses several different aspects, including webpage layout, content production, and graphic design.


Web Development refers to building, creating, and an maintaining websites. It includes aspects such as web design, web publishing, web programming and database management.

Graphic Design

Graphic design is the process of visual communication and problem-solving through the use of typography, photography, and illustration. The field is considered a subset of visual communication and communication design.


Photography is the art, application and practice of creating durable images by recording light or other electromagnetic radiation, either electronically by means of an image sensor, or chemically by means of a light-sensitive material such as photographic film.

User Experience

User experience (UX) design is the process design teams use to create products that provide meaningful and relevant experiences to users. This involves the design of the entire process of acquiring and integrating the product, including aspects of branding, design.


Get in touch with me


Bangalore, India