Friday, 11 August 2023
Thursday, 27 July 2023
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) { filteredList.add(element); } } // 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
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.
Example:
- 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)
.collect(Collectors.toList());
System.out.println(Arrays.toString(ans.toArray()));
}
}
Output:
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.
Example:

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.
- Calling the parallel() method on a sequential stream.
- 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.
Example:
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);
stream.parallel().forEach(System.out::println);
}
}
Output:
3
5
4
1
2
parallelStream(): parallelStream() is called on Java collections like List, Set, 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));
list.parallelStream().forEach(System.out::println);
}
}
Output:
3
1
5
4
2
Important Points to Remember About Java Streams
- Streams are not data structures but rather a sequence of elements that can be processed in a pipeline of operations.
- 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.
- Streams are designed to be lazy, meaning that the elements are processed on-demand as the terminal operation is executed.
- 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.
- Java streams are not suitable for all scenarios. In some cases, traditional iteration using loops may be more appropriate and efficient.
Wednesday, 22 July 2020
Creational patterns
Abstract factory
- javax.xml.parsers.DocumentBuilderFactory#newInstance()
- javax.xml.transform.TransformerFactory#newInstance()
- javax.xml.xpath.XPathFactory#newInstance()
Builder
- java.lang.StringBuilder#append() (unsynchronized)
- java.lang.StringBuffer#append() (synchronized)
- java.nio.ByteBuffer#put() (also on CharBuffer, ShortBuffer, IntBuffer, LongBuffer, FloatBuffer and DoubleBuffer)
- javax.swing.GroupLayout.Group#addComponent()
- All implementations of java.lang.Appendable
- java.util.stream.Stream.Builder
Factory method
- java.util.Calendar#getInstance()
- java.util.ResourceBundle#getBundle()
- java.text.NumberFormat#getInstance()
- java.nio.charset.Charset#forName()
- java.net.URLStreamHandlerFactory#createURLStreamHandler(String) (Returns singleton object per protocol)
- java.util.EnumSet#of()
- javax.xml.bind.JAXBContext#createMarshaller() and other similar methods
Prototype
- java.lang.Object#clone() (the class has to implement java.lang.Cloneable)
Singleton
Structural patterns
Adapter
- java.util.Arrays#asList()
- java.util.Collections#list()
- java.util.Collections#enumeration()
- java.io.InputStreamReader(InputStream) (returns a Reader)
- java.io.OutputStreamWriter(OutputStream) (returns a Writer)
- javax.xml.bind.annotation.adapters.XmlAdapter#marshal() and #unmarshal()
Bridge
- 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
Composite
- java.awt.Container#add(Component) (practically all over Swing thus)
- javax.faces.component.UIComponent#getChildren() (practically all over JSF UI thus)
Decorator
- All subclasses of java.io.InputStream, OutputStream, Reader and Writer have a constructor taking an instance of same type.
- java.util.Collections, the checkedXXX(), synchronizedXXX() and unmodifiableXXX() methods.
- javax.servlet.http.HttpServletRequestWrapper and HttpServletResponseWrapper
- javax.swing.JScrollPane
Facade
- javax.faces.context.FacesContext, it internally uses among others the abstract/interface types LifeCycle, ViewHandler, NavigationHandler and many more without that the enduser has to worry about it (which are however overrideable by injection).
- javax.faces.context.ExternalContext, which internally uses ServletContext, HttpSession, HttpServletRequest, HttpServletResponse, etc.
Flyweight
- java.lang.Integer#valueOf(int) (also on Boolean, Byte, Character, Short, Longand BigDecimal)
Proxy
- java.lang.reflect.Proxy
- java.rmi.*
- javax.ejb.EJB (explanation here)
- javax.inject.Inject (explanation here)
- javax.persistence.PersistenceContext
Behavioral patterns
Chain of responsibility
Command
- All implementations of java.lang.Runnable
- All implementations of javax.swing.Action
Interpreter
- java.util.Pattern
- java.text.Normalizer
- All subclasses of java.text.Format
- All subclasses of javax.el.ELResolver
Iterator
- All implementations of java.util.Iterator (thus among others also java.util.Scanner!).
- All implementations of java.util.Enumeration
Mediator
- java.util.Timer (all scheduleXXX() methods)
- java.util.concurrent.Executor#execute()
- java.util.concurrent.ExecutorService (the invokeXXX() and submit() methods)
- java.util.concurrent.ScheduledExecutorService (all scheduleXXX() methods)
- java.lang.reflect.Method#invoke()
Memento
- java.util.Date (the setter methods do that, Date is internally represented by a longvalue)
- All implementations of java.io.Serializable
- All implementations of javax.faces.component.StateHolder
Observer (or Publish/Subscribe)
- java.util.Observer/java.util.Observable (rarely used in real world though)
- All implementations of java.util.EventListener (practically all over Swing thus)
- javax.servlet.http.HttpSessionBindingListener
- javax.servlet.http.HttpSessionAttributeListener
- javax.faces.event.PhaseListener
State
- javax.faces.lifecycle.LifeCycle#execute() (controlled by FacesServlet, the behaviour is dependent on current phase (state) of JSF lifecycle)
Strategy
- java.util.Comparator#compare(), executed by among others Collections#sort().
- javax.servlet.http.HttpServlet, the service() and all doXXX() methods take HttpServletRequest and HttpServletResponse and the implementor has to process them (and not to get hold of them as instance variables!).
- javax.servlet.Filter#doFilter()
Template method
- All non-abstract methods of java.io.InputStream, java.io.OutputStream, java.io.Reader and java.io.Writer.
- All non-abstract methods of java.util.AbstractList, java.util.AbstractSet and java.util.AbstractMap.
- javax.servlet.http.HttpServlet, all the doXXX() methods by default sends a HTTP 405 "Method Not Allowed" error to the response. You're free to implement none or any of them.
Visitor
- javax.lang.model.element.AnnotationValue and AnnotationValueVisitor
- javax.lang.model.element.Element and ElementVisitor
- javax.lang.model.type.TypeMirror and TypeVisitor
- java.nio.file.FileVisitor and SimpleFileVisitor
- javax.faces.component.visit.VisitContext and VisitCallback