0% found this document useful (0 votes)
10 views6 pages

Java Streams 30 Questions With Examples

The document contains 30 practice problems focused on Java Streams, each with example inputs and outputs. Problems include tasks such as removing duplicate characters, counting occurrences of characters, and finding the longest word in a sentence. The solutions utilize various Stream operations like filtering, mapping, and collecting to achieve the desired results.
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)
10 views6 pages

Java Streams 30 Questions With Examples

The document contains 30 practice problems focused on Java Streams, each with example inputs and outputs. Problems include tasks such as removing duplicate characters, counting occurrences of characters, and finding the longest word in a sentence. The solutions utilize various Stream operations like filtering, mapping, and collecting to achieve the desired results.
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

Java Streams – 30 Practice Problems (With Examples)

Q1. Remove Duplicate Characters


Input: programming
Output: progamin
String res =
[Link]()
.distinct()
.mapToObj(c -> [Link]((char) c))
.collect([Link]());

Q2. Count Occurrences of Each Character


Input: banana
Output: {b=1, a=3, n=2}
Map<Character, Long> res =
[Link]()
.mapToObj(c -> (char) c)
.collect([Link](
[Link](),
[Link]()
));

Q3. First Non-Repeating Character


Input: swiss
Output: w
Character res =
[Link]()
.mapToObj(c -> (char) c)
.collect([Link](
[Link](),
LinkedHashMap::new,
[Link]()
))
.entrySet()
.stream()
.filter(e -> [Link]() == 1)
.map([Link]::getKey)
.findFirst()
.orElse(null);

Q4. Find Duplicate Characters


Input: programming
Output: [g, r, m]
List<Character> res =
[Link]()
.mapToObj(c -> (char) c)
.collect([Link](
[Link](),
LinkedHashMap::new,
[Link]()
))
.entrySet()
.stream()
.filter(e -> [Link]() > 1)
.map([Link]::getKey)
.toList();

Q5. Count Words in a Sentence


Input: Java streams are powerful
Output: 4
long res =
[Link]([Link]("\s+")).count();

Q6. Sort Characters in a String


Input: dcba
Output: abcd
String res =
[Link]()
.sorted()
.mapToObj(c -> [Link]((char) c))
.collect([Link]());

Q7. Longest Word in a Sentence


Input: Java streams simplify coding
Output: simplify
String res =
[Link]([Link]("\s+"))
.max([Link](String::length))
.orElse(null);

Q8. Case-Insensitive Word Frequency


Input: Java java STREAM stream
Output: {java=2, stream=2}
Map<String, Long> res =
[Link]([Link]("\s+"))
.map(String::toLowerCase)
.collect([Link](
[Link](),
[Link]()
));

Q9. Partition Strings by Length


Input: [Java, Streams, API, Power]
Output: {true=[Streams, Power], false=[Java, API]}
Map<Boolean, List<String>> res =
[Link]()
.collect([Link](s -> [Link]() > 4));
Q10. First Repeating Character
Input: abca
Output: a
Character res =
[Link]()
.mapToObj(c -> (char) c)
.collect([Link](
[Link](),
LinkedHashMap::new,
[Link]()
))
.entrySet()
.stream()
.filter(e -> [Link]() > 1)
.map([Link]::getKey)
.findFirst()
.orElse(null);

Q11. Sum of All Even Numbers


Input: [1,2,3,4,5,6]
Output: 12
int sum =
[Link]()
.filter(n -> n % 2 == 0)
.mapToInt(Integer::intValue)
.sum();

Q12. Find Maximum Number


Input: [10,45,32,99,67]
Output: 99
Optional<Integer> res =
[Link]().max(Integer::compareTo);

Q13. Find Minimum Number


Input: [10,45,32,99,67]
Output: 10
Optional<Integer> res =
[Link]().min(Integer::compareTo);

Q14. Count Elements Greater Than 50


Input: [10,45,32,99,67,80]
Output: 3
long res =
[Link]().filter(n -> n > 50).count();

Q15. Convert Integers to Squares


Input: [1,2,3,4]
Output: [1,4,9,16]
List<Integer> res =
[Link]().map(n -> n * n).toList();

Q16. Remove Duplicate Elements


Input: [1,2,2,3,4,4,5]
Output: [1,2,3,4,5]
List<Integer> res =
[Link]().distinct().toList();

Q17. Sort in Descending Order


Input: [5,1,9,3]
Output: [9,5,3,1]
List<Integer> res =
[Link]().sorted([Link]()).toList();

Q18. Find Second Highest Number


Input: [10,45,32,99,67]
Output: 67
int res =
[Link]()
.distinct()
.sorted([Link]())
.skip(1)
.findFirst()
.orElseThrow();

Q19. Group Even and Odd Numbers


Input: [1,2,3,4,5,6]
Output: {false=[1,3,5], true=[2,4,6]}
Map<Boolean, List<Integer>> res =
[Link]()
.collect([Link](n -> n % 2 == 0));

Q20. Convert List to Set


Input: [1,2,2,3,4]
Output: [1,2,3,4]
Set<Integer> res =
[Link]().collect([Link]());

Q21. Convert List to Map (Value → Square)


Input: [1,2,3,4]
Output: {1=1,2=4,3=9,4=16}
Map<Integer, Integer> res =
[Link]()
.collect([Link](
[Link](),
x -> x * x
));

Q22. Count Occurrences of Each Number


Input: [1,2,2,3,3,3]
Output: {1=1,2=2,3=3}
Map<Integer, Long> res =
[Link]()
.collect([Link](
[Link](),
[Link]()
));

Q23. Partition Numbers Greater Than 10


Input: [5,12,3,20,8,15]
Output: {false=[5,3,8], true=[12,20,15]}
Map<Boolean, List<Integer>> res =
[Link]()
.collect([Link](n -> n > 10));

Q24. Find Average of Numbers


Input: [10,20,30,40]
Output: 25.0
double avg =
[Link]()
.collect([Link](Integer::intValue));

Q25. Numbers Starting With Digit 1


Input: [10,15,21,13,45,19]
Output: [10,15,13,19]
List<Integer> res =
[Link]()
.map(String::valueOf)
.filter(s -> [Link]("1"))
.map(Integer::valueOf)
.toList();

Q26. Flatten a List of Lists


Input: [[1,2],[3,4],[5]]
Output: [1,2,3,4,5]
List<Integer> res =
[Link]()
.flatMap(List::stream)
.toList();
Q27. Find Any Number Greater Than 50
Input: [10,45,32,99,67,80]
Output: Optional[99]
Optional<Integer> res =
[Link]()
.filter(n -> n > 50)
.findAny();

Q28. Check If All Numbers Are Positive


Input: [5,10,3,8]
Output: true
boolean res =
[Link]().allMatch(n -> n > 0);

Q29. Check If Any Number Is Negative


Input: [5,-2,3,8]
Output: true
boolean res =
[Link]().anyMatch(n -> n < 0);

Q30. Check If No Number Is Zero


Input: [5,3,8,2]
Output: true
boolean res =
[Link]().noneMatch(n -> n == 0);

You might also like