-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnestedStreams.java
More file actions
60 lines (48 loc) · 1.9 KB
/
nestedStreams.java
File metadata and controls
60 lines (48 loc) · 1.9 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
60
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
/**
* Created by pranikchainani on 8/19/16.
*/
public class nestedStreams {
public static List<Integer> factors(int number)
{
return IntStream.rangeClosed(1, number)
.filter(i -> number % i == 0)
.boxed()
.collect(Collectors.toList());
}
// /Create a function Factors(list)
// this will call factors(int) at each step
public static Set<Integer> factors(List<Integer> numbers)
{
// Imperetive Style
// for(Integer number: numbers){
// allFactors.addAll(factors(number));
// }
//
// return allFactors;
// FunctionalStyle
// return numbers.stream() // Stream<Intger>
// .map( e -> factors(e))
// .reduce(new HashSet<>(),
// (factorSet,factorsForANumber) -> {
// factorSet.addAll(factorsForANumber);
// return factorSet;
// }, // Stream<set> { donald. trump, hillary, clinton)
// (set1, set2) -> {set1.addAll(set2);
// return set1;}); // Set
// Reduce explanations
// (set1, set2) -> Set1. addAll(set2); return outputSet
// (outputSet, set3) -> outputSet.addAll(set3); return outputSet
// (outputSet, set4) -> outputSet.addAll(set4); return outputSet;
return numbers.stream()
.flatMap( e -> factors(e).stream())
.collect(Collectors.toSet());
}
public static void main(String[] args) {
// Create a List here and call overloaded function factors(List)
List<Integer> numbers = Arrays.asList(1,4,6,10);
System.out.println(factors(numbers));
}
}