Java 8 Coding Interview Questions
Java 8 Coding Interview Questions
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 .