15+ Coding Based Java 8 Coding Interview Question

In this Page contains the Most important coding questions related to Java 8. There are lots of new features added in Java 8 which has also changed some syntax.

If you are working on old Java then you should practice a lot of programs to accept the changes made in Java 8. Also during the interview, Interviewer will surely ask you to write some programs on Java 8.

So to clear the coding round or to solve the coding question asked you to write in Java 8, you should also prepare for writing the code. 

Java 8 coding interview questions

Q1). Java 8 Program to add prefixe and suffix to the String?

Ans:

To write a program in java to add prefix and suffix in a given String we will use StringJoiner class, a newly added in Java 8.

In the below program We will be adding “#“ and “#” as a prefix and suffix in the string.

Program

import java.util.StringJoiner;
public class Main {
    public static void main(String[] args) {
        StringJoiner stringJoiner = new StringJoiner(",", "#", "#");
        stringJoiner.add("Interview");
        stringJoiner.add("Questions");
        stringJoiner.add("Answers");
        System.out.println("String after adding # in suffix and prefix :");
        System.out.println(stringJoiner);
    }
}

Output

String after adding # in suffix and prefix :
#Interview,Questions,Answers#

Q2). Java 8 Program to Print ten random numbers using forEach?

Ans:

Below is the program to generate 10 random number using forEach. Here we are using a Random class to generate Random number.

Program

import java.util.Random;
public class Main {    
  public static void main(String[] args) {    
    Random random = new Random();
    random.ints().limit(10).forEach(System.out::println);
  }    
} 

Output

2095408578
-489399752
36994816
-2142661
-205103943
-93934163
-1884588823
-373986104
-909012198
1134601393

Q3). Java 8 program to iterate a Stream using the forEach method?

Ans:

In the below program, we have created a List of array types. And We are iterating this using forEach after converting into the stream.

Program to iterate stream using forEach in java 8

import java.util.*;
public class Main {
    public static void main(String[] args) {
        List<String> str = Arrays.asList("Hello","Interview","Questions","Answers");
        str.stream().forEach(System.out::println);
    }
}

Output

Hello
Interview
Questions
Answers

Q4). Java 8 program to find the Minimum number of a Stream?

Ans:

To find the minimum number in stream, we will use comparator.comparing() method which will take integer value from Stream.

Below is the program to find the minimum in Stream.

Program 1

import java.util.Comparator;
import java.util.stream.*;
public class Main {
    public static void main(String[] args) {
      Integer min = Stream.of(1, 2, 3, 4, 5, 6,7)
                        .min(Comparator.comparing(Integer::valueOf))
                        .get();
    System.out.println("The Minimum number is: " + min);
    }
}

Output

The Minimum number is: 1

Program 2

import java.util.*;
class Main{
    public static void main(String[] args) {
        int arr[] = {1,2,3,4,5};
        int min = Arrays.stream(arr).min().getAsInt();
        System.out.println(min);
    }
}

Output

The Minimum number is: 1

Q5). Java 8 program to find the Maximum number of a Stream?

Ans:

To find the Maximum number in the stream, we will use comparator.comparing() method which will take integer value from Stream.

Below is the program to find Maximum in Stream.

Program

import java.util.Comparator;
import java.util.stream.*;
public class Main {
    public static void main(String[] args) {
      Integer max = Stream.of(1, 2, 3, 4, 5, 6,7)
                        .max(Comparator.comparing(Integer::valueOf))
                        .get();
    System.out.println("The Maximum number is: " + max);
    }
}

Output

The Maximum number is: 7

Q6) Java 8 program to Count Strings whose length is greater than 3 in List?

Ans:

Below is the program where we have created a String List. We will be using filter and count function of java 8 to identify the string whose length is greater than 3.

Program

import java.util.*;
public class Main {
    public static void main(String[] args) {
        List<String> stringList = Arrays.asList("Hello","Interview","Questions","Answers","Ram","for");
        long count = stringList.stream().filter(str -> str.length() > 3).count();
        System.out.println("String count with greater than 3 digit : " + count);
    }
}

