Java 8 Stream API

Short Overview:
Stream API allows functional-style processing of collections using operations like filter, map, reduce, collect, etc.


1️⃣ What is Stream API?

Answer: Stream is a sequence of elements supporting functional operations.

List list = Arrays.asList("Java","Spring");
list.stream().forEach(System.out::println);

2️⃣ Difference between filter() and map()?

Answer: filter() selects elements; map() transforms elements.

List nums = Arrays.asList(1,2,3,4);

nums.stream()
    .filter(n -> n % 2 == 0)
    .map(n -> n * 2)
    .forEach(System.out::println);

3️⃣ What is reduce()?

Answer: Combines elements into single result.

int sum = Arrays.asList(1,2,3,4)
    .stream()
    .reduce(0, Integer::sum);

System.out.println(sum);

4️⃣ How to find maximum number?

int max = Arrays.asList(10,20,5)
    .stream()
    .max(Integer::compare)
    .get();

System.out.println(max);

5️⃣ How to sort list using Stream?

Arrays.asList(5,2,8,1)
    .stream()
    .sorted()
    .forEach(System.out::println);

6️⃣ What is flatMap()?

Answer: Flattens nested collections.

List> list = Arrays.asList(
    Arrays.asList(1,2),
    Arrays.asList(3,4)
);

list.stream()
    .flatMap(Collection::stream)
    .forEach(System.out::println);

7️⃣ How to collect results into List?

List even = Arrays.asList(1,2,3,4)
    .stream()
    .filter(n -> n % 2 == 0)
    .collect(Collectors.toList());

8️⃣ What are Intermediate and Terminal operations?

Intermediate: filter(), map(), sorted() Terminal: forEach(), collect(), reduce()


9️⃣ How to group elements?

Map> grouped =
    Arrays.asList(1,2,3,4,5)
        .stream()
        .collect(Collectors.groupingBy(n -> n % 2));

🔟 How to find duplicate elements?

List nums = Arrays.asList(1,2,3,2,4,1);

Set seen = new HashSet<>();

nums.stream()
    .filter(n -> !seen.add(n))
    .forEach(System.out::println);

1️⃣1️⃣ How to count elements?

long count = Arrays.asList(1,2,3,4)
    .stream()
    .count();

System.out.println(count);

1️⃣2️⃣ How to use parallelStream()?

Arrays.asList(1,2,3,4)
    .parallelStream()
    .forEach(System.out::println);

Used for parallel processing.

Stream API - 20 Programming Questions 

Short Overview:
Below are commonly asked Stream API coding questions with complete programs.


1️⃣ Find even numbers

List list = Arrays.asList(1,2,3,4,5,6);
list.stream()
    .filter(n -> n % 2 == 0)
    .forEach(System.out::println);

2️⃣ Find odd numbers

list.stream()
    .filter(n -> n % 2 != 0)
    .forEach(System.out::println);

3️⃣ Find sum of all numbers

int sum = list.stream()
    .reduce(0, Integer::sum);

4️⃣ Find average

double avg = list.stream()
    .mapToInt(Integer::intValue)
    .average()
    .orElse(0);

5️⃣ Find maximum number

int max = list.stream()
    .max(Integer::compare)
    .get();

6️⃣ Find minimum number

int min = list.stream()
    .min(Integer::compare)
    .get();

7️⃣ Remove duplicate elements

list.stream()
    .distinct()
    .forEach(System.out::println);

8️⃣ Sort numbers ascending

list.stream()
    .sorted()
    .forEach(System.out::println);

9️⃣ Sort numbers descending

list.stream()
    .sorted(Comparator.reverseOrder())
    .forEach(System.out::println);

🔟 Count elements

long count = list.stream().count();

1️⃣1️⃣ Find first element

Optional first = list.stream().findFirst();

1️⃣2️⃣ Check if all match condition

boolean allEven = list.stream()
    .allMatch(n -> n % 2 == 0);

1️⃣3️⃣ Check if any match condition

boolean anyEven = list.stream()
    .anyMatch(n -> n % 2 == 0);

1️⃣4️⃣ Convert list to Set

Set set = list.stream()
    .collect(Collectors.toSet());

1️⃣5️⃣ Convert list of strings to uppercase

List names = Arrays.asList("java","spring");

names.stream()
    .map(String::toUpperCase)
    .forEach(System.out::println);

1️⃣6️⃣ Find second highest number

int second = list.stream()
    .sorted(Comparator.reverseOrder())
    .skip(1)
    .findFirst()
    .get();

1️⃣7️⃣ Group numbers by even/odd

Map> grouped =
    list.stream()
        .collect(Collectors.groupingBy(n -> n % 2 == 0));

1️⃣8️⃣ Join strings with comma

String result = names.stream()
    .collect(Collectors.joining(","));

1️⃣9️⃣ Flatten nested list

List> nested =
    Arrays.asList(Arrays.asList(1,2), Arrays.asList(3,4));

nested.stream()
    .flatMap(Collection::stream)
    .forEach(System.out::println);

2️⃣0️⃣ Parallel stream example

list.parallelStream()
    .forEach(System.out::println);

Interview Summary

  • filter() → select elements
  • map() → transform elements
  • reduce() → combine elements
  • collect() → gather results
  • distinct() → remove duplicates
  • Streams are lazy and cannot be reused

0 comments

Leave a comment