Java 8 Coding Interview Questions
Java 8 Coding Interview Questions
PM Questions
Here are the some Java 8 interview sample coding questions with answers. I
hope it will be helpful for you guys while preparing for an interview.
[Link] 1/2
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions
([Link]
content/uploads/2023/06/Java_8_Interview_Sample_Coding_Questions.pn
g?ssl=1)
[Link] 2/2
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions
1 import [Link];
2 import [Link];
3 import [Link];
4 import [Link];
5 import [Link];
6 import
[Link]; 7
8 public class
Java8Code 9 {
10 public static void main(String[]
args) 11 {
12 List<Integer> listOfIntegers = [Link](71, 18, 42, 21,
6
13
14 Map<Boolean, List<Integer>> oddEvenNumbersMap =
15 [Link]().collect([Link]
ionin 16
17 Set<Entry<Boolean, List<Integer>>> entrySet =
oddEvenNumbersMa
18
19 for (Entry<Boolean, List<Integer>> entry :
entrySet) 20 {
21 [Link]("-------------------");
22
23 if ([Link]())
24 {
25 [Link]("Even
Numbers"); 26 }
27 else
28 {
29 [Link]("Odd
Numbers"); 30 }
31
32 [Link]("-------------------");
33
34 List<Integer> list =
[Link](); 35
36 for (int i : list)
37 {
38
[Link](i);
39 }
40 }
41 }
42 }
Output :
————–
Odd Numbers
————–
71
21
67
95
87
————–
Even Numbers
————–
18
[Link] 3/2
6/27/23, 11:34 Java 8 Interview Sample Coding
PM 42 Questions
32
[Link] 4/2
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions
14
56
2) How do you remove duplicate elements from a list using Java 8 streams?
1 import [Link];
2 import [Link];
3 import
[Link]; 4
5 public class
Java8Code 6 {
7 public static void main(String[] args)
8 {
9 List<String> listOfStrings = [Link]("Java",
"Python", " 10
11 List<String> uniqueStrngs =
[Link]().distinct(). 12
13
[Link](uniqueStrngs);
14 }
15 }
Output :
3) How do you find frequency of each character in a string using Java 8 streams?
[Link] 5/2
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions
1 import [Link];
2 import [Link];
3 import
[Link]; 4
5 public class
Java8Code 6 {
7 public static void main(String[] args)
8 {
9 String inputString = "Java Concept Of The
Day"; 10
11 Map<Character, Long> charCountMap =
12 [Link]()
13 .mapToObj(c -> (char) c)
14 .collect([Link](Func
tio 15
16
[Link](charCountMap);
17 }
18 }
Output :
{ =4, a=3, c=1, C=1, D=1, e=2, f=1, h=1, J=1, n=1, O=1, o=1, p=1, T=1, t=1, v=1,
y=1}
1 impor [Link];
t
2 impor [Link];
t
3 impor [Link];
t
4 impor [Link];
t
5 impor [Link];
t
6
7 publi class Java8Code
c
8 {
9 public static void main(String[] args)
10 {
11 List<String> stationeryList = [Link]("Pen", "
"Eraser",
12
13 Map<String, Long> stationeryCountMap =
14 [Link]().collect([Link]
ngBy(
15
16 [Link](stationeryCountMap);
17 }
18 }
Output :
[Link] 6/2
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions
1 import [Link];
2 import [Link];
3 import
[Link]; 4
5 public class
Java8Code 6 {
7 public static void main(String[] args)
8 {
9 List<Double> decimalList = [Link](12.45, 23.58,
17.13, 10
11
[Link]().sorted([Link]()).forEach 12
}
13 }
Output :
71.85
56.98
42.89
33.78
23.58
21.12
17.13
12.45
6) Given a list of strings, join the strings with ‘[‘ as prefix, ‘]’ as suffix and ‘,’ as delimiter?
[Link] 7/2
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions
1 import [Link];
2 import [Link];
3 import
[Link]; 4
5 public class
Java8Code 6 {
7 public static void main(String[] args)
8 {
9 List<String> listOfStrings = [Link]("Facebook",
"Twitte 10
11 String joinedString =
[Link]().collect(Collector 12
13
[Link](joinedString);
14 }
15 }
Output :
7) From the given list of integers, print the numbers which are multiples of 5?
1 import [Link];
2 import
[Link]; 3
4 public class
Java8Code 5 {
6 public static void main(String[]
args) 7 {
8 List<Integer> listOfIntegers = [Link](45, 12, 56,
15, 2 9
10 [Link]().filter(i -> i % 5 ==
0).forEach(System
11 }
12 }
Output :
45
15
75
[Link] 8/2
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions
1 import [Link];
2 import [Link];
3 import
[Link]; 4
5 public class
Java8Code 6 {
7 public static void main(String[] args)
8 {
9 List<Integer> listOfIntegers = [Link](45, 12, 56,
15, 2 10
11 int max =
[Link]().max([Link]( 12
13 [Link]("Maximum Element :
"+max); 14
15 int min =
[Link]().min([Link](
16
17 [Link]("Minimum Element :
"+min); 18 }
19 }
Output :
Maximum Element :
89 Minimum
Element : 12
9) How do you merge two unsorted arrays into single sorted array using Java 8
streams?
1 import [Link];
2 import
[Link]; 3
4 public class
Java8Code 5 {
6 public static void main(String[] args)
7 {
8 int[] a = new int[] {4, 2, 7, 1};
9
10 int[] b = new int[] {8, 3, 9, 5};
11
12 int[] c = [Link]([Link](a),
[Link](b)) 13
14
[Link]([Link](c));
15 }
16 }
Output :
[1, 2, 3, 4, 5, 7, 8, 9]
10) How do you merge two unsorted arrays into single sorted array without duplicates?
[Link] 9/2
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions
1 import [Link];
2 import
[Link]; 3
4 public class Java8Code
5 {
6 public static void main(String[]
args) 7 {
8 int[] a = new int[] {4, 2, 5, 1};
9
10 int[] b = new int[] {8, 1, 9, 5};
11
12 int[] c = [Link]([Link](a),
[Link](b)) 13
14
[Link]([Link](c));
15 }
16 }
Output :
[1, 2, 4, 5, 8, 9]
11) How do you get three maximum numbers and three minimum numbers from the
given list of integers?
1 impor [Link];
t
2 impor [Link];
t
3 impor [Link];
t
4
5 publi class Java8Code
c
6 {
7 public static void main(String[] args)
8 {
9 List<Integer> listOfIntegers = [Link](45, 12, 2
56, 15,
10
11 //3 minimum Numbers
12
13 [Link]("------------------------");
14
15 [Link]("Minimum 3
Numbers"); 16
17 [Link]("------------------------");
18
19
[Link]().sorted().limit(3).forEach([Link]:: 20
21 //3 Maximum
Numbers 22
23 [Link]("------------------------");
24
25 [Link]("Maximum 3
Numbers"); 26
27 [Link]("------------------------");
28
29
[Link]().sorted([Link]()).limit(3).fo
[Link] 10
6/27/23, 11:34 Java 8 Interview Sample Coding
PM r 30 } Questions
31 }
Output :
[Link] 11
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions
—————–
Minimum 3 Numbers
—————–
12
15
24
—————–
Maximum 3 Numbers
—————–
89
75
56
12) Java 8 program to check if two strings are anagrams or not?
1 import [Link];
2 import
[Link]; 3
4 public class Java8Code
5 {
6 public static void main(String[]
args) 7 {
8 String s1 = "RaceCar";
9 String s2 =
"CarRace"; 10
11 s1 =
[Link]([Link]("")).map(String::toUpperCase).sorted() 12
13 s2 =
[Link]([Link]("")).map(String::toUpperCase).sorted() 14
15 if ([Link](s2))
16 {
17 [Link]("Two strings are
anagrams"); 18 }
19 else
20 {
21 [Link]("Two strings are not
anagrams"); 22 }
23 }
24 }
Output :
[Link] 12
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions
1 import [Link];
2 import
[Link]; 3
4 public class Java8Code
5 {
6 public static void main(String[]
args) 7 {
8 int i = 15623;
9
10 Integer sumOfDigits =
[Link]([Link](i).split("")).c 11
12 [Link](sumOfDigits);
13 }
14 }
Output :
17
1 import [Link];
2 import [Link];
3 import
[Link]; 4
5 public class
Java8Code 6 {
7 public static void main(String[]
args) 8 {
9 List<Integer> listOfIntegers = [Link](45, 12, 56,
15, 2 10
11 Integer secondLargestNumber =
[Link]().sorted(C
12
13
[Link](secondLargestNumber);
14 }
15 }
Output :
75
15) Given a list of strings, sort them according to increasing order of their length?
[Link] 13
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions
1 import [Link];
2 import [Link];
3 import
[Link]; 4
5 public class
Java8Code 6 {
7 public static void main(String[] args)
8 {
9 List<String> listOfStrings = [Link]("Java",
"Python", " 10
11
[Link]().sorted([Link](String::len 12
}
13 }
Output :
C
C#
C+
+
Java
HTML
COBOL
Pytho
n
Kotlin
16) Given an integer array, find sum and average of all elements?
1 import
[Link]; 2
3 public class
Java8Code 4 {
5 public static void main(String[]
args) 6 {
7 int[] a = new int[] {45, 12, 56, 15, 24, 75, 31, 89};
8
9 int sum =
[Link](a).sum(); 10
11 [Link]("Sum =
"+sum); 12
13 double average =
[Link](a).average().getAsDouble(); 14
15 [Link]("Average =
"+average); 16 }
17 }
Output :
Sum = 347
Average = 43.375
[Link] 14
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions
1 import [Link];
2 import
[Link]; 3
4 public class Java8Code
5 {
6 public static void main(String[]
args) 7 {
8 List<Integer> list1 = [Link](71, 21, 34, 89,
56, 28); 9
10 List<Integer> list2 = [Link](12, 56, 17, 21,
94, 34); 11
12
[Link]().filter(list2::contains).forEach([Link]::pri
13 }
14 }
Output :
21
34
56
1 import [Link];
2 import
[Link]; 3
4 public class
Java8Code 5 {
6 public static void main(String[] args)
7 {
8 String str = "Java Concept Of The
Day"; 9
10 String reversedStr = [Link]([Link](" "))
11 .map(word -> new StringBuffer(word
12 .collect([Link](" "));
13
14
[Link](reversedStr);
15 }
16 }
Output :
[Link] 15
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions
1 import
[Link];
2
3 public class Java8Code
4 {
5 public static void main(String[] args)
6 {
7 int sum = [Link](1, 11).sum()
;
8
9 [Link](sum);
10 }
11 }
Output :
55
1 impor [Link];
t
2 impor [Link]
t m;
3
4 publi class Java8Code
c
5 {
6 public static void main(String[] args)
7 {
8 int[] array = new int[] {5, 1, 7, 3, 9, 6};
9
10 int[] reversedArray = [Link](1,
11
12 [Link]).m
13
14 [Link]([Link](reversedArray));
} }
Output :
[6, 9, 3, 7, 1, 5]
1 import
[Link]; 2
3 public class
Java8Code 4 {
5 public static void main(String[] args)
6 {
7 [Link](1, 10).map(i -> i *
2).forEach([Link] 8 }
9 }
Output :
[Link] 16
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions
2
4
6
[Link] 17
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions
8
10
12
14
16
18
20
1 import [Link];
2 import [Link];
3 import [Link];
4 import [Link];
5 import [Link];
6 import
[Link]; 7
8 public class
Java8Code 9 {
10 public static void main(String[]
args) 11 {
12 List<String> listOfStrings = [Link]("Pen", "Eraser",
"N
13
14 Map<String, Long> elementCountMap = [Link]()
15 .collect(Coll
16
17 Entry<String, Long> mostFrequentElement =
[Link] 18
19 [Link]("Most Frequent Element :
"+mostFrequentElem 20
21 [Link]("Count :
"+[Link]()); 22 }
23 }
Output :
[Link] 18
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions
1 import
[Link]; 2
3 public class
Java8Code 4 {
5 public static void main(String[]
args) 6 {
7 String str = "ROTATOR";
8
9 boolean isItPalindrome = [Link](0, [Link]()/2).
10 noneMatch(i -> [Link](i) !=
[Link]([Link]( 11
12 if (isItPalindrome)
13 {
14 [Link](str+" is a
palindrome"); 15 }
16 else
17 {
18 [Link](str+" is not a
palindrome"); 19 }
20 }
21 }
Output :
ROTATOR is a palindrome
24) Given a list of strings, find out those strings which start with a number?
1 import [Link];
2 import
[Link]; 3
4 public class Java8Code
5 {
6 public static void main(String[]
args) 7 {
8 List<String> listOfStrings = [Link]("One", "2wo",
"3hre 9
10 [Link]().filter(str ->
[Link]([Link] 11 }
12 }
Output :
2wo
3hre
e
5ive
[Link] 19
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions
1 import [Link];
2 import [Link];
3 import [Link];
4 import [Link];
5 import
[Link]; 6
7 public class Java8Code
8 {
9 public static void main(String[]
args) 10 {
11 List<Integer> listOfIntegers = [Link](111, 222,
333, 11 12
13 Set<Integer> uniqueElements = new
HashSet<>(); 14
15 Set<Integer> duplicateElements =
[Link]().filte
16
17
[Link](duplicateElements);
18 }
19 }
Output :
1 import [Link];
2 import [Link];
3 import [Link];
4 import
[Link]; 5
6 public class Java8Code
7 {
8 public static void main(String[]
args) 9 {
10 String inputString = "Java Concept Of The
Day".replaceAll("\\s 11
12 Set<String> uniqueChars = new
HashSet<>(); 13
14 Set<String> duplicateChars =
15 [Link]([Link](""))
16 .filter(ch -> ! [Link](ch))
17 .collect([Link](
)); 18
19 [Link](duplicateChars);
20 }
21 }
Output :
[a, c, t, e, o]
[Link] 20
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions
1 impor [Link];
t
2 impor [Link];
t
3 impor [Link];
t
4 impor [Link]
t on;
5 impor [Link]
t rs;
6
7 publi class Java8Code
c
8 {
9 public static void main(String[] args)
10 {
11 String inputString = "Java Concept Of The
12 Day".replaceAll("\\s
13
14 Map<String, Long> charCountMap =
15 [Link]([Link](""))
16 .collect([Link](Fun
17
18 String firstRepeatedChar = [Link]()
19 .stream()
20 .filter(entry -> entry
21 .map(entry -> [Link]
22 .findFirst()
23 .get();
24
25 [Link](firstRepeatedChar);
26 }
}
Output :
1 import [Link];
2 import [Link];
3 import [Link];
4 import [Link];
5 import
[Link]; 6
7 public class
Java8Code 8 {
9 public static void main(String[] args)
10 {
11 String inputString = "Java Concept Of The
Day".replaceAll("\\s 12
13 Map<String, Long> charCountMap =
14 [Link]([Link](""))
15 .collect([Link](Fun
16
17 String firstNonRepeatedChar = [Link]()
18 .stream()
19 .filter(entry -> entry
20 .map(entry -> [Link]
21 .findFirst()
22 .get();
23
[Link] 21
6/27/23, 11:34 Java 8 Interview Sample Coding
PM 24 Questions
[Link](firstNonRepeatedChar);
25 }
26 }
[Link] 22
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions
Output :
1 import
[Link]; 2
3 public class
Java8Code 4 {
5 public static void main(String[]
args) 6 {
7 [Link](new int[] {0, 1}, f -> new int[] {f[1],
f[0]+f[
8 .limit(10)
9 .map(f -> f[0])
10 .forEach(i -> [Link](i+"
")); 11 }
12 }
Output :
0 1 1 2 3 5 8 13 21 34
1 import
[Link]; 2
3 public class Java8Code
4 {
5 public static void main(String[]
args) 6 {
7 [Link](new int[] {1, 3}, f -> new int[] {f[1],
f[1]+2} 8 .limit(10)
9 .map(f -> f[0])
10 .forEach(i -> [Link](i+"
")); 11 }
12 }
Output :
1 3 5 7 9 11 13 15 17 19
[Link] 23
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions
1 import [Link];
2 import
[Link]; 3
4 public class Java8Code
5 {
6 public static void main(String[]
args) 7 {
8 List<String> listOfStrings = [Link]("One", "Two",
"Thre 9
10 String lastElement =
[Link]().skip(listOfStrings 11
12 [Link](lastElement);
13 }
14 }
Output :
Six
32) Find the age of a person in years if the birthday has given?
1 import [Link];
2 import
[Link]; 3
4 public class
Java8Code 5 {
6 public static void main(String[] args)
7 {
8 LocalDate birthDay = [Link](1985, 01, 23);
9 LocalDate today =
[Link](); 10
11 [Link]([Link](birthDay,
today));
12 }
13 }
Also Read :
Watch Yo uTube
Get inspired by fun content and
Shorts
new creators,
songs, more
YouTube Shorts
[Link] 24
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions
([Link]
PREVIOUS
strings-cheat-sheet/)
Related Posts
Leave a Reply
Name *
Email *
Website
[Link] 25
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions
Add Comment
Post Comment
Tutorials :
Java 11 ([Link]
Java 10 ([Link]
Java 9 ([Link]
Java 8 ([Link]
Java Collections ([Link]
hierarchy/) Java Threads ([Link]
java-tutorial/)
Exception Handling ([Link]
handling/) Java Generics
([Link]
Java Strings
([Link]
Java Arrays
([Link]
JDBC ([Link]
Quiz :
Interview Q&A :
[Link] 27
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions
[Link] 28
In Java 8, streams can be used to partition a list of integers into odd and even numbers using the Collectors.partitioningBy method. By streaming the list and applying the partitioning logic with a modulus check inside a Collectors.partitioningBy, you can separate the numbers. For example, if you have a List<Integer> listOfIntegers, you can separate them by: Map<Boolean, List<Integer>> oddEvenMap = listOfIntegers.stream().collect(Collectors.partitioningBy(n -> n % 2 == 0)); This separates even numbers into one list and odd numbers into another .