Top 10 Java 8 Coding and Programming Interview Questions and Answers

It has been 6 years since Java 8 was released. I have already shared the Java 8 Interview questions and answers. In this tutorial, I will be sharing the top 10 Java 8 coding and programming interview questions and answers. I have only used Stream API functions to solve the below questions. Please bookmark this page as I will keep adding more questions to it.

Read Also: Java 8 Interview Questions and Answers

Q1 Given a list of integers, find out all the even numbers exist in the list using Stream functions?

import java.util.*;
import java.util.stream.*;
public class JavaHungry {
    public static void main(String args[]) {
            List<Integer> myList = Arrays.asList(10,15,8,49,25,98,32);
            myList.stream()
                  .filter(n -> n%2 == 0)
                  .forEach(System.out::println);
    }
}

Output:
10, 8, 98, 32


Q2 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 JavaHungry {
    public static void main(String args[]) {
            List<Integer> myList = Arrays.asList(10,15,8,49,25,98,32);
            myList.stream()
                  .map(s -> s + "") // Convert integer to String
                  .filter(s -> s.startsWith("1"))
                  .forEach(System.out::println);
    }
}

Output:
10, 15



Q3 How to find duplicate elements in a given integers list in java using Stream functions?

import java.util.*;
import java.util.stream.*;
public class JavaHungry {
    public static void main(String args[]) {
            List<Integer> myList = Arrays.asList(10,15,8,49,25,98,98,32,15);
            Set<Integer> set = new HashSet();
            myList.stream()
                  .filter(n -> !set.add(n))
                  .forEach(System.out::println);
    }
}

Output:
98, 15

Q4 Given the list of integers, find the first element of the list using Stream functions?

import java.util.*;
import java.util.stream.*;
public class JavaHungry {
    public static void main(String args[]) {
            List<Integer> myList = Arrays.asList(10,15,8,49,25,98,98,32,15);
            myList.stream()
                  .findFirst()
                  .ifPresent(System.out::println);
    }
}

Output:
10

Q5 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 JavaHungry {
    public static void main(String args[]) {
            List<Integer> myList = Arrays.asList(10,15,8,49,25,98,98,32,15);
            long count =  myList.stream()
                                .count();
            System.out.println(count);                    
    }
}

Output:
9

Q6 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 JavaHungry {
    public static void main(String args[]) {
            List<Integer> myList = Arrays.asList(10,15,8,49,25,98,98,32,15);
            int max =  myList.stream()
                             .max(Integer::compare)
                             .get();
            System.out.println(max);                    
    }
}

Output:
98

Q7 Given a String, find the first non-repeated character in it using Stream functions?

import java.util.*;
import java.util.stream.*;
import java.util.function.Function;

public class JavaHungry {
    public static void main(String args[]) {
            String input = "Java Hungry Blog Alive is Awesome";

            Character result = input.chars() // Stream of String       
                                    .mapToObj(s -> Character.toLowerCase(Character.valueOf((char) s))) // First convert to Character object and then to lowercase         
                                    .collect(Collectors.groupingBy(Function.identity(), LinkedHashMap::new, Collectors.counting())) //Store the chars in map with count 
                                    .entrySet()
                                    .stream()
                                    .filter(entry -> entry.getValue() == 1L)
                                    .map(entry -> entry.getKey())
                                    .findFirst()
                                    .get();
            System.out.println(result);                    
    }
}

Output:
j

Q8 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 JavaHungry {
    public static void main(String args[]) {
            String input = "Java Hungry Blog Alive is Awesome";

            Character result = input.chars() // Stream of String       
                                    .mapToObj(s -> Character.toLowerCase(Character.valueOf((char) s))) // First convert to Character object and then to lowercase         
                                    .collect(Collectors.groupingBy(Function.identity(), LinkedHashMap::new, Collectors.counting())) //Store the chars in map with count 
                                    .entrySet()
                                    .stream()
                                    .filter(entry -> entry.getValue() > 1L)
                                    .map(entry -> entry.getKey())
                                    .findFirst()
                                    .get();
            System.out.println(result);                    
    }
}

Output:
a

Q9 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 JavaHungry {
    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


Q10 Given a list of integers, sort all the values present in it in descending order using Stream functions?

import java.util.*;
import java.util.stream.*;
import java.util.function.Function;

public class JavaHungry {
    public static void main(String args[]) {
            List<Integer> myList = Arrays.asList(10,15,8,49,25,98,98,32,15);

             myList.stream()
                   .sorted(Collections.reverseOrder())
                   .forEach(System.out::println);
    }
}

Output:
98
98
49
32
25
15
15
10
8


That's all for today regarding 10 java 8 coding and programming interview questions and answers. Please mention in the comments in case you know any other better approach to solving the above questions.

About The Author

Subham Mittal has worked in Oracle for 3 years .
For more java articles ,Click here to Subscribe JavaHungry