[Go to site: main page, start]

0% found this document useful (0 votes)
45 views24 pages

Java 8 Interview Questions & Answers

jhg
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views24 pages

Java 8 Interview Questions & Answers

jhg
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

9/15/25, 2:42 PM Java 8 Coding and Programming Interview Questions and Answers | by Anusha SP | Dev Genius

Open in app Sign up Sign in

Search

Writing is for everyone.


Register for Medium Day

Dev Genius · Follow publication

Java 8 Coding and Programming Interview


Questions and Answers
8 min read · Jan 31, 2023

Anusha SP Follow

Listen Share

It has been 8 years since Java 8 was released. I have already shared the Java 8
Interview Questions and Answers and also Java 8 Stream API Interview Questions
and Answers . You can also find the Java 8 — Real-Time Coding Interview Questions
and Answers.
[Link] 1/24
9/15/25, 2:42 PM Java 8 Coding and Programming Interview Questions and Answers | by Anusha SP | Dev Genius

Get Anusha SP’s stories in your inbox


Join Medium for free to get updates from this writer.

Enter your email

Subscribe

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. Given a list of integers, find out all the even numbers that exist in the list using
Stream functions?

import [Link].*;
import [Link].*;

public class EvenNumber{


public static void main(String args[]) {
List<Integer> list = [Link](10,15,8,49,25,98,32);
[Link]()
.filter(n -> n%2 == 0)
.forEach([Link]::println);

/* or can also try below method */

/* When numbers are given as Array int[] arr = {10,15,8,49,25,98,32}; */

Map<Boolean, List<Integer>> list = [Link](arr).boxed()


.collect([Link](num -> num % 2 == 0));
[Link](list);
}
}

Output:
10, 8, 98, 32

2. Given a list of integers, find out all the numbers starting with 1 using Stream
functions?

[Link] 2/24
9/15/25, 2:42 PM Java 8 Coding and Programming Interview Questions and Answers | by Anusha SP | Dev Genius

import [Link].*;
import [Link].*;

public class NumberStartingWithOne{


public static void main(String args[]) {
List<Integer> myList = [Link](10,15,8,49,25,98,32);
[Link]()
.map(s -> s + "") // Convert integer to String
.filter(s -> [Link]("1"))
.forEach([Link]::println);

/* or can also try below method */

/* When numbers are given as Array int[] arr = {10,15,8,49,25,98,32}; */


List<String> list = [Link](arr).boxed()
.map(s -> s + "")
.filter(s -> [Link]("1"))
.collect([Link]());

[Link](list);
}
}

Output:
10, 15

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

f all repeated number */


{
ng args[]) {
= [Link](10,15,8,49,25,98,98,32,15);
ew HashSet();

[Link](n))
[Link]::println);

e the only repeated values in the list */

[Link] 3/24
9/15/25, 2:42 PM Java 8 Coding and Programming Interview Questions and Answers | by Anusha SP | Dev Genius

istinct/unique values */

outDuplicates() {
[Link](1, 1, 85, 6, 2, 3, 65, 6, 45, 45, 5662, 2582, 2, 2, 266, 666, 656);
).forEach(noDuplicateData -> [Link](noDuplicateData));

2582 266 666 656

distinct/unique values */

outDuplicates() {
[Link](1, 1, 85, 6, 2, 3, 65, 6, 45, 45, 5662, 2582, 2, 2, 266, 666, 656);
ashSet<>(myList);

to a list if needed
= [Link]().collect([Link]());

ents
[Link]::println);

85 2582 666 5662

istinct/unique values */

rray int[] arr = {10,15,8,49,25,98,98,32,15}; */

ream(arr).boxed().distinct()

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

import [Link].*;
import [Link].*;

