[JAVA] Ramda(람다식) 정리 . java 1.8이상

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
public class RamdaTest {
    
    
    interface Func{
        public int calc(int a, int b);
    }
    
    
 
    public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(“Hello Original”);
            }
        }).start();;    
        
        
        new Thread( ()> {
            System.out.println(“Hello Ramda”);
        }).start();;
        
        Func add = (int a, int b) > {
                return a + b;
            };
        Func sub = (int a ,int b) > a  b;
        
        System.out.println(“Ramda Add : “ + add.calc(12));
        System.out.println(“Ramda Sub : “ + sub.calc(12));
        
        int result = add.calc(13+ sub.calc(24);
        System.out.println(“Randa calc : “ + result);
        
        
        //forEach
        System.out.println(“forEach”);
        Arrays.asList(1,2,3).stream()
        .forEach(System.out::print);
        
        System.out.println();
        
        //map
        System.out.println(“map”);
        Arrays.asList(1,2,3).stream()
        .map(i > i*i)
        .forEach(System.out::print);
        
        //limit    ->해당 번째까지만 
        System.out.println();
        System.out.println(“limit”);
        Arrays.asList(1,2,3).stream()
        .limit(1)
        .forEach(System.out::print);
        
        //skip    -> 해당 index를 빼고 
        System.out.println();
        System.out.println(“skip”);
        Arrays.asList(1,2,3).stream()
        .skip(1)
        .forEach(System.out::print);
        
        //filter  -> 규칙에 해당되는것만 
        System.out.println();
        System.out.println(“filter”);
        Arrays.asList(1,2,3).stream()
        .filter(i> 3>i)
        .forEach(System.out::print);
        
        //flatMap  -> Map을 합쳐줌 
        System.out.println();
        System.out.println(“flatMap”);
        Arrays.asList(Arrays.asList(1,2),Arrays.asList(3,4,5),Arrays.asList(6,7,8,9)).stream()
        .flatMap(i > i.stream())
        .forEach(System.out::print); // 1,2,3,4,5,6,7,8,9
        
        
        //reduce  -> 단일요소로 반환 
        System.out.println();
        System.out.println(“reduce”);        
        Arrays.asList(1,2,3).stream()
        .reduce((a,b)> ab)
        .get(); // -4
        //앞에서부터 차근차근 계산됨. 1-2 => -1
        // -1 – 3  => -4 이렇게 순서대로 계산 
        
        
        //콜렉션 으로 
        System.out.println();
        System.out.println(“Collectors”);            
        Arrays.asList(1,2,3).stream()
        .collect(Collectors.toList());
        Arrays.asList(1,2,3).stream()
        .iterator();
    
        
        
 
    }
 
}
cs

댓글 남기기