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

Java Streams 50 Problems

The document provides 50 common problems and solutions using Java Streams, showcasing various operations such as counting elements, filtering, mapping, and reducing. Each problem is accompanied by a code snippet demonstrating the solution. The document serves as a practical reference for developers looking to utilize Java Streams effectively.
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)
6 views5 pages

Java Streams 50 Problems

The document provides 50 common problems and solutions using Java Streams, showcasing various operations such as counting elements, filtering, mapping, and reducing. Each problem is accompanied by a code snippet demonstrating the solution. The document serves as a practical reference for developers looking to utilize Java Streams effectively.
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 – 50 COMMON PROBLEMS WITH CODE

1. Count elements

[Link]().count();

2. Filter even numbers

[Link]().filter(n -> n % 2 == 0).toList();

3. Convert to uppercase

[Link]().map(String::toUpperCase).toList();

4. Find max

[Link]().max(Integer::compare).get();

5. Find min

[Link]().min(Integer::compare).get();

6. Sum

[Link]().mapToInt(Integer::intValue).sum();

7. Sort

[Link]().sorted().toList();

8. Reverse sort

[Link]().sorted((a,b)->b-a).toList();

9. Distinct

[Link]().distinct().toList();

10. Limit

[Link]().limit(3).toList();

11. Skip

[Link]().skip(2).toList();
12. Any match

[Link]().anyMatch(n -> n > 10);

13. All match

[Link]().allMatch(n -> n > 0);

14. None match

[Link]().noneMatch(n -> n < 0);

15. Find first

[Link]().findFirst().get();

16. Find any

[Link]().findAny().get();

17. To set

[Link]().collect([Link]());

18. Join strings

[Link]().collect([Link](","));

19. Frequency

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

20. Group by length

[Link]().collect([Link](String::length));

21. Partition even/odd

[Link]().collect([Link](n->n%2==0));

22. Remove nulls

[Link]().filter(Objects::nonNull).toList();
23. Flatten

[Link]().flatMap(List::stream).toList();

24. To map

[Link]().collect([Link](x->x, x->x*x));

25. Peek debug

[Link]().peek([Link]::println).toList();

26. First non-repeating char

[Link]().mapToObj(c->(char)c).collect([Link](c->c, LinkedHashMap::new,
[Link]())).entrySet().stream().filter(e->[Link]()==1).findFirst().get().getKey();

27. Duplicates

[Link]().collect([Link](x->x, [Link]())).entrySet().stream().filter(e->
[Link]()>1).map([Link]::getKey).toList();

28. Second highest

[Link]().sorted((a,b)->b-a).skip(1).findFirst().get();

29. Average

[Link]().mapToInt(x->x).average().getAsDouble();

30. Sum reduce

[Link]().reduce(0, Integer::sum);

31. Multiply reduce

[Link]().reduce(1, (a,b)->a*b);

32. Longest string

[Link]().max([Link](String::length)).get();

33. Shortest string

[Link]().min([Link](String::length)).get();
34. Sort by length

[Link]().sorted([Link](String::length)).toList();

35. Count vowels

[Link]().filter(c->"aeiouAEIOU".indexOf(c)!=-1).count();

36. Reverse string

new StringBuilder(str).reverse().toString();

37. Check palindrome

[Link](new StringBuilder(str).reverse().toString());

38. Remove duplicates string

[Link]().distinct().mapToObj(c->""+(char)c).collect([Link]());

39. First digit

[Link]().filter(Character::isDigit).findFirst();

40. Count words

[Link]([Link](" ")).count();

41. Max frequency char

[Link]().mapToObj(c->(char)c).collect([Link](c->c,
[Link]())).entrySet().stream().max([Link]()).get();

42. Sum of squares

[Link]().map(n->n*n).reduce(0, Integer::sum);

43. Even squares

[Link]().filter(n->n%2==0).map(n->n*n).toList();

44. Merge lists

[Link]([Link](), [Link]()).toList();
45. Find duplicates count

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

46. Top 3 numbers

[Link]().sorted((a,b)->b-a).limit(3).toList();

47. Partition positive/negative

[Link]().collect([Link](n->n>=0));

48. Convert to array

[Link]().toArray();

49. Map to length

[Link]().map(String::length).toList();

50. Check empty

[Link]().findAny().isEmpty();

You might also like