public class FindFirstElement{


public static void main(String args[]) {
List<Integer> myList = [Link](10,15,8,49,25,98,98,32,15);
[Link]()
.findFirst()
.ifPresent([Link]::println);

[Link] 4/24
9/15/25, 2:42 PM Java 8 Coding and Programming Interview Questions and Answers | by Anusha SP | Dev Genius

/* or can also try below single line code */


/* When numbers are given as Array int[] arr = {10,15,8,49,25,98,98,32,15
[Link](arr).boxed().findFirst().ifPresent([Link]::print);
}
}

Output:
10

5. Given a list of integers, find the total number of elements present in the list
using Stream functions?

import [Link].*;
import [Link].*;

 public class FindTheTotalNumberOfElements{ 

public static void main(String args[]) {


List<Integer> myList = [Link](10,15,8,49,25,98,98,32,15);
long count = [Link]()
.count();
[Link](count);

/* or can also try below line code */


/* When numbers are given as Array int[] arr = {10,15,8,49,25,98,98,32,15}; */
[Link](arr).boxed().count();
}
}

Output:
9

 

6. Given a list of integers, find the maximum value element present in it using
Stream functions?

import [Link].*;
import [Link].*;

public class FindMaxElement{


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

[Link] 5/24
9/15/25, 2:42 PM Java 8 Coding and Programming Interview Questions and Answers | by Anusha SP | Dev Genius

int max = [Link]()


.max(Integer::compare)
.get();
[Link](max);

/* or we can try using below way */


/* When numbers are given as Array int[] arr = {10,15,8,49,25,98,98,32,15}; */

int maxdata = [Link](arr).boxed()


.max([Link]()).get();

[Link](maxdata);
}
}

Output:
98

7. Given a String, find the first non-repeated character in it using Stream


functions?

 import [Link].*; 

import [Link].*;
import [Link];

public class FirstNonRepeated{


public static void main(String args[]) {
String input = "Java articles are Awesome";

Character result = [Link]() // Stream of String


.mapToObj(s -> [Link]([Link]((char) s)))
.collect([Link]([Link](), LinkedHashMap::
.entrySet()
.stream()
.filter(entry -> [Link]() == 1L)
.map(entry -> [Link]())
.findFirst()
.get();
[Link](result);

/* or can also try using */

[Link]().mapToObj(c -> (char) c)


.filter(ch -> [Link](ch) == [Link](ch))
.findFirst().orElse(null);
}
}

[Link] 6/24
9/15/25, 2:42 PM Java 8 Coding and Programming Interview Questions and Answers | by Anusha SP | Dev Genius

Output:
j

8. Given a String, find the first repeated character in it using Stream functions?

import [Link].*;
import [Link].*;
import [Link];

public class FirstRepeated{


public static void main(String args[]) {
String input = "Java Articles are Awesome";

Character result = [Link]() // Stream of String


.mapToObj(s -> [Link](Characte
.collect([Link]([Link]
.entrySet()
.stream()
.filter(entry -> [Link]() > 1L)
.map(entry -> [Link]())
.findFirst()
.get();
[Link](result);

/* or can also try */

 Set<Character> seenCharacters = new HashSet<>(); 

return [Link]()
.mapToObj(c -> (char) c)
.filter(c -> ![Link](c))
.findFirst()
.orElse(null);
}
}

Output:
a

 

9. Given a list of integers, sort all the values present in it using Stream functions?

[Link] 7/24
9/15/25, 2:42 PM Java 8 Coding and Programming Interview Questions and Answers | by Anusha SP | Dev Genius

import [Link].*;
import [Link].*;
import [Link];

public class SortValues{


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

[Link]()
.sorted()
.forEach([Link]::println);

/* Or can also try below way */


/* When numbers are given as Array int[] arr = {10,15,8,49,25,98,98,32,15

[Link](arr).boxed().sorted().collect([Link]())
}
}

Output:
8
10
15
15
25
32
49
98
98

 

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

import [Link].*;
import [Link].*;
import [Link];

public class SortDescending{


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

[Link]()
.sorted([Link]())
.forEach([Link]::println);
}

[Link] 8/24
9/15/25, 2:42 PM Java 8 Coding and Programming Interview Questions and Answers | by Anusha SP | Dev Genius

Output:
98
98
49
32
25
15
15
10
8

11. Given an integer array nums , return true if any value appears at least twice in
the array, and return false if every element is distinct.

public boolean containsDuplicate(int[] nums) {


List<Integer> list = [Link](nums)
.boxed()
.collect([Link]());
Set<Integer> set = new HashSet<>(list);
if([Link]() == [Link]()) {
return false;
}
return true;

/* or can also try below way */


Set<Integer> setData = new HashSet<>();
return [Link](nums)
.anyMatch(num -> ![Link](num));
}

Input: nums = [1,2,3,1]


Output: true

Input: nums = [1,2,3,4]


Output: false

12. How will you get the current date and time using Java 8 Date and Time API?

class Java8 {
public static void main(String[] args) {
[Link]("Current Local Date: " + [Link]());

[Link] 9/24
9/15/25, 2:42 PM Java 8 Coding and Programming Interview Questions and Answers | by Anusha SP | Dev Genius

//Used LocalDate API to get the date


[Link]("Current Local Time: " + [Link]());
//Used LocalTime API to get the time
[Link]("Current Local Date and Time: " + [Link]
//Used LocalDateTime API to get both date and time
}
}

13. Write a Java 8 program to concatenate two Streams?

 

import [Link];
import [Link];
import [Link];

public class Java8 {


public static void main(String[] args) {

List<String> list1 = [Link]("Java", "8");


List<String> list2 = [Link]("explained", "through", "programs");

Stream<String> concatStream = [Link]([Link](), [Link]

// Concatenated the list1 and list2 by converting them into Stream

[Link](str -> [Link](str + " "));

// Printed the Concatenated Stream

}
}

 

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

import [Link].*;

public class Main {


public static void main(String[] args) {
List<Integer> integerList = [Link](4,5,6,7,1,2,3);
[Link]()
.map(i -> i*i*i)

[Link] 10/24
9/15/25, 2:42 PM Java 8 Coding and Programming Interview Questions and Answers | by Anusha SP | Dev Genius

.filter(i -> i>50)


.forEach([Link]::println);
}
}

Output:
64
125
216
343

15. Write a Java 8 program to sort an array and then convert the sorted array into
Stream?

import [Link];

public class Java8 {

public static void main(String[] args) {


int arr[] = { 99, 55, 203, 99, 4, 91 };
[Link](arr);
// Sorted the Array using parallelSort()

[Link](arr).forEach(n > [Link](n + " "));


/* Converted it into Stream and then
printed using forEach */
}
}

16. How to use map to convert object into Uppercase in Java 8?

public class Java8 {

public static void main(String[] args) {


List<String> nameLst = [Link]()
.map(String::toUpperCase)
.collect([Link]());
[Link](nameLst);
}
}

[Link] 11/24
9/15/25, 2:42 PM Java 8 Coding and Programming Interview Questions and Answers | by Anusha SP | Dev Genius

output:
AA, BB, CC, DD

17. How to convert a List of objects into a Map by considering duplicated keys and
store them in sorted order?

public class TestNotes {

public static void main(String[] args) {

List<Notes> noteLst = new ArrayList<>();


[Link](new Notes(1, "note1", 11));
[Link](new Notes(2, "note2", 22));
[Link](new Notes(3, "note3", 33));
[Link](new Notes(4, "note4", 44));
[Link](new Notes(5, "note5", 55));

[Link](new Notes(6, "note4", 66));

Map<String, Long> notesRecords = [Link]()


.sorted(Comparator
.comparingLong(Notes::getTagId)
.reversed()) // sorting is based on
.collect([Link]
(Notes::getTagName, Notes::getTagId
(oldValue, newValue) -> oldValue,Li
// consider old value 44 for dupilcate key
// it keeps order
[Link]("Notes : " + notesRecords);
}
}

 

18. How to count each element/word from the String ArrayList in Java8?

public class TestNotes {

public static void main(String[] args) {


List<String> names = [Link]("AA", "BB", "AA", "CC");
Map<String,Long> namesCount = names
.stream()
.collect(

[Link] 12/24
9/15/25, 2:42 PM Java 8 Coding and Programming Interview Questions and Answers | by Anusha SP | Dev Genius

[Link](
[Link](), [Link]()));
[Link](namesCount);
}
}

Output:
{CC=1, BB=1, AA=2}

19. How to find only duplicate elements with its count from the String ArrayList in
Java8?

public class TestNotes {


 
public static void main(String[] args)
List<String> names = [Link]("AA", "BB", "AA", "CC");
Map<String,Long> namesCount = names
.stream()
.filter(x->[Link](names, x)>1)
.collect([Link]
([Link](), [Link]()));
[Link](namesCount);

/*or you can also try using */

Map<String, Long> namesCount = [Link]()


.collect([Link]([Link](), Collectors.
.entrySet()
.stream()
.filter(entry -> [Link]() > 1)
.collect([Link]([Link]::getKey, [Link]::getValu
}
}

Output:
{AA=2}

 

20. How to check if list is empty in Java 8 using Optional, if not null iterate through
the list and print the object?

[Link] 13/24
9/15/25, 2:42 PM Java 8 Coding and Programming Interview Questions and Answers | by Anusha SP | Dev Genius

[Link](noteLst)
.orElseGet(Collections::emptyList) // creates empty immutable list:
.stream().filter(Objects::nonNull) //loop throgh each object and co
.map(note -> Notes::getTagName) // method reference, consider only
.forEach([Link]::println); // it will print tag names

 

21. Write a Program to find the Maximum element in an array?

public static int findMaxElement(int[] arr) {


return [Link](arr).max().getAsInt();
}

Input: 12,19,20,88,00,9
output: 88

22. Write a program to print the count of each character in a String?

public static void findCountOfChars(String s) {


Map<String, Long> map = [Link]([Link](""))
.map(String::toLowerCase)
.collect(Collectors
.groupingBy(str -> str,
LinkedHashMap::new, [Link]()));

// or you can also try using [Link]() instead of LinkedHashMap

Map<String, Long> mapObject = [Link]([Link](""))


.map(String::toLowerCase)
.collect([Link]([Link](), [Link]()));

Input: String s = "string data to count each character";


Output: {s=1, t=5, r=3, i=1, n=2, g=1, =5, d=1, a=5, o=2, c=4, u=1, e=2, h=2}

 

Here, we come to the end of Java 8 coding interview questions and answers. These
questions are very common in all the Java 8 interviews, so save and practice the
[Link] 14/24
9/15/25, 2:42 PM Java 8 Coding and Programming Interview Questions and Answers | by Anusha SP | Dev Genius

programs accordingly.

Thank you for reading the article. Please clap, share and comment. it will encourage me to
write more such articles. Do share your valuable suggestions, I appreciate your honest
feedback!!!

Java Java8 Java Interview Questions Programming Stream

Follow

Published in Dev Genius


28K followers · Last published 5 days ago

Coding, Tutorials, News, UX, UI and much more related to development

Follow

Written by Anusha SP
2K followers · 4 following

I write what I read and practice !!!

Responses (24)

Write a response

What are your thoughts?

[Link] 15/24
9/15/25, 2:42 PM Java 8 Coding and Programming Interview Questions and Answers | by Anusha SP | Dev Genius

Teja Madhu Kiran


Feb 22, 2024

[Link]()
.filter(n -> ![Link](n))
.forEach([Link]::println);

If we are adding 3 or more duplicates then it returning those values. instead you can try

Set<Integer> set = [Link]().filter(n->[Link](mylist,n)>1).collect([Link]());

[Link](set);

68 2 replies Reply

Ajesh Kalayil
Aug 26, 2023 (edited)

Question 8 (First Non repeated character) can be further reduced to

```

[Link]()

.filter(i -> [Link](i) == [Link](i))

.mapToObj(Character::toString)

.findFirst()... more

75 1 reply Reply

John Hua (johnhuatech)


Dec 21, 2023

Thanks for sharing. I'm in the market for a new engineering role. This is great refresher & learning topic. I am
hoping to network around as well.

55 1 reply Reply

See all responses

[Link] 16/24
9/15/25, 2:42 PM Java 8 Coding and Programming Interview Questions and Answers | by Anusha SP | Dev Genius

More from Anusha SP and Dev Genius

Anusha SP

Multithreading: Essential Coding Questions for Interviews


Multithreading is a programming concept that enables concurrent execution of multiple
threads within a single process, improving…

Mar 5 14 1

[Link] 17/24
9/15/25, 2:42 PM Java 8 Coding and Programming Interview Questions and Answers | by Anusha SP | Dev Genius

In Dev Genius by Ashish Singh

Automating HTTPS with Docker, Nginx & Certbot


A Practical Guide to Securing Your Web Apps with Free SSL/TLS Certificates

Jun 17 18

In Dev Genius by Mohammad Abir Abbas

Rust at Light Speed: Mastering Microsecond-Level Performance in High-


Frequency Systems
How to squeeze every nanosecond out of your Rust code when milliseconds feel like geological
epochs

Aug 19 59 3

[Link] 18/24
9/15/25, 2:42 PM Java 8 Coding and Programming Interview Questions and Answers | by Anusha SP | Dev Genius

Anusha SP

Java 8 to 17: A Complete Guide to Default, Static and Sealed Methods


As we all know, Java has evolved tremendously over the years. From Java 8 to Java 23 (the
latest version as of 2024), the concepts related…

Mar 16 56 3

See all from Anusha SP

See all from Dev Genius

Recommended from Medium

[Link] 19/24
9/15/25, 2:42 PM Java 8 Coding and Programming Interview Questions and Answers | by Anusha SP | Dev Genius

Anusha SP

Java Integer Based Programming Questions


In this article let us solve list of integer based programming questions and answers, which can
be asked in any java interviews from…

Jun 12 59 1

[Link] 20/24
9/15/25, 2:42 PM Java 8 Coding and Programming Interview Questions and Answers | by Anusha SP | Dev Genius

Code With Sunil | Code Smarter, not harder

Top 10 String Coding Interview Questions for Java Developers

Jul 30 65 4

[Link] 21/24
9/15/25, 2:42 PM Java 8 Coding and Programming Interview Questions and Answers | by Anusha SP | Dev Genius

Arvind Kumar

300+ Spring Boot interview questions


Comprehensive list of 300+ Spring Boot interview questions tailored for a Microservices
Backend Engineer role and , covering all important…

Apr 16 347 9

In Javarevisited by Kavya's Programming Path

Why Interviewers Keep Asking Immutability in Java: How I Impressed


Them

[Link] 22/24
9/15/25, 2:42 PM Java 8 Coding and Programming Interview Questions and Answers | by Anusha SP | Dev Genius

How a “simple” Java interview question on immutability turned into a deep dive on strings,
thread safety, and design patterns.

Sep 5 434 12

A cup of JAVA coffee with NeeSri

🔥 Top 15+ Tricky HashMap Interview Questions (With Smart Answers)


Whether you’re preparing for interviews at Amazon, Google, TCS, Infosys, Wipro, or Capgemini,
or simply leveling up your Java skills…

Apr 5 88 1

[Link] 23/24
9/15/25, 2:42 PM Java 8 Coding and Programming Interview Questions and Answers | by Anusha SP | Dev Genius

Gain Java Knowledge

Top Senior Java Developer Interview Questions and Answers


1. Explain use cases of Kafka.

Jul 17 56 1

See more recommendations

[Link] 24/24

Common questions

Powered by AI

Convert a list of objects into a map using Java 8 by collecting stream results with `Collectors.toMap()` allowing for duplicate keys. To handle duplicates, resolve conflicts with a lambda function and sort values. Implementation example: `Map<String, Long> notesRecords = noteLst.stream().sorted(Comparator.comparingLong(Notes::getTagId).reversed()).collect(Collectors.toMap(Notes::getTagName, Notes::getTagId, (oldValue, newValue) -> oldValue, LinkedHashMap::new));` This maintains sorting and resolves duplicate keys by retaining older entries. .

Java 8 Stream API enables finding the first non-repeated character using stream operations and a LinkedHashMap to preserve the order of characters. The method involves mapping characters to lowercase, grouping them by their identity, and filtering entries to find the first that has a value of 1. This can be achieved with the streams as follows: `input.chars().mapToObj(i -> Character.toLowerCase((char) i)).collect(Collectors.groupingBy(Function.identity(), LinkedHashMap::new, Collectors.counting())).entrySet().stream().filter(e -> e.getValue() == 1).map(e -> e.getKey()).findFirst().orElse(null);` .

In Java 8, `Optional.ofNullable()` can be employed to check if a list is null or empty, returning an empty list if null, permitting subsequent operations on the list. The usage is: `Optional.ofNullable(noteLst).orElseGet(Collections::emptyList).stream().filter(Objects::nonNull).forEach(System.out::println);`. This line checks for non-null values, allowing use of `forEach()` to iterate and print the list elements. .

Java 8 provides the `sorted()` method with `Collections.reverseOrder()` to sort a list of integers in descending order. This can be achieved by using the stream's sorted method as follows: `myList.stream().sorted(Collections.reverseOrder()).forEach(System.out::println);` This statement processes the list to reorder its elements in descending order before printing each element. .

Java 8's Stream API allows computation and filtering of cubes of integer list elements using a combination of the `map()` and `filter()` operations. For instance, `integerList.stream().map(i -> i * i * i).filter(i -> i > 50).forEach(System.out::println);` This line first maps each integer to its cubic value and then filters the results to only those above 50. .

To determine if a list of integers contains duplicates, you can use a HashSet to track seen numbers and utilize the `anyMatch` method from the stream. The method returns true if `anyMatch()` finds a number that cannot be added to the set, indicating it has already been added before. Here's how it can be done: `return Arrays.stream(nums).anyMatch(num -> !setData.add(num));` .

You can find the maximum value in a list of integers using Java 8 Stream API by utilizing the `max()` collector method with `Integer::compare` as a comparator. The method will return the maximum value found within the stream of numbers as follows: `int max = myList.stream().max(Integer::compare).get();` This approach efficiently processes the list to identify the largest element. .

In Java 8, you can apply stream operations to manipulate list elements by mapping each integer to its cube and filtering results over a threshold. For example, to cube numbers and get those greater than 50, you can use `map()` to apply the cube operation and `filter()` to apply the conditional check: `integerList.stream().map(i -> i * i * i).filter(i -> i > 50).forEach(System.out::println);` This line of code will output only those cubed results greater than 50. .

In Java 8, you can concatenate two different streams using the `Stream.concat()` method, which takes two streams as parameters and merges their contents into a single stream. Implementation example: `Stream<String> concatStream = Stream.concat(list1.stream(), list2.stream());` Then you can perform any stream operation, such as printing using `forEach()`, on the resulting concatenated stream. .

To count each element's occurrences in a String ArrayList using Java 8 stream functions, you can utilize `collect()` with `Collectors.groupingBy()` and `Collectors.counting()`. Here's the approach: `Map<String, Long> namesCount = names.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));` This effectively groups the list items by themselves and counts the occurrences of each distinct item. .

You might also like