JAVA8 Coding Interview Questions
JAVA8 Coding Interview Questions
https://blog.devgenius.io/java-8-coding-and-programming-interview-questions-and-answers-
62512c44f062
1. Given a list of integers, find out all the even numbers that exist in the list using Stream functions
list.stream()
.filter(n->n%2==0)
.forEach(System.out::println);
.collect(Collectors.partitioningBy(num->num%2==0));
System.out.println(list1);
Notes:
Stream<Integer> java.util.stream.IntStream.boxed()
Returns a Stream consisting of the elements of this stream, each boxed to an Integer.
Returns a Collector which partitions the input elements according to a Predicate, and organizes them
into a Map<Boolean, List<T>>. The returned Map always contains mappings for both false and true keys.
There are no guarantees on the type, mutability, serializability, or thread-safety of the Map or List
returned.
// Given a list of integers, find out all the numbers starting with 1 using Stream functions?
list.stream()
.filter(n->n.startsWith("1"))
.forEach(System.out::println);
list.stream()
.filter(n->n.startsWith("2"))
.forEach(System.out::println);
list.stream()
.filter(n->n.startsWith("3"))
.forEach(System.out::println);
Note:
Map->Returns a stream consisting of the results of applying the given function to the elements of this
stream.
Returns a stream consisting of the elements of this stream that match the given predicate.
//3. How to find duplicate elements in a given integers list in java using Stream functions?
.filter(n-> !set.add(n))
.forEach(System.out::println);
Note: