Java - Streams - Interview Questions

Java arrays and collections are a core part of any Java program. Almost every Java application makes use of arrays or collections to process data, group data, summarize data and gather intelligence from data.

But processing data from arrays or collections, filtering data, grouping data and summarizing data is not perfect. You will end up writing multiple loops leading to complex code.

Streams API, introduced in Java 8, extensively simplifies and reduces the lines of code needed to process data from collections, by providing a declarative and functional-style operations to process data. In addition, operations can be chained together to form a pipeline which would have required multiple loops prior to Java 8.

You may not be asked specific questions on Streams, but your knowledge of using Streams to process data is absolutely necessary to excel in coding sessions.

Below we will see how to use Streams for performing some common data operations. For data, we will use a list of 'State' objects as example. Each 'State' object contains attributes 'code', 'name', and list of 'City' objects. 'City' contains attributes 'name' and 'population'.

Given a list of state objects, print the state codes.

 
//List of states
List<State> stateList = Util.buildStateList();
//Print list of states codes
stateList
  .stream()
  .forEach(e -> {System.out.println(e.getStateCode()));}

Given a list of state objects, print the state codes that begin with A.

 
//List of states
List<State> stateList = Util.buildStateList();
//Filter states and print state codes
stateList
  .stream()
  .filter(e -> e.getStateCode().startsWith('A'))
  .forEach(e -> {System.out.println(e.getStateCode()));}

Given a list of state objects, modify names to upper case, and print names whose codes begin with A.

 
//List of states
List<State> stateList = Util.buildStateList();
//Filter states and print state codes
stateList
  .stream()
  .filter(e -> e.getStateCode().startsWith('A'))
  .map(e -> e.getStateName().toUpperCase())
  .forEach(e -> {System.out.println(e.getStateName()));}

Given a list of state objects, modify names to upper case, and print names whose codes begin with A and sorted in alphabetical order.

 

*** See complete answer and code snippet in the Java Interview Guide.

Given a list of state objects, create a map with code as key and name as value.

 

*** See complete answer and code snippet in the Java Interview Guide.

Java Interview Guide has over 250 REAL questions from REAL interviews. Get the guide for $15.00 only.
 
BUY EBOOK
 

Given a list of state objects, print the list of cities in each state.

 

*** See complete answer and code snippet in the Java Interview Guide.

 
Java Interview Guide

$15.00

BUY EBOOK
  SSL Secure Payment
Java Interview Quesiuons - Secure Payment
Java Interview Guide

$15.00

BUY EBOOK
  SSL Secure Payment
Java Interview Quesiuons - Secure Payment
 

Java - Interview Questions

Java - Object Oriented ProgrammingJava - Objects & ClassesJava - Data TypesJava - VariablesJava - StringsJava - ArraysJava - CollectionsJava - ReflectionJava - Lambda ExpressionsJava - StreamsJava - GenericsJava - ExceptionsJava - IOJava - ThreadsJava - ConcurrencyJava - JDBCJava - NetworkingJava - SecurityJava - JVM InternalsJava - PerformanceJava - New in Java 8Java - New in Java 9Java - New in Java 10Java - New in Java 11
 
MASTER Java  

Top ranked courses to help you master Java skills.
Java Programming Masterclass

iconicon

Offered By - Tim Buchalka
Platform - Udemy
Rating - * * * * *
Students Enrolled - 575,000 +

RECOMMENDED RESOURCES
Behaviorial Interview
Top resource to prepare for behaviorial and situational interview questions.

STAR Interview Example