Output

String count with greater than 3 digit : 4

Q7). Java 8 program to Print Strings whose length is greater than 3 in List.

Ans :

Program

import java.util.*;
public class Main {
    public static void main(String[] args) {
        List<String> stringList = Arrays.asList("Hello","Interview","Questions","Answers","Ram","for");
        stringList.stream().filter(str -> str.length() > 3).forEach(System.out::println);
    }
}

Output

Hello
Interview
Questions
Answers

Q8) Java 8 program to multiply 3 to all elements in the list and print the list?

Ans:

To perform a multiply of 3 on each element of the list we will use the map() function. This function will perform the operation and will generate a new stream with an updated value.

Program

import java.util.*;
import java.util.stream.Collectors;  
public class Main {
    public static void main(String[] args) {
        List<Integer> integerList = Arrays.asList(1,2,3,4,5,6,7);
        System.out.println(integerList.stream().map(i -> i*3).collect(Collectors.toList()));
    }
}

Output

[3, 6, 9, 12, 15, 18, 21]

Q9) Java 8 program to perform concatenation on two Streams?

Ans:

To Perform concatenation on two stream, first we will create two streams and using the concat function we will perform string concatenation.

Program to concatenate two Streams

import java.util.*;
import java.util.stream.*; 
public class Main {
    public static void main(String[] args) {
        List<Integer> integerList1 = Arrays.asList(1,2,3,4);
        List<Integer> integerList2 = Arrays.asList(5,6,7);
        Stream<Integer> concatStream = Stream.concat(integerList1.stream(), integerList2.stream());
        concatStream.forEach(System.out::print);
    }
}

Output

1234567

Q10). Java 8 program to remove the duplicate elements from the list?

Ans:

To remove duplicate element form list we have used Collectors.toSet(). As we know that set can have only unique value, we are converting our list to set so that we can have only unique value.

Program

import java.util.*;
import java.util.stream.*; 
import java.util.stream.Collectors;  
public class Main {
    public static void main(String[] args) {
        List<Integer> integerList = Arrays.asList(1,2,3,4,1,2,3);
        System.out.println("After removing duplicates : ");
        integerList.stream().collect(Collectors.toSet()).forEach(System.out::print);
    }
} 

Output

After removing duplicates : 
1234

Program 2:

import java.util.*;
import java.util.stream.*;
public class Main
{
	public static void main(String[] args) {
	    List<Integer> list = new ArrayList<>(
            Arrays.asList(1, 10, 1, 2, 2, 3, 10, 3, 3, 4, 5, 5));
	    list.stream().distinct().collect(Collectors.toList()).forEach(System.out::println);;
	}
}

Output

1
10
2
3
4
5

Q11). Print current date and time in Java 8 Date and Time API?

Ans: To get the current date and time we are using LocalDate, LocalTime, and LocalDateTime API provided in Java 8.

Program

public class Main {
    public static void main(String[] args) {
        System.out.println("Current Date: " + java.time.LocalDate.now());
        System.out.println("Current Time: " + java.time.LocalTime.now());
        System.out.println("Current Date and Time: " + java.time.LocalDateTime.now());
    }
}

Output

Current Date: 2023-07-06
Current Time: 13:18:05.516980
Current Date and Time: 2023-07-06T13:18:05.517781

Q12). Java 8 program to Sort the given list?

Ans: To Sort the integer element given in List, we will use the sorted() method which will sort the List elements.

Program 

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().sorted().forEach(System.out::println);
    }
}

Output

1
2
3
4
5
6
7

Q13). Write a Java 8 program to get the sum of all numbers present in a list.

Ans:

Program

import java.util.*;
public class Main {
    public static void main(String[] args) {
       List<Integer> integerList = Arrays.asList(4,5,6,7,1,2,3);
       System.out.println(integerList.stream().mapToInt(Integer::intValue).sum());
    }
}

Output

28

Q14) Java 8 program to perform cube on list elements and filter numbers greater than 50.

Ans:

Program

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