In this tutorial, We will discuss top Java 8 coding and programming interview questions and answers using Stream API functions.
1. Given a list of integers, find out all the even numbers that exist in the list using Stream functions?
import java.util.stream.*;
public class EvenNumberTest{
public static void main(String args[]) {
List<Integer> list = Arrays.asList(20,15,80,49,25,18,36);
list.stream()
.filter(n -> n%2 == 0)
.forEach(System.out::println);
}
}
Output:
20, 80, 18, 36
2. Given a list of integers, find out all the numbers starting with 1 using Stream functions?
import java.util.*;
import java.util.stream.*;
public class NumberStartingWithOneTest{
public static void main(String args[]) {
List<Integer> myList = Arrays.asList(100,99,35,20,15);
myList.stream()
.map(s -> s + "") // Convert integer to String
.filter(s -> s.startsWith("1"))
.forEach(System.out::println);
}
}
Output:
100, 15
3. How to find duplicate elements in a given integers list in java using Stream functions?
import java.util.*;
import java.util.stream.*;
public class DuplicateElements {
public static void main(String args[]) {
List<Integer> myList = Arrays.asList(20,15,18,25,98,98,32,15);
Set<Integer> set = new HashSet();
myList.stream()
.filter(n -> !set.add(n))
.forEach(System.out::println);
}
}
Output:
98, 15
4. Given the list of integers, find the first element of the list using Stream functions?
import java.util.*;
import java.util.stream.*;
public class FindFirstElement{
public static void main(String args[]) {
List<Integer> myList = Arrays.asList(15,8,98,32,18);
myList.stream()
.findFirst()
.ifPresent(System.out::println);
}
}
Output:
15
5. Given a list of integers, find the total number of elements present in the list using Stream functions?
import java.util.*;
import java.util.stream.*;
public class FindTheTotalNumberOfElements{
public static void main(String args[]) {
List<Integer> myList = Arrays.asList(10,25,98,15,8,49,98,32);
long count = myList.stream()
.count();
System.out.println(count);
}
}
Output:
8
6. Given a list of integers, find the maximum value element present in it using Stream functions?
import java.util.*;
import java.util.stream.*;
public class FindMaxElement{
public static void main(String args[]) {
List<Integer> myList = Arrays.asList(25,8,49,25,88,88,32,25);
int max = myList.stream()
.max(Integer::compare)
.get();
System.out.println(max);
}
}
Output:
88
7. Given a String, find the first non-repeated character in it using Stream functions?
import java.util.stream.*;
import java.util.function.Function;
public class FirstNonRepeated{
public static void main(String args[]) {
String input = "Java articles are Awesome";
Character result = input.chars() // Stream of String
.mapToObj(s -> Character.toLowerCase(Character.valueOf((char) s)))
.collect(Collectors.groupingBy(Function.identity(), LinkedHashMap::new, Collectors.counting()))
.entrySet()
.stream()
.filter(entry -> entry.getValue() == 1L)
.map(entry -> entry.getKey())
.findFirst()
.get();
System.out.println(result);
}
}
Output:
j
8. Given a String, find the first repeated character in it using Stream functions?
import java.util.*;
import java.util.stream.*;
import java.util.function.Function;
public class FirstRepeated{
public static void main(String args[]) {
String input = "Java Articles are Awesome";
Character result = input.chars() // Stream of String
// First convert to Character object and then to lowercase
.mapToObj(s -> Character.toLowerCase(Character.valueOf((char) s)))
//Store the chars in map with count
.collect(Collectors.groupingBy(Function.identity(), LinkedHashMap::new, Collectors.counting()))
.entrySet()
.stream()
.filter(entry -> entry.getValue() > 1L)
.map(entry -> entry.getKey())
.findFirst()
.get();
System.out.println(result);
}
}
Output:
a
9. Given a list of integers, sort all the values present in it using Stream functions?
import java.util.*;
import java.util.stream.*;
import java.util.function.Function;
public class SortValues{
public static void main(String args[]) {
List<Integer> myList = Arrays.asList(10,15,8,49,25,98,98,32,15);
myList.stream()
.sorted()
.forEach(System.out::println);
}
}
Output:
8
10
15
15
25
32
49
98
98
11. Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.
public boolean containsDuplicate(int[] nums) {
List<Integer> list = Arrays.stream(nums)
.boxed()
.collect(Collectors.toList());
Set<Integer> set = new HashSet<>(list);
if(set.size() == list.size()) {
return false;
}
return true;
}
Input: nums = [1,2,3,1]
Output: true
Input: nums = [1,2,3,4]
Output: false
12. How will you get the current date and time using Java 8 Date and Time API?
class Java8 {
public static void main(String[] args) {
//Used LocalDate API to get the date
System.out.println("Current Local Date: " + java.time.LocalDate.now());
//Used LocalTime API to get the time
System.out.println("Current Local Time: " + java.time.LocalTime.now());
//Used LocalDateTime API to get both date and time
System.out.println("Current Local Date and Time: " + java.time.LocalDateTime.now());
}
}
13. Write a Java 8 program to concatenate two Streams?
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
public class Java8 {
public static void main(String[] args) {
List<String> list1 = Arrays.asList("Java", "8");
List<String> list2 = Arrays.asList("explained", "through", "programs");
// Concatenated the list1 and list2 by converting them into Stream
Stream<String> concatStream = Stream.concat(list1.stream(), list2.stream());
// Printed the Concatenated Stream
concatStream.forEach(str -> System.out.print(str + " "));
}
}
14. Java 8 program to perform cube on list elements and filter numbers greater than 50.
import java.util.*;
public class Main {
public static void main(String[] args) {
List<Integer> integerList = Arrays.asList(4,5,6,7,1,2,3);
integerList.stream()
.map(i -> i*i*i)
.filter(i -> i>50)
.forEach(System.out::println);
}
}
Output:
64
125
216
343
15. Write a Java 8 program to sort an array and then convert the sorted array into Stream?
import java.util.Arrays;
public class Java8 {
public static void main(String[] args) {
int arr[] = { 99, 55, 203, 99, 4, 91 };
Arrays.parallelSort(arr);
/* Converted it into Stream and then print using forEach */
Arrays.stream(arr).forEach(n -> System.out.print(n + " "));
}
}
16. How to use map to convert object into Uppercase in Java 8?
public class Java8 {
public static void main(String[] args) {
List<String> nameLst = names.stream()
.map(String::toUpperCase)
.collect(Collectors.toList());
System.out.println(nameLst);
}
}
output:
AA, BB, CC, DD
17. How to convert a List of objects into a Map by considering duplicated keys and store them in sorted order?
public class TestNotes {
public static void main(String[] args) {
List<Notes> noteLst = new ArrayList<>();
noteLst.add(new Notes(1, "note1", 11));
noteLst.add(new Notes(2, "note2", 22));
noteLst.add(new Notes(3, "note3", 33));
noteLst.add(new Notes(4, "note4", 44));
noteLst.add(new Notes(5, "note5", 55));
noteLst.add(new Notes(6, "note4", 66));
Map<String, Long> notesRecords = noteLst.stream()
// sorting is based on TagId 55,44,33,22,11
.sorted(Comparator.comparingLong(Notes::getTagId).reversed())
// consider old value 44 for dupilcate key and it keeps order
.collect(Collectors.toMap(Notes::getTagName, Notes::getTagId,
(oldValue, newValue) -> oldValue,LinkedHashMap::new));
System.out.println("Notes : " + notesRecords);
}
}
18. How to count each element/word from the String ArrayList in Java8?
public class TestNotes {
public static void main(String[] args) {
List<String> names = Arrays.asList("AA", "BB", "AA", "CC");
Map<String,Long> namesCount = names
.stream()
.collect( Collectors.groupingBy( Function.identity() , Collectors.counting() ));
System.out.println(namesCount);
}
}
Output:
{CC=1, BB=1, AA=2}
19. How to find only duplicate elements with its count from the String ArrayList in Java8?
public class TestNotes {
public static void main(String[] args) {
List<String> names = Arrays.asList("AA", "BB", "AA", "CC");
Map<String,Long> namesCount = names
.stream().filter(x->Collections.frequency(names, x)>1)
.collect(Collectors.groupingBy (Function.identity(), Collectors.counting()));
System.out.println(namesCount);
}
}
Output:
{AA=2}
20. How to check if list is empty in Java 8 using Optional, if not null iterate through the list and print the object?
.orElseGet(Collections::emptyList)
.stream().filter(Objects::nonNull)
.map(note -> Notes::getTagName)
21. Write a Program to find the Maximum element in an array?
public static int findMaxElement(int[] arr) {
return Arrays.stream(arr).max().getAsInt();
}
Input: 12,19,20,88,00,9
output: 88
22. Write a program to print the count of each character in a String?
public static void findCountOfChars(String s) {
Map<String, Long> map = Arrays.stream(s.split(""))
.map(String::toLowerCase)
.collect(Collectors
.groupingBy(str -> str, LinkedHashMap::new, Collectors.counting()));
}
Input: String s = "string data to count each character";
Output: {s=1, t=5, r=3, i=1, n=2, g=1, =5, d=1, a=5, o=2, c=4, u=1, e=2, h=2}