I just practice lamdas java 8. My problem is as follows
Sum all digits in integers until their value is less than 10 (means one digit to the left) and check if it has 1
Input Example 1
100
Output Example 1
1 // true because its one
Input Example 2
55
Output Example 2
1 ie 5+5 = 10 then 1+0 = 1 so true
I wrote the code
System.out.println(Arrays.asList( String.valueOf(number).split("") ).stream() .map(Integer::valueOf) .mapToInt(i->i) .sum() == 1);
It works for input 1, i.e. 100, but not for input 2, i.e. 55, which I clearly understand that in the second case, 10 is the result because the iteration is not recursive.
So, how can I make this lambdas expression recursive so that it can work in the second case too? I can create a method with this lambda expression and call it every time until the return value is <10, but I was wondering if there is any approach in lambdas.
thanks
source share