0% found this document useful (0 votes)
2 views4 pages

Updated Java 8 Coding Questions

The document contains 26 Java 8 coding questions focused on using Streams and Lambda expressions. Each question includes a code snippet that demonstrates how to perform specific tasks such as separating odd and even numbers, removing duplicates, calculating frequencies, and more. The questions cover a range of topics from basic operations to more complex algorithms like finding anagrams and generating Fibonacci series.
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)
2 views4 pages

Updated Java 8 Coding Questions

The document contains 26 Java 8 coding questions focused on using Streams and Lambda expressions. Each question includes a code snippet that demonstrates how to perform specific tasks such as separating odd and even numbers, removing duplicates, calculating frequencies, and more. The questions cover a range of topics from basic operations to more complex algorithms like finding anagrams and generating Fibonacci series.
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
You are on page 1/ 4

Important Java 8 Stream and Lambda Coding Questions (26 Total)

1. Separate Odd And Even Numbers


listOfIntegers.stream()
.collect(Collectors.partitioningBy(i -> i % 2 == 0));

2. Remove Duplicate Elements From List


listOfStrings.stream().distinct().collect(Collectors.toList());

3. Frequency of Each Character In String


inputString.chars()
.mapToObj(c -> (char) c)
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

4. Frequency of Each Element In An Array


anyList.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

5. Sort The List In Reverse Order


anyList.stream().sorted(Comparator.reverseOrder()).forEach(System.out::println);

6. Join List Of Strings With Prefix, Suffix And Delimiter


listOfStrings.stream().collect(Collectors.joining("Delimiter", "Prefix", "Suffix"));

7. Print Multiples of 5 From The List


listOfIntegers.stream()
.filter(i -> i % 5 == 0).forEach(System.out::println);

8. Maximum & Minimum In A List


listOfIntegers.stream().max(Comparator.naturalOrder()).get();
listOfIntegers.stream().min(Comparator.naturalOrder()).get();

9. Merge Two Unsorted Arrays Into Single Sorted Array


IntStream.concat(Arrays.stream(a), Arrays.stream(b)).sorted().toArray();

10. Anagram Program In Java 8


s1 = Stream.of(s1.split("")).map(String::toUpperCase).sorted().collect(Collectors.joining());
s2 = Stream.of(s2.split("")).map(String::toUpperCase).sorted().collect(Collectors.joining());
// If s1 and s2 are equal, they are anagrams.

11. Merge Two Unsorted Arrays Into Single Sorted Array Without Duplicates
IntStream.concat(Arrays.stream(a), Arrays.stream(b))
.sorted().distinct().toArray();

12. Three Max & Min Numbers From The List


// Min 3 Numbers
listOfIntegers.stream().sorted().limit(3).forEach(System.out::println);
// Max 3 Numbers
listOfIntegers.stream().sorted(Comparator.reverseOrder()).limit(3).forEach(System.out::println);

13. Sum Of All Digits Of A Number


Stream.of(String.valueOf(inputNumber).split(""))
.collect(Collectors.summingInt(Integer::parseInt));

14. Second Largest Number In An Integer Array


listOfIntegers.stream().sorted(Comparator.reverseOrder()).skip(1).findFirst().get();

15. Sort List Of Strings In Increasing Order Of Their Length


listOfStrings.stream().sorted(Comparator.comparing(String::length)).forEach(System.out::println);

16. Common Elements Between Two Arrays


list1.stream().filter(list2::contains).forEach(System.out::println);

17. Sum & Average of All Elements Of An Array


// Sum
Arrays.stream(inputArray).sum();
// Average
Arrays.stream(inputArray).average().getAsDouble();

18. Reverse Each Word Of A String


Arrays.stream(str.split(" "))
.map(word -> new StringBuffer(word).reverse())
.collect(Collectors.joining(" "));

19. Sum Of First 10 Natural Numbers


IntStream.range(1, 11).sum();

20. Reverse An Integer Array


IntStream.rangeClosed(1, array.length)
.map(i -> array[array.length - i])
.toArray();

21. Find Strings Which Start With Number


listOfStrings.stream()
.filter(str -> Character.isDigit(str.charAt(0)))
.forEach(System.out::println);

22. Palindrome Program In Java 8


IntStream.range(0, str.length()/2)
.noneMatch(i -> str.charAt(i) != str.charAt(str.length() - i - 1));

23. Find Duplicate Elements From An Array


listOfIntegers.stream()
.filter(i -> !set.add(i))
.collect(Collectors.toSet());

24. Last Element Of An Array


listOfStrings.stream().skip(listOfStrings.size() - 1).findFirst().get();
25. Age Of Person In Years
LocalDate birthDay = LocalDate.of(1985, 01, 23);
LocalDate today = LocalDate.now();
System.out.println(ChronoUnit.YEARS.between(birthDay, today));

26. Fibonacci Series


Stream.iterate(new int[]{0, 1}, f -> new int[]{f[1], f[0] + f[1]})
.limit(10)
.map(f -> f[0])
.forEach(i -> System.out.print(i + " "));

You might also like