$ Java 8 Stream API
// Interview Prep — Quick Reference
Quick Reference
Source
Creates a Stream from a Collection
Intermediate
Lazy, Returns a Stream, Chainable
Terminal
Triggers Execution, Ends the Stream
Collectors
Reduces Stream into List / Set / Map
Legend
Source
Intermediate (Lazy)
Terminal
Collectors
Core Rules
Lazy Intermediate Ops Don't Run Until Terminal Op Called
Once A Stream Can Be Consumed Only Once
Immutable Stream Never Modifies the Source Collection
Stream Operations
Source
stream()Converts a Collection into a Stream
parallelStream()Parallel stream, uses ForkJoinPool internally
Intermediate Operations (Lazy)
filter()Filters records — .filter(n -> n%2==0)
map()Transforms data (1-to-1) — .map(String::toUpperCase)
flatMap()Flattens nested collections (1-to-many) — .flatMap(List::stream)
distinct()Removes duplicate elements
sorted()Sorts — natural or via Comparator
peek()Debug/log elements without consuming stream
limit(n)Returns first N elements
skip(n)Skips first N elements (pagination, top-N)
Terminal Operations
collect()Converts stream result into a Collection
count()Returns total element count
forEach()Iterates over elements
findFirst()Returns first element as Optional
findAny()Returns any element, useful with parallel streams
anyMatch()True if at least one element matches
allMatch()True if all elements match
noneMatch()True if no elements match
reduce()Aggregation — .reduce(0, Integer::sum)
min() / max()Min/Max element via Comparator
Collectors
toList() / toSet()Collect into List or Set
toMap()toMap(key, value) functions
groupingBy()Groups records by a classifier function
counting()Counts grouped records (used with groupingBy)
joining()Joins Strings with a delimiter
partitioningBy()Splits into TRUE / FALSE groups
averagingDouble()Calculates average of a numeric field
summingDouble()Sum all double values
maxBy()Find maximum element
Pipeline Flow & map() vs flatMap()
Stream Execution Pipeline
collection.stream()
filter()
map()
sorted()
collect()
Source → Intermediate Ops (Lazy) → Terminal Op (Executes)
Interview Use Cases for skip()/limit()
Pagination
2nd Highest Salary
Top N Records
map() vs flatMap()
map()
RelationOne-to-One Transformation
ExampleList<Employee> → List<String>
Usage.map(Employee::getName)
flatMap()
RelationOne-to-Many + Flattening
ExampleList<List<String>> → List<String>
Usage.flatMap(emp -> emp.getSkills().stream())
Frequently Asked Interview Problems
Highest Salary Employee
employees.stream() .max(Comparator.comparing(Employee::getSalary));
Second Highest Salary
employees.stream() .sorted(Comparator.comparing(Employee::getSalary).reversed()) .skip(1) .findFirst();
Group Employees by Department
employees.stream() .collect(Collectors.groupingBy(Employee::getDepartment));
Count Employees by Department
employees.stream() .collect(Collectors.groupingBy(Employee::getDepartment, Collectors.counting()));
Average Salary by Department
employees.stream() .collect(Collectors.groupingBy(Employee::getDepartment, Collectors.averagingDouble(Employee::getSalary)));
Find Duplicate Elements
Set<Integer> seen = new HashSet<>(); list.stream() .filter(n -> !seen.add(n)) .collect(Collectors.toList());
Employees with Salary > 50000
employees.stream() .filter(e -> e.getSalary() > 50000) .collect(Collectors.toList());
Sort Employees by Salary (Ascending)
employees.stream() .sorted(Comparator.comparing(Employee::getSalary)) .collect(Collectors.toList());
Highest Paid Employee per Department
employees.stream() .collect(Collectors.groupingBy(Employee::getDepartment, Collectors.maxBy(Comparator.comparing(Employee::getSalary))));
Employee Names to Uppercase
employees.stream() .map(e -> e.getName().toUpperCase()) .collect(Collectors.toList());
Sum of Salary by Department
list.stream() .collect(Collectors.groupingBy(Employee::getDepartment, Collectors.summingDouble(Employee::getSalary)));
Salary > 20000 AND Gender Male
list.stream() .filter(e -> e.getSalary() > 20000.00) .filter(e -> e.getGender().equalsIgnoreCase("Male")) .collect(Collectors.toList());
Duplicate Employee Names ⭐ ⭐
employees.stream() .collect(Collectors.groupingBy(Employee::getName, Collectors.counting())) .entrySet().stream() .filter(en -> en.getValue() > 1) .map(Map.Entry::getKey) //.map(e -> e.getKey()) .collect(Collectors.toSet());
First Non-Repeating Character | String str = "swiss ⭐ ⭐
str.chars().mapToObj(c -> (char) c) .collect(Collectors.groupingBy( c -> c, LinkedHashMap::new,Collectors.counting())) .entrySet().stream() .filter(e -> e.getValue() == 1) .map(Map.Entry::getKey) .findFirst();
Names Starting with "C" → Uppercase
names.stream() .filter(a -> a.startsWith("C")) .map(String::toUpperCase) .collect(Collectors.toList());
Remove Duplicates via distinct()
List<Integer> uniqueList = list.stream() .distinct() .collect(Collectors.toList());
30-Second Interview Revision
Stream introduced in Java 8
Stream does not store data
Stream does not modify original collection
Intermediate Operations are Lazy
Terminal Operation triggers execution
Stream can be consumed only once
map() = Transform Data (1-to-1)
flatMap() = Flatten Nested Collections
filter() = Apply Condition
reduce() = Aggregation
collect() = Final Result
groupingBy() = Group Data
counting() = Count Grouped Data
averagingDouble() = Calculate Average
partitioningBy() = Split into TRUE/FALSE
parallelStream() = Parallel Processing