Skip to main content


Showing posts from December, 2021

Setter Dependency Injection in Spring

In the post dependency injection in Spring we have already gone through the concept of dependency injection, in this post we’ll see in details one of the type of dependency injection- Setter dependency injection in Spring . For another type of dependency injection, Constructor dependency injection check this post- Constructor Dependency Injection in Spring Spring Setter dependency injection In setter based DI Spring container calls setter methods on your beans after invoking a no-argument constructor or no-argument static factory method to instantiate your bean. For configuring setter based dependencies you can use XML configuration as well as annotations. We’ll see examples of doing it using both of these ways. Spring Setter dependency injection Example In the example there is a class to place orders called Order and purchase can be done from an online store or a retail store. In Order class dependencies for the properties are injected using setter dependency injection.

Summarizing Collectors in Java Stream

In this tutorial you’ll learn about the different summarizing collectors in Java Stream API that returns a summary statistics Collectors. Summarizing collectors in Collectors class There are three summarizing collector methods in Collectors class which are as following- summarizingInt(ToIntFunction<? super T> mapper) - Returns a Collector which applies an int-producing mapping function to each input element. summarizingLong(ToLongFunction<? super T> mapper) - Returns a Collector which applies an long-producing mapping function to each input element. summarizingDouble(ToDoubleFunction<? super T> mapper) - Returns a Collector which applies an double-producing mapping function to each input element. Argument to these methods is a corresponding functional interface ToIntFunction, ToLongFunction and ToDoubleFunction that represents a function that produces respectively a int, long or double valued result. Return type of the method used is also as per t

Generate Random Numbers Java Stream

If you are writing a Java program to generate random numbers two tried and tested ways are to either use Math.random() method or methods of Random class to get a random number but the limitation with these methods is that they generate a single random number and you would need to use them in a loop in case you need a list of random numbers. With the introduction of Java Stream API in Java 8 , Random class has also been expanded to include methods that can return a stream of ints, longs or doubles which means a return type of IntStream, LongStream or DoubleStream. Random class methods for generating streams In Random class there are following methods for generating infinite streams- 1. ints() - Returns an effectively unlimited stream of int values. 2. ints(int randomNumberOrigin, int randomNumberBound) - Returns an effectively unlimited stream of int values within the given bounds. Following methods restrict the generated random numbers. 1. ints(long streamSize) - Returns

Java HashMap merge() With Examples

The Java HashMap merge() method is used to insert a new (key,value) pair into the HashMap or to modify value for an already existing key. Syntax of merge() method merge(K key, V value, BiFunction<? super V,? super V,? extends V> remappingFunction) The parameters are- key - Key with which the resulting value has to be mapped. value - The non-null value which replaces the existing value associated with the key or inserts this new value. remappingFunction - It is an expression of type BiFunction functional interface used to recompute a value if present merge method has the following scenarios- If the specified key is not already associated with a value associates it with the given non-null value and the (key, value) pair is inserted into the HashMap. If the specified key is associated with null value, associates it with the given non-null value. If the key already exists then replaces (or merges) the associated value with the results of the given rem