Monday, 24 July 2023

Java 8 Stream Coding Interview questions and answers

      Before attending any interview need to look into the Stream coding questions beacuse Stream API is an important topic in java 8.

Below are the java 8 stream coding questions and answers.

1) Write a program to print the list of elements from a list using java 8 stream

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

public class Java8Stream {
	public static void main(String[] args) {
              List<String> list = Arrays.asList(new String[] {"ABC", "BCD", "CDE"});
	      list.stream().forEach(s -> System.out.println(s));
	}
}

Output:- ABC
              BCD
              CDE

2) Write a program to convert lowercase string element of a list into uppercase.

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

public class Java8Stream {
     public static void main(String[] args) {
	   List<String> list = Arrays.asList(new String[] {"Abc", "bcd", "cde"});
	   list.stream().map(s-> s.toUpperCase()).forEach(
				s -> System.out.println(s));
     }
}

Output:- ABC
              BCD
              CDE

3) Print a list of strings those start with letter "A" and return updated list.

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

public class Java8Stream {
	public static void main(String[] args) {
		List<String> list = Arrays.asList(new String[] {"Abc", "bcd", "cde"});
		List<String> updatedList = list.stream().filter(s-> s.startsWith("A"))
				.collect(Collectors.toList());
		updatedList.stream().forEach(s -> System.out.println(s));
	}
}

Output:- Abc

4) Write a code to calculate the summation of the integers from the list.

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

public class Java8Stream {
	public static void main(String[] args) {
		List<Integer> list = Arrays.asList(new Integer[] {1,2,3, 4,5});
		//Using lambda expression
		Integer sum = list.stream().mapToInt(i -> i).sum();
		System.out.println(sum);
		
		//Using Integer valueOf method
		Integer sumOfIntegers = list.stream().mapToInt(Integer::valueOf).sum();
		System.out.println(sumOfIntegers);
		
		//Using Integer intValue method
		Integer sumOfIntegerValue = list.stream().mapToInt(Integer::intValue).sum();
		System.out.println(sumOfIntegerValue);	
	}
}

Output:- 15
              15
              15

5) Create a Map object from List using Stream API

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

public class Java8Stream {
	public static void main(String[] args) {
		List<String> list = Arrays.asList(new String[] {"Ab", "Bcd", "Ce"});
		Map<String, Integer> map = list.stream().collect(Collectors.toMap(
				s->s, s->s.length()));
		map.forEach((x,v) -> System.out.println("key - "+x+", value - "+v));
	}
}

Output:-
key - Ab, value - 2
key - Ce, value - 2
key - Bcd, value - 3

6) Write a program to list the distinct integers from list or remove duplicates from the list.

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

public class Java8Stream {
	public static void main(String[] args) {
		List<Integer> list = Arrays.asList(new Integer[] {1, 2, 1,4});
		List<Integer> listWithDistinct = list.stream().distinct().collect(Collectors.toList());
		listWithDistinct.stream().forEach(s-> System.out.println(s));
	}
}

Output:-1
             2
             4

7) Write a program to find the Minimum number from a Stream/list.

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

public class Java8Stream {
	public static void main(String[] args) {
	     List<Integer> list = Arrays.asList(new Integer[] {1, 2,3,4});
	     Integer minNumber = list.stream().mapToInt(s->s).min().getAsInt();
	     System.out.println(minNumber);
	}
}

Output:-1

8) Write a program to find the Maximum number from a Stream/list.

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

public class Java8Stream {
	public static void main(String[] args) {
	     List<Integer> list = Arrays.asList(new Integer[] {1, 2,3,4});
	     Integer maxNumber = list.stream().mapToInt(s->s).max().getAsInt();
	     System.out.println(maxNumber);
	}
}

Output:-4

9) Write a program to sort the given list using java 8 stream.

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

public class Java8Stream {
	public static void main(String[] args) {
	     List<Integer> list = Arrays.asList(new Integer[] {8, 2,3,4});
	     List<Integer> sortedList = list.stream().sorted().collect(Collectors.toList());
	     sortedList.stream().forEach(s-> System.out.println(s));
	}
}

Output:-2
             3
             4
             8

10) Write a program to sort the given list in descending order using stream.

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

public class Java8Stream {
	public static void main(String[] args) {
		List<Integer> list = Arrays.asList(new Integer[] {8, 2,3,4});
		List<Integer> sortedList = list.stream().sorted(
				Collections.reverseOrder()).collect(Collectors.toList());
		sortedList.stream().forEach(s-> System.out.println(s));
	}
}

Output:-8
             4
             3
             2

11) Write a program to count the total number of integers in the list.

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

public class Java8Stream {
	public static void main(String[] args) {
	     List<Integer> list = Arrays.asList(new Integer[] {8, 2,3,4});
	     long count = list.stream().count();
	     System.out.println(count);
	}
}

Output:-4

12)  Convert List<List<String>> into List<String> using flatMap method.

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

public class Java8Stream {
	public static void main(String[] args) {
	     List<List<Integer>> listListOfStrings = new ArrayList<List<Integer>>();
	     List<Integer> list = Arrays.asList(new Integer[] {8, 2,3,4});
	     listListOfStrings.add(list);
	     //use flatMap method
	     List<Integer> newList = listListOfStrings.stream().flatMap(
				s-> s.stream()).collect(Collectors.toList());
	     newList.stream().forEach(s-> System.out.println(s));
	}
}

Output:-8
             2
             3
             4

13) Group by a list element and display the total count of the element using Collectors groupingBy method.

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

public class Java8Stream {
	public static void main(String[] args) {
	     List<String> letters = Arrays.asList("A", "B", "C","A", "D", "C", "D");
	     Map<String, Long> mapWithLetterCount = letters.stream().collect(
	                        Collectors.groupingBy(
	                                Function.identity(), Collectors.counting()));  
	     System.out.println(mapWithLetterCount);   
	}
}

Output:- {A=2, B=1, C=2, D=2}

14)  Find the first element from the given list using java 8 stream.

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

public class Java8Stream {
	public static void main(String[] args) {
	     List<String> letters = Arrays.asList("A", "B", "C","A", "D", "C", "D");
	     String firstElement = letters.stream().findFirst().orElse("Default");  
	     System.out.println(firstElement);   
	}
}

Output:-A

15)  anyMatch code example - if any one of the list element matches with given character "A" then return true.

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

public class Java8Stream {
	public static void main(String[] args) {
             List<String> letters = Arrays.asList("A", "B", "C", "D");
	     boolean foundElement = letters.stream().anyMatch(s-> s.equalsIgnoreCase("A"));  
	     System.out.println(foundElement);   
	}
}

Output:-true

16) allMatch code example - if all of the elements matches with the given element say "A" then return true else false.

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

public class Java8Stream {
	public static void main(String[] args) {
             List<String> letters = Arrays.asList("A", "B", "C", "D");
	     boolean foundMatches = letters.stream().allMatch(s-> s.equalsIgnoreCase("A"));  
	     System.out.println(foundMatches);   
	}
}

Output:-false

17) Write a program to remove the duplicates from the list using Collectors toSet method.

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

public class Java8Stream {
	public static void main(String[] args) {
	     List<String> letters = Arrays.asList("A", "B", "C", "D", "A", "B");
	     Set<String> nonDuplicateSet = letters.stream().collect(Collectors.toSet());  
	     nonDuplicateSet.stream().forEach(s-> System.out.println(s));
	}
}

Output:-A
             B
             C
             D

     
Thank you for reading the post.

Reference Posts:-

No comments:

Post a Comment