Java 8 Coding and Programming Interview Questions and Answers

Java 8 Coding and Programming Interview Questions and Answers

In this tutorial, I will be sharing the top 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.

  1. To find all the even numbers in a list of integers using Java 8 Stream functions, you can use the filter and collect methods. Here’s an example:
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class EvenNumbers {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

        // Find all even numbers using Stream API
        List<Integer> evenNumbers = numbers.stream()
                                            .filter(number -> number % 2 == 0)
                                            .collect(Collectors.toList());

        System.out.println("Even numbers: " + evenNumbers);
    }
}

In this example, the filter method is used to select only the numbers that are even. The collect method gathers the even numbers into a new list, which is then printed.

2. To find all the numbers starting with “1” in a list of integers using Java 8 Stream functions, you can use the filter method along with converting each number to a string. Here’s an example:

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

public class NumbersStartingWithOne {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(10, 23, 12, 34, 15, 61, 17, 81, 19, 100);

        // Find all numbers starting with "1" using Stream API
        List<Integer> numbersStartingWithOne = numbers.stream()
                                                      .filter(number -> String.valueOf(number).startsWith("1"))
                                                      .collect(Collectors.toList());

        System.out.println("Numbers starting with 1: " + numbersStartingWithOne);
    }
}

In this example, the filter method is used to select numbers where the string representation starts with “1”. The collect method gathers these numbers into a new list, which is then printed.

To find duplicate elements in a list of integers using Java 8 Stream functions, you can use the filter and collect methods, along with Collections.frequency or a separate set to track seen and duplicate elements. Here’s an example using a set:

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

public class DuplicateElements {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 10, 3);

        // Find duplicate elements using Stream API
        Set<Integer> seen = new HashSet<>();
        List<Integer> duplicates = numbers.stream()
                                          .filter(n -> !seen.add(n))
                                          .collect(Collectors.toList());

        System.out.println("Duplicate elements: " + duplicates);
    }
}

In this example:

  • seen.add(n) attempts to add each number to the set.
  • If add returns false, it means the number is already in the set, indicating a duplicate.
  • The filter method retains only these duplicates.
  • collect gathers the duplicates into a new list, which is then printed.
Aadiya Soni
https://www.whatnexthelp.com

Leave a Reply

Your email address will not be published. Required fields are marked *