0% found this document useful (0 votes)
192 views5 pages

Java 8 Coding Interview Questions

The document presents 10 tricky Java 8 coding questions commonly asked in interviews, along with their solutions and outputs. Each question covers a specific Java 8 feature, such as streams, sorting, and grouping. The examples provided are designed to help readers understand the logic and implementation of Java 8 functionalities.

Uploaded by

ragnar1180930
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)
192 views5 pages

Java 8 Coding Interview Questions

The document presents 10 tricky Java 8 coding questions commonly asked in interviews, along with their solutions and outputs. Each question covers a specific Java 8 feature, such as streams, sorting, and grouping. The examples provided are designed to help readers understand the logic and implementation of Java 8 functionalities.

Uploaded by

ragnar1180930
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

🚀 10 Tricky Java 8 Coding Questions (Medium

→ Advanced, Real Interview Problems)

🙌
Here are some of the most frequently asked Java 8 coding interview questions with approache,
and outputs so that it’s easy to follow.

🔹[Link] do you retrieve the 5th element from a List in Java 8?


List<String> list = [Link]("a","b","c","d","e","f");

🧑‍💻 Logic:
String res= [Link]().skip(4).findFirst().orElse(null);
[Link](res);

✅ Output: e

🔹[Link] a String, how do you find the character with the


second-highest frequency using Java 8 Streams?

(For example: "abbcccd" → b=2)

🧑‍💻 Logic:
String s = "abbcccd";

Map<Character, Long> map= [Link]().mapToObj(c -> (char) c)


.collect([Link](c -> c,
[Link]()));

Character second = [Link]().stream()


.sorted([Link].<Character, Long>comparingByValue().reversed())
.skip(1).findFirst()
.get().getKey();
[Link](second + "=" + [Link](second));

✅ Output: b=2
🔹 [Link] s = "abcd", how do you generate the pattern
abbcccdddd using Java 8 Streams?

🧑‍💻 Logic:
String s = "abcd";

String result = [Link](0, [Link]())


.mapToObj(i -> [Link]([Link](i)).repeat(i + 1))
.collect([Link]());
[Link](result);

✅ Output:
abbcccdddd

🔹 Q4. How do you sort a list of Employee objects by both name and
salary using Java 8?

🧑‍💻 Logic:
[Link]()
.sorted([Link](Employee::getName)
.thenComparing(Employee::getSalary))
.forEach([Link]::println);
🔹 [Link] a list of integers, how do you separate the elements into two
lists — one containing duplicates and the other containing unique elements
using Java 8?

🧑‍💻 Logic:
List<Integer> list=[Link](1,2,3,1,2,4,5);

List<Integer> list = [Link](1,2,3,1,2,4,5);

Map<Integer, Long> freq = [Link]()


.collect([Link](i -> i, [Link]()));

List<Integer> duplicates=[Link]().stream().filter(e->[Link]() > 1)


.map([Link]::getKey).collect([Link]());

List<Integer> unique =
[Link]().stream()
.filter(e -> [Link]() == 1)
.map([Link]::getKey)
.collect([Link]());

✅ Output:
Duplicates → [1,2]
Unique → [3,4,5]

🔹 Q6. How do you find common elements between two sorted integer lists
using Java 8 Streams?

🧑‍💻 Logic:
List<Integer> a = [Link](1,2,3,4);
List<Integer> b = [Link](2,4,6);

List<Integer> common = [Link]()


.filter(b::contains)
.collect([Link]());

✅ Output:
[2,4]
🔹 [Link] a list of Strings, how do you remove the words that contain
any numeric digits using Java 8?

(Example: "ab1c" should be removed (here number contains in word))

🧑‍💻 Logic:
List<String> list=[Link]("abc", "ab1c", "hello", "h3i");

List<String> result = [Link]()


.filter(s -> ![Link](".*\\d.*"))
.collect([Link]());
✅ Output:
["abc", "hello"]

🔹 Q8. Given a String, how do you find the character with the highest
frequency?

🧑‍💻 Logic:
String s="bbaaac";

Map<Character, Long> map = [Link]()


.mapToObj(c -> (char) c)
.collect([Link](c -> c, [Link]()));

long max = [Link]().stream().mapToLong(v -> v).max().orElse(0);

