Java Interview question-18
Java || SpringBoot ||AWS
Question 171: Diffrence between map vs flat map
Map:
The map() operation applies a function to each element in the stream and returns a new stream consisting of the results.
It performs a one-to-one mapping, where each input element is transformed into exactly one output element.
The resulting stream has the same size as the input stream.
List<String> names = Arrays.asList("John", "Alice", "Bob");
List<String> upperCaseNames = names.stream()
.map(String::toUpperCase)
.collect(Collectors.toList());
// Result: ["JOHN", "ALICE", "BOB"]
FlatMap:
The FlatMap
()operation is used when each input element can be mapped to zero or more output elements.It flattens nested collections or arrays into a single stream, merging the elements from all the nested collections or arrays.
The resulting stream may have a different size than the input stream, as the number of elements can vary based on the mapping
List<List<Integer>> numbers = Arrays.asList(
Arrays.asList(1, 2, 3),
Arrays.asList(4, 5, 6),
Arrays.asList(7, 8, 9)
);
List<Integer> flattenedNumbers = numbers.stream()
.flatMap(Collection::stream)
.collect(Collectors.toList());
// Result: [1, 2, 3, 4, 5, 6, 7, 8, 9]
Question 172: Hibernate load vs get
load():
The
load()the method is a lazy-loading method. It returns a proxy object that acts as a placeholder for the actual entity and defers loading the data from the database until it's accessed.If the entity is not found in the database, a special proxy object is returned (a subclass of the entity class) that throws an exception if any properties other than the identifier are accessed before initialization.
It can be useful when you only need the entity to access its identifier or establish associations without actually loading the complete entity data from the database.
Book book = session.load(Book.class, 123); // Returns a proxy object
System.out.println(book.getId()); // Does not trigger a database query
get:
The
get()method eagerly loads the complete entity data from the database immediately upon invocation. It returns the actual entity object if found in the database ornullif not found.If the entity is not found, it simply returns
nullwithout throwing an exception.It is suitable when you need the complete entity data and are confident that the entity with the specified identifier exists in the database.
Book book=session.get(Book.class,123);// Returns the actual entity or null
System.out.println(book.getTitle()); // Triggers a database query
Question 173: How to create a custom transaction manager’ in Spring boot
We have to follow three steps:
1. Implement the PlatformTransactionManager interface:
Create a new class that implements the
PlatformTransactionManagerinterface.Override the necessary methods defined by the interface to provide the desired transaction management behavior.
2. Register the custom transaction manager in the Spring Boot configuration:
- In your Spring Boot configuration class, define a bean of type
PlatformTransactionManagerand provide an instance of your custom transaction manager.
3. Use the custom transaction manager in your application:
- With the custom transaction manager registered, you can now use it in your application by injecting it where transaction management is required.
Question 174: what are the bean scopes? difference between singleton and Request bean scope?
Singleton: The default scope in Spring.
Prototype: Creates a new instance of the bean every time it is requested.
Request (Web-specific): Creates a new instance of the bean for each HTTP request in a web application.
Session (Web-specific): Creates a single instance of the bean per HTTP session.
Global Session (Web-specific): Similar to the Session scope, but creates a single instance per global HTTP session
Question 175: Design pattern used by Spring framework
Spring framework includes Dependency Injection (DI) Pattern, Singleton Pattern, Factory Pattern, Proxy Pattern, Observer Pattern, and Strategy Pattern.
Question 176: What is @spingbootapplication annotation?
It combines three important annotations: @Configuration, @EnableAutoConfiguration, and @ComponentScan.
Question 177: How to use validation in Springboot
by combining the javax.validation annotations with the @Valid annotation and utilizing the validation framework provided by Spring
Question 178: What is the load-balancing policy we are using for Kubernetes
The Round Robin policy ensures that the workload is distributed evenly among the pods, providing a basic form of load balancing
Additional load-balancing strategies, such as session affinity (sticky sessions) or more sophisticated algorithms like weighted round-robin, least connections, or IP Hash, can be implemented by utilizing these external components or custom configurations based on your specific requirements.
Question 179: How dispatcher servlet works

Question 180: How many ways we can communicate with microservice?
HTTP/REST: Microservices can communicate over HTTP using RESTful APIs. Each microservice exposes its endpoints, and other microservices can make HTTP requests to interact with them. This approach is widely adopted due to its simplicity and compatibility with various programming languages and frameworks.
There are a few clients in Spring Boot through which we can make API calls — RestTemplate, Web-client is used to make HTTP requests and consume other services.
Messaging: Messaging systems, such as Apache Kafka, RabbitMQ, or ActiveMQ, can be used for asynchronous communication between microservices. Messages are sent to specific topics or queues, and microservices can subscribe to these topics or queues to consume the messages. This pattern enables loose coupling and scalability.
Kafka is one of the popular example from messaging
Event-driven communication: Microservices can communicate through events. When an event occurs in one microservice, it publishes the event to an event bus or message broker, and other microservices that are interested in that event can consume it. This pattern promotes decoupling and allows for real-time data propagation.
RPC (Remote Procedure Call): RPC allows microservices to invoke methods or functions on remote services as if they were local. It abstracts the communication details and provides a more seamless interaction between microservices. gRPC is a popular RPC framework used in microservices architectures.
GraphQL: GraphQL is a query language and runtime for APIs. It allows clients to request specific data and shape the response according to their needs. Microservices can expose GraphQL APIs, and clients can query multiple microservices in a single request, reducing over-fetching and under-fetching of data.
Service Mesh: A service mesh, like Istio or Linkerd, provides a dedicated infrastructure layer for managing communication between microservices. It offers features such as service discovery, load balancing, traffic management, security, and observability, simplifying the development and deployment of microservices.
