0% found this document useful (0 votes)
18 views

Java8 Stream API Interview Program Questions Experienced

Uploaded by

happymailtosri
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Java8 Stream API Interview Program Questions Experienced

Uploaded by

happymailtosri
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Java 8 Stream API Interview Program

Questions for Experienced


1. Write a program to find the sum of all even numbers in a list using Stream
API.

```java
import java.util.Arrays;
import java.util.List;

public class SumEvenNumbers {


public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
int sum = numbers.stream()
.filter(n -> n % 2 == 0)
.mapToInt(Integer::intValue)
.sum();
System.out.println("Sum of even numbers: " + sum);
}
}
```

2. Write a program to convert a list of strings to uppercase using Stream API.

```java
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class ConvertToUpperCase {


public static void main(String[] args) {
List<String> words = Arrays.asList("hello", "world", "java", "stream");
List<String> upperCaseWords = words.stream()
.map(String::toUpperCase)
.collect(Collectors.toList());
System.out.println(upperCaseWords);
}
}
```

3. Write a program to filter a list of integers to only include numbers greater


than 10 using Stream API.

```java
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class FilterNumbers {


public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(5, 10, 15, 20, 25);
List<Integer> filteredNumbers = numbers.stream()
.filter(n -> n > 10)
.collect(Collectors.toList());
System.out.println(filteredNumbers);
}
}
```

4. Write a program to sort a list of strings in natural order using Stream API.

```java
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class SortStrings {


public static void main(String[] args) {
List<String> words = Arrays.asList("banana", "apple", "cherry", "date");
List<String> sortedWords = words.stream()
.sorted()
.collect(Collectors.toList());
System.out.println(sortedWords);
}
}
```
5. Write a program to count the number of empty strings in a list using Stream
API.

```java
import java.util.Arrays;
import java.util.List;

public class CountEmptyStrings {


public static void main(String[] args) {
List<String> words = Arrays.asList("apple", "", "banana", "", "cherry");
long count = words.stream()
.filter(String::isEmpty)
.count();
System.out.println("Number of empty strings: " + count);
}
}
```

6. Write a program to find the maximum value in a list of integers using Stream
API.

```java
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

public class MaxValue {


public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
Optional<Integer> max = numbers.stream()
.max(Integer::compare);
max.ifPresent(value -> System.out.println("Maximum value: " + value));
}
}
```

You might also like