[Link]().stream()
.filter(e -> [Link]() == max)
.forEach([Link]::println);

✅ Output:
a=3
🔹 Q9. Given a sentence, how do you find duplicate words along with their
occurrence count, sorted by frequency in descending order?

🧑‍💻 Logic:
String sentence="Java is Java and Java is great";

[Link]([Link](" "))
.collect([Link](w -> w, [Link]()))
.entrySet().stream()
.sorted([Link].<String,Long>comparingByValue().reversed())
.forEach([Link]::println);

✅ Output:
Java=3
is=2

🔹 [Link] do you print the top 3 longest Strings from a list using Java 8
Streams?

🧑‍💻 Logic:
List<String> list=[Link]("apple","banana","cherry","watermelon","kiwi","strawberry");

[Link]()
.sorted([Link](String::length).reversed())
.limit(3)
.forEach([Link]::println);

✅ Output:
watermelon
strawberry
banana

Common questions

Powered by AI

You can retrieve an element from a Java List using Java 8 Streams by using the `skip` and `findFirst` methods. For example, to retrieve the 5th element from a list `list = Arrays.asList("a","b","c","d","e","f")`, you would use `list.stream().skip(4).findFirst().orElse(null)`, which returns "e" .

To find common elements between two lists, use `filter` to retain elements in one list that exist in the other. For instance, lists `a = Arrays.asList(1,2,3,4); b = Arrays.asList(2,4,6);` would be processed as `List<Integer> common = a.stream().filter(b::contains).collect(Collectors.toList())`, resulting in [2,4].

Sort the list of strings by length in descending order using `sorted(Comparator.comparingInt(String::length).reversed())`, and then limit the result to the top N using `limit(N)`. For instance, to get the top 3 longest strings from `List<String> list=Arrays.asList("apple","banana","cherry","watermelon","kiwi","strawberry")`, the resulting strings would be `watermelon`, `strawberry`, and `banana` .

Convert the string into a character stream and use `Collectors.groupingBy()` with `Collectors.counting()` to count each character's occurrences. Then, find the maximum count using `values().stream().mapToLong(v -> v).max()`, and filter entries matching this maximum value to identify the character. In the string "bbaaac", the character 'a' appears most frequently, 3 times .

Use `filter` combined with a regex check `!s.matches(".*\\d.*")` to exclude words containing digits. For example, given `List<String> list=Arrays.asList("abc", "ab1c", "hello", "h3i");`, the resulting list after filtering would be `["abc", "hello"]` by excluding any string like "ab1c" that contains numbers .

To separate duplicates from unique elements, use `Collectors.groupingBy()` to count occurrences of each element. Use `filter(e -> e.getValue() > 1)` to identify duplicates and `filter(e -> e.getValue() == 1)` for unique elements. Convert entries to lists with `map(Map.Entry::getKey)`. Example list [1,2,3,1,2,4,5] results in duplicates [1,2] and unique [3,4,5].

Split the sentence by spaces and group occurrences using `Collectors.groupingBy(String::valueOf, Collectors.counting())`. Sort entries by count in descending order with `sorted(Map.Entry.<String,Long>comparingByValue().reversed())`, then print duplicates with their counts. For "Java is Java and Java is great", duplicates are `(Java=3, is=2)` .

In Java 8, you can sort a list of custom objects by multiple criteria using the `Comparator.comparing` method combined with `thenComparing`. For instance, to sort Employee objects by name and then salary: `list.stream().sorted(Comparator.comparing(Employee::getName).thenComparing(Employee::getSalary)).forEach(System.out::println);` .

To generate a pattern like "abbcccdddd" from string "abcd", use `IntStream.range(0, s.length())` to iterate over positions, then for each character at position `i`, repeat it `i+1` times using `String.valueOf(s.charAt(i)).repeat(i + 1)`, and finally collect these strings into a single result with `Collectors.joining()` .

To find the character with the second-highest frequency, convert the string into a character stream and group them with `Collectors.groupingBy()`. Then, sort the entries by value in descending order with `sorted(Map.Entry.<Character, Long>comparingByValue().reversed())`, and then use `skip(1)` followed by `findFirst` to get the second entry. Example logic: `String s = "abbcccd"`; the second most frequent character is 'b' with a count of 2 .

You might also like