-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLambdaFn.java
More file actions
59 lines (47 loc) · 1.54 KB
/
LambdaFn.java
File metadata and controls
59 lines (47 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.fresco;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import static java.lang.Math.*;
public class LambdaFn {
public static void main(String[] args) {
// TODO Auto-generated method stub
long a=301;
long b=400;
List<String> listOfIntegers = new ArrayList<>();
for(long i=a;i<=b;i++)
{
listOfIntegers.add(i+"");
}
// System.out.println(listOfIntegers);
List<Long> actual = functionalProgramming(listOfIntegers);
System.out.println(actual);
}
public List<Long> functionalProgramming(List<String> listOfIntegers)
{
List<Long> outputList = Collections.emptyList();
outputList=listOfIntegers.stream()
.map(s->Long.parseLong(s))
.filter(s->{
// count the number of digits
long cnt = 0;
long dup = s;
long dup1 = s;
while (dup>0) {
cnt+=1;
dup=dup/10;
}
long sum = 0;
while (dup1>0)
{
sum += Math.pow(dup1 % 10, cnt);
dup1 /= 10;
}
return (s == sum);
}).collect(Collectors.toList());
// System.out.println(outputList);
return outputList;
}
// important link for functional programming
// https://javarevisited.blogspot.com/2018/05/java-8-filter-map-collect-stream-example.html#axzz72YUGiEQM