-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
1857 lines (1400 loc) · 130 KB
/
index.html
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.0.0">
<link rel="apple-touch-icon" sizes="180x180" href="/images/Tiffany_Blue.ico">
<link rel="icon" type="image/png" sizes="32x32" href="/images/Tiffany_Blue.ico">
<link rel="icon" type="image/png" sizes="16x16" href="/images/Tiffany_Blue.ico">
<link rel="mask-icon" href="/images/Tiffany_Blue.ico" color="#222">
<link rel="stylesheet" href="/css/main.css">
<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.14.0/css/all.min.css">
<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">
<script class="hexo-configurations">
var NexT = window.NexT || {};
var CONFIG = {"hostname":"namhogim.github.io","root":"/","scheme":"Muse","version":"8.0.0","exturl":false,"sidebar":{"position":"right","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"Searching...","empty":"We didn't find any results for the search: ${query}","hits_time":"${hits} results found in ${time} ms","hits":"${hits} results found"},"path":"search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}};
</script>
<meta property="og:type" content="website">
<meta property="og:title" content="A Software Guy Blog">
<meta property="og:url" content="https://namhogim.github.io/index.html">
<meta property="og:site_name" content="A Software Guy Blog">
<meta property="og:locale" content="en_US">
<meta property="article:author" content="Namho Kim">
<meta property="article:tag" content="Software Engineering">
<meta name="twitter:card" content="summary">
<link rel="canonical" href="https://namhogim.github.io/">
<script class="page-configurations">
// https://hexo.io/docs/variables.html
CONFIG.page = {
sidebar: "",
isHome : true,
isPost : false,
lang : 'en'
};
</script>
<title>A Software Guy Blog</title>
<noscript>
<style>
body { margin-top: 2rem; }
.use-motion .menu-item,
.use-motion .sidebar,
.use-motion .post-block,
.use-motion .pagination,
.use-motion .comments,
.use-motion .post-header,
.use-motion .post-body,
.use-motion .collection-header {
visibility: visible;
}
.use-motion .header,
.use-motion .site-brand-container .toggle,
.use-motion .footer { opacity: initial; }
.use-motion .site-title,
.use-motion .site-subtitle,
.use-motion .custom-logo-image {
opacity: initial;
top: initial;
}
.use-motion .logo-line {
transform: scaleX(1);
}
.search-pop-overlay, .sidebar-nav { display: none; }
.sidebar-panel { display: block; }
</style>
</noscript>
</head>
<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
<div class="headband"></div>
<main class="main">
<header class="header" itemscope itemtype="http://schema.org/WPHeader">
<div class="header-inner"><div class="site-brand-container">
<div class="site-nav-toggle">
<div class="toggle" aria-label="Toggle navigation bar">
<span class="toggle-line"></span>
<span class="toggle-line"></span>
<span class="toggle-line"></span>
</div>
</div>
<div class="site-meta">
<a href="/" class="brand" rel="start">
<i class="logo-line"></i>
<h1 class="site-title">A Software Guy Blog</h1>
<i class="logo-line"></i>
</a>
</div>
<div class="site-nav-right">
<div class="toggle popup-trigger">
<i class="fa fa-search fa-fw fa-lg"></i>
</div>
</div>
</div>
<nav class="site-nav">
<ul class="main-menu menu">
<li class="menu-item menu-item-home">
<a href="/" rel="section"><i class="fa fa-home fa-fw"></i>Home</a>
</li>
<li class="menu-item menu-item-about">
<a href="/namho/" rel="section"><i class="fa fa-user fa-fw"></i>About</a>
</li>
<li class="menu-item menu-item-tags">
<a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>Tags</a>
</li>
<li class="menu-item menu-item-categories">
<a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>Categories</a>
</li>
<li class="menu-item menu-item-archives">
<a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>Archives</a>
</li>
<li class="menu-item menu-item-search">
<a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>Search
</a>
</li>
</ul>
</nav>
<div class="search-pop-overlay">
<div class="popup search-popup">
<div class="search-header">
<span class="search-icon">
<i class="fa fa-search"></i>
</span>
<div class="search-input-container">
<input autocomplete="off" autocapitalize="off" maxlength="80"
placeholder="Searching..." spellcheck="false"
type="search" class="search-input">
</div>
<span class="popup-btn-close">
<i class="fa fa-times-circle"></i>
</span>
</div>
<div class="search-result-container no-result">
<div class="search-result-icon">
<i class="fa fa-spinner fa-pulse fa-5x"></i>
</div>
</div>
</div>
</div>
</div>
<div class="toggle sidebar-toggle">
<span class="toggle-line"></span>
<span class="toggle-line"></span>
<span class="toggle-line"></span>
</div>
<aside class="sidebar">
<div class="sidebar-inner sidebar-overview-active">
<ul class="sidebar-nav">
<li class="sidebar-nav-toc">
Table of Contents
</li>
<li class="sidebar-nav-overview">
Overview
</li>
</ul>
<!--noindex-->
<section class="post-toc-wrap sidebar-panel">
</section>
<!--/noindex-->
<section class="site-overview-wrap sidebar-panel">
<div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
<img class="site-author-image" itemprop="image" alt="Namho Kim"
src="/images/Tiffany_Blue.png">
<p class="site-author-name" itemprop="name">Namho Kim</p>
<div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap animated">
<nav class="site-state">
<div class="site-state-item site-state-posts">
<a href="/archives/">
<span class="site-state-item-count">26</span>
<span class="site-state-item-name">posts</span>
</a>
</div>
<div class="site-state-item site-state-categories">
<a href="/categories/">
<span class="site-state-item-count">24</span>
<span class="site-state-item-name">categories</span></a>
</div>
<div class="site-state-item site-state-tags">
<a href="/tags/">
<span class="site-state-item-count">52</span>
<span class="site-state-item-name">tags</span></a>
</div>
</nav>
</div>
<div class="links-of-author animated">
<span class="links-of-author-item">
<a href="https://github.com/NamhoGim" title="GitHub → https://github.com/NamhoGim" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
</span>
<span class="links-of-author-item">
<a href="https://www.linkedin.com/in/namho-kim-6b1848118" title="LinkedIn → https://www.linkedin.com/in/namho-kim-6b1848118" rel="noopener" target="_blank"><i class="fab fa-linkedin fa-fw"></i>LinkedIn</a>
</span>
</div>
</section>
</div>
</aside>
<div class="sidebar-dimmer"></div>
</header>
<div class="back-to-top">
<i class="fa fa-arrow-up"></i>
<span>0%</span>
</div>
<a href="https://github.com/NamhoGim" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>
<noscript>
<div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>
<div class="main-inner index posts-expand">
<article itemscope itemtype="http://schema.org/Article" class="post-block" lang="en">
<link itemprop="mainEntityOfPage" href="https://namhogim.github.io/2020/10/05/stream-api/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/Tiffany_Blue.png">
<meta itemprop="name" content="Namho Kim">
<meta itemprop="description" content="">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="A Software Guy Blog">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">
<a href="/2020/10/05/stream-api/" class="post-title-link" itemprop="url">Stream API</a>
</h2>
<div class="post-meta-container">
<div class="post-meta">
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar"></i>
</span>
<span class="post-meta-item-text">Posted on</span>
<time title="Created: 2020-10-05 08:32:52" itemprop="dateCreated datePublished" datetime="2020-10-05T08:32:52+09:00">2020-10-05</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar-check"></i>
</span>
<span class="post-meta-item-text">Edited on</span>
<time title="Modified: 2020-10-07 09:01:59" itemprop="dateModified" datetime="2020-10-07T09:01:59+09:00">2020-10-07</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-folder"></i>
</span>
<span class="post-meta-item-text">In</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/Stream-API/" itemprop="url" rel="index"><span itemprop="name">Stream API</span></a>
</span>
</span>
</div>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<p>Most of the contents are excerpt from “Modern Java in Action”</p>
<h2 id="What-are-streams"><a href="#What-are-streams" class="headerlink" title="What are streams?"></a>What are streams?</h2><p><em>Streams</em> are an update to the Java API that let you manipulate collections of data<br>in a declarative way (you express a query rather than code an ad hoc implementation<br>for it.) In addition, streams can be processed in parallel <em>transparently</em>, without you having to write any multithreaded code.</p>
<ul>
<li>Before (Java 7)</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">List<Dish> lowCaloricDishes = <span class="keyword">new</span> ArrayList<>();</span><br><span class="line"><span class="keyword">for</span>(Dish dish: menu) {</span><br><span class="line"> <span class="keyword">if</span>(dish.getCalories() < <span class="number">400</span>) {</span><br><span class="line"> lowCaloricDishes.add(dish); <span class="comment">// Filters the elements using an accumulator</span></span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">Collections.sort(lowCaloricDishes, <span class="keyword">new</span> Comparator<Dish>() { <span class="comment">// Sorts the dishes with an anonymous class</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">compare</span><span class="params">(Dish dish1, Dish dish2)</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> Integer.compare(dish1.getCalories(), dish2.getCalories());</span><br><span class="line"> }</span><br><span class="line">});</span><br><span class="line"></span><br><span class="line">List<String> lowCaloricDishesName = <span class="keyword">new</span> ArrayList<>();</span><br><span class="line"><span class="keyword">for</span>(Dish dish: lowCaloricDishes) {</span><br><span class="line"> lowCaloricDishesName.add(dish.getName()); <span class="comment">// Processes the sorted list to select the names of dishes</span></span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<ul>
<li>After (Java 8)</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> <span class="keyword">static</span> java.util.Comparator.comparing;</span><br><span class="line"><span class="keyword">import</span> <span class="keyword">static</span> java.util.stream.Collectors.toList;</span><br><span class="line">List<String> lowCaloricDishsesName =</span><br><span class="line"> menu.stream()</span><br><span class="line"> .filter(d -> d.getCalories() < <span class="number">400</span>)</span><br><span class="line"> .sorted(comparing(Dish::getCalories))</span><br><span class="line"> .map(Dish::getName)</span><br><span class="line"> .collect(toList());</span><br></pre></td></tr></table></figure>
<p>You can see that the new approach offers several immediate benefits from a software<br>engineering point of view:</p>
<ul>
<li><p>The code is written in a <em>declarative way</em>: you specify <em>what</em> you what to achieve<br>as opposed to specifying <em>how</em> to implement an operation (using control-flow blocks such as loops and <code>if</code> conditions)</p>
</li>
<li><p>You chain together several building-block operations to express a complicated<br>data-processing pipeline while keeping your code readable and its intent clear.</p>
</li>
</ul>
<p>The Stream API in Java 8 lets you write code that’s</p>
<ul>
<li><em>Declarative</em> – More concise and readable</li>
<li><em>Composable</em> – Greater flexibility</li>
<li><em>Parallelizable</em> – Better performance</li>
</ul>
<h2 id="Stream-operations"><a href="#Stream-operations" class="headerlink" title="Stream operations"></a>Stream operations</h2><p>Stream operations that can be connected are called <em>intermediate operations</em>, and oerations that close a stream are called <em>terminal operations</em>.</p>
<h3 id="Intermediate-operations"><a href="#Intermediate-operations" class="headerlink" title="Intermediate operations"></a>Intermediate operations</h3><p>Intermediate operations such as <code>filer</code> or <code>sorted</code> return another stream as the return type. This allows the operations to be connected to form a query.<br>What’s important is that inermediate operations don’t perform any processing until a terminal operations can usually be merged and processed into a single pass by terminal operation.</p>
<h3 id="Terminal-operations"><a href="#Terminal-operations" class="headerlink" title="Terminal operations"></a>Terminal operations</h3><p>Terminal operations produce a result from a stream pipleline.<br>A result is any non-stream value such as <code>List</code>, an <code>Integer</code>, or even <code>void</code>.<br>For example, in the following pipeline, <code>forEach</code> is terminal operation that returns<br><code>void</code> and applies a lambda to each object. Passing <code>System.out.println</code> to <code>forEach</code> asks it to print every object in the stream.</p>
<p><code>stream().forEach(System.out::println);</code></p>
<h2 id="Working-with-streams"><a href="#Working-with-streams" class="headerlink" title="Working with streams"></a>Working with streams</h2><h3 id="Filtering"><a href="#Filtering" class="headerlink" title="Filtering"></a>Filtering</h3><h4 id="Filtering-with-a-predicate"><a href="#Filtering-with-a-predicate" class="headerlink" title="Filtering with a predicate"></a>Filtering with a predicate</h4><p>The <code>Stream</code> interface supports a <code>filter</code> method. This operations takes as argument a <em>predicate</em> (a function returning <code>boolean</code>) and returns a stream including all elements that match the predicate.</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> <span class="keyword">static</span> java.util.stream.Collectors.toList;</span><br><span class="line">List<Dish> vegetarianDishes =</span><br><span class="line"> menu.stream()</span><br><span class="line"> .filter(Dish::isVegetarian) <span class="comment">// Use a method reference to check if dish is vegetarian friendly.</span></span><br><span class="line"> .collect(toList());</span><br></pre></td></tr></table></figure>
<h4 id="Filtering-unique-elements"><a href="#Filtering-unique-elements" class="headerlink" title="Filtering unique elements"></a>Filtering unique elements</h4><p>Streams also support a method called <code>distinct</code> that returns a stream with unique<br>elements (according to the implementation of the <code>hashcode</code> and <code>equals</code> methods of<br>the objects produced by the stream).</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">List<Integer> numbers = Arrays.asList(<span class="number">1</span>, <span class="number">2</span>, <span class="number">1</span>, <span class="number">3</span>, <span class="number">3</span>, <span class="number">2</span>, <span class="number">4</span>);</span><br><span class="line">numbers.stream()</span><br><span class="line"> .filter(i -> i % <span class="number">2</span> == <span class="number">0</span>)</span><br><span class="line"> .distinct()</span><br><span class="line"> .forEach(System.out::println);</span><br></pre></td></tr></table></figure>
<h4 id="Truncating-a-stream"><a href="#Truncating-a-stream" class="headerlink" title="Truncating a stream"></a>Truncating a stream</h4><p>Streams support the <code>limit(n)</code> methods, which returns another stream that’s no<br>longer thant a given size. The requested size is passed as argument to <code>limit</code>.<br>If the stream is ordered, the first elements are returned up to a maximum of <code>n</code>.</p>
<h4 id="Skipping-elements"><a href="#Skipping-elements" class="headerlink" title="Skipping elements"></a>Skipping elements</h4><p>Streams support the <code>skip(n)</code> method to return a stream that discards the first <code>n</code><br>elements. If the stream has fewer than <code>n</code> elements, an empty stream is returned.<br>Note that <code>limit(n)</code> and <code>skip(n)</code> are complementary.</p>
<h3 id="Mapping"><a href="#Mapping" class="headerlink" title="Mapping"></a>Mapping</h3><p>A common data processing idion is to select information from certain objects.<br>For example, in SQL you can select a particular column from a table. The Stream API<br>provides similar facilities through the <code>map</code> and <code>flatMap</code> methods.</p>
<h4 id="Applying-a-function-to-each-element-of-a-stream"><a href="#Applying-a-function-to-each-element-of-a-stream" class="headerlink" title="Applying a function to each element of a stream"></a>Applying a function to each element of a stream</h4><p>Stream support the <code>map</code> method, which takes a function as argument. The function is<br>Applied to each element, mapping it into a new element (the word <em>mapping</em> is used<br>because it has a meaning similar to <em>transforming</em> but with the nuance of “creating<br>a new version of” rather than “modifying”).<br>For example, in the following code you pass a method reference <code>Dish::getName</code> to the <code>map</code> method to <em>extract</em> the names of the dishess in the stream:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">List<String> dishNames = menu.stream()</span><br><span class="line"> .map(Dish::getName)</span><br><span class="line"> .collect(toList());</span><br></pre></td></tr></table></figure>
<h4 id="Flattening-streams"><a href="#Flattening-streams" class="headerlink" title="Flattening streams"></a>Flattening streams</h4><p>How could you return a list of all the <em>unique characters</em> for a list of words?<br>For example, given the list of words [“Hello”, “World”] -> [“H”, “e”, “l”, “o”, “W”, “r”, “d”].</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">words.stream()</span><br><span class="line"> .map(word -> word.spit(<span class="string">""</span>))</span><br><span class="line"> .distinct()</span><br><span class="line"> .collect(toList());</span><br></pre></td></tr></table></figure>
<p>Problem: the <code>map</code> method returns a <code>String[]</code> (an array of String) for each word.</p>
<h4 id="Attempt-Using-Map-and-Arrays-stream"><a href="#Attempt-Using-Map-and-Arrays-stream" class="headerlink" title="Attempt Using Map and Arrays.stream"></a>Attempt Using Map and Arrays.stream</h4><p>First, you need a stream of characters instead of a stream of arrays. There’s a method called <code>Arrays.stream()</code> that takes an array and produces a stream:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">String[] arrayOfWord = {<span class="string">"Goodbye"</span>, <span class="string">"World"</span>};</span><br><span class="line">Stream<String> streamOfWords = Arrays.stream(arrayOfWords);</span><br></pre></td></tr></table></figure>
<p>Use it in the previous pipeline to see what happens:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">words.stream()</span><br><span class="line"> .map(word -> word.split(<span class="string">""</span>))</span><br><span class="line"> .map(Arrays::stream)</span><br><span class="line"> .distinct()</span><br><span class="line"> .collect(toList());</span><br></pre></td></tr></table></figure>
<p>Indeed, first convert each word into an array of its individual letters and then<br>make each array into a separate stream.</p>
<h4 id="Using-flatMap"><a href="#Using-flatMap" class="headerlink" title="Using flatMap"></a>Using <code>flatMap</code></h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">List<String> uniqueCharacters =</span><br><span class="line"> words.stream()</span><br><span class="line"> .map(word -> word.split(<span class="string">""</span>)) <span class="comment">// Converts each word into a array of its individual letters</span></span><br><span class="line"> .flatMap(Arrays::stream) <span class="comment">// Flatten each generated stream into single stream</span></span><br><span class="line"> .distinct()</span><br><span class="line"> .collect(toList());</span><br></pre></td></tr></table></figure>
<p>Using the <code>flatMap</code> method has the effect of mapping each array not with a stream<br>but <em>with the contents of that stream.</em> All the separate streams that were generated<br>when using <code>map(Arrays::stream)</code> get amalgamated – flatten into a single stream.</p>
<h3 id="Finding-and-matching"><a href="#Finding-and-matching" class="headerlink" title="Finding and matching"></a>Finding and matching</h3><p>Another common data processing idiom is finding whether some elements in a set of<br>data match a given property.<br>The Stream API provides such facilities through the <code>allMatch</code>, <code>anyMatch</code>, <code>noneMatch</code>, <code>findFirst</code>, and <code>findAny</code></p>
<ul>
<li><p><code>anyMatch(Predicate<T>)</code>: Checking to see if a predicate matches at least one element</p>
</li>
<li><p><code>allMatch(Predicate<T>)</code>: Checking to see if a predicate matches all elements</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">boolean</span> isHealthy = menu.stream()</span><br><span class="line"> .allMatch(dish -> dish.getCalories() < <span class="number">1000</span>);</span><br></pre></td></tr></table></figure>
</li>
<li><p><code>noneMatch(Predicate<T>)</code>: The opposite of <code>allMatch</code> is <code>noneMatch</code>. It ensures that no elements in the stream match the given predicate.</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">boolean</span> isHealth = menu.stream()</span><br><span class="line"> .noneMatch(d -> d.getCalories() >= <span class="number">1000</span>);</span><br></pre></td></tr></table></figure>
</li>
<li><p><code>findAny()</code>: Returns an arbitrary element of the current stream.</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">menu.stream()</span><br><span class="line"> .filter(Dish::isVegetarian)</span><br><span class="line"> .findAny() <span class="comment">// Returns an Optional<Dish></span></span><br><span class="line"> .ifPresent(dish -> System.out.println(dish.getName())); <span class="comment">// If a value is contained, it's printed; otherwise nothing happens.</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><code>findFirst()</code>: Returns a first element in an <em>encounter order.</em></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">List<Integer> someNumbers = Arrays.asList(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>);</span><br><span class="line">Optional<Integer> firstSquareDivisibleByThree =</span><br><span class="line"> someNumbers.stream()</span><br><span class="line"> .map(n -> n * n)</span><br><span class="line"> .filter(n -> n % <span class="number">3</span> == <span class="number">0</span>)</span><br><span class="line"> .findFirst(); <span class="comment">// 9</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
<h3 id="Reducing"><a href="#Reducing" class="headerlink" title="Reducing"></a>Reducing</h3><h4 id="Summing-the-elements"><a href="#Summing-the-elements" class="headerlink" title="Summing the elements"></a>Summing the elements</h4><p>You can sum all elements of a stream as follows:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span> sum = numbers.stream().reduce(<span class="number">0</span>, (a, b) -> a + b);</span><br></pre></td></tr></table></figure>
<p><code>reduce</code> takes two arguments:</p>
<ul>
<li>An initial value, here 0.</li>
<li>A <code>BinaryOperator<T></code> to combine two elements and produce a new value; here<br>you use the lambda (a, b) -> a + b.</li>
</ul>
<h4 id="Maximum-and-minimum"><a href="#Maximum-and-minimum" class="headerlink" title="Maximum and minimum"></a>Maximum and minimum</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">Optional<Integer> max = numbers.stream().reduce(Integer::max);</span><br><span class="line">Optional<Integer> min = numbers.stream().reduce(Integer::min);</span><br></pre></td></tr></table></figure>
<h3 id="Intermediate-and-terminal-operations"><a href="#Intermediate-and-terminal-operations" class="headerlink" title="Intermediate and terminal operations"></a>Intermediate and terminal operations</h3><table>
<thead>
<tr>
<th align="left">Operation</th>
<th align="left">Type</th>
<th align="left">Return type</th>
<th align="left">Type / functional interface used</th>
<th align="left">Function descriptor</th>
</tr>
</thead>
<tbody><tr>
<td align="left"><code>filter</code></td>
<td align="left">Intermediate</td>
<td align="left"><code>Stream<T></code></td>
<td align="left"><code>Predicate<T></code></td>
<td align="left"><code>T -> boolean</code></td>
</tr>
<tr>
<td align="left"><code>distinct</code></td>
<td align="left">Intermediate</td>
<td align="left"><code>Stream<T></code></td>
<td align="left"></td>
<td align="left"></td>
</tr>
<tr>
<td align="left"><code>takeWhile</code></td>
<td align="left">Intermediate</td>
<td align="left"><code>Stream<T></code></td>
<td align="left"><code>Predicate<T></code></td>
<td align="left"><code>T -> boolean</code></td>
</tr>
<tr>
<td align="left"><code>dropWhile</code></td>
<td align="left">Intermediate</td>
<td align="left"><code>Stream<T></code></td>
<td align="left"><code>Predicate<T></code></td>
<td align="left"><code>T -> boolean</code></td>
</tr>
<tr>
<td align="left"><code>skip</code></td>
<td align="left">Intermediate</td>
<td align="left"><code>Stream<T></code></td>
<td align="left"><code>long</code></td>
<td align="left"></td>
</tr>
<tr>
<td align="left"><code>limit</code></td>
<td align="left">Intermediate</td>
<td align="left"><code>Stream<T></code></td>
<td align="left"><code>long</code></td>
<td align="left"></td>
</tr>
<tr>
<td align="left"><code>map</code></td>
<td align="left">Intermediate</td>
<td align="left"><code>Stream<R></code></td>
<td align="left"><code>Function<T, R></code></td>
<td align="left"><code>T -> R</code></td>
</tr>
<tr>
<td align="left"><code>flatMap</code></td>
<td align="left">Intermediate</td>
<td align="left"><code>Stream<R></code></td>
<td align="left"><code>Function<T, Stream<R>></code></td>
<td align="left"><code>T -> Stream<R></code></td>
</tr>
<tr>
<td align="left"><code>sorted</code></td>
<td align="left">Intermediate</td>
<td align="left"><code>Stream<T></code></td>
<td align="left"><code>Comparator<T></code></td>
<td align="left"><code>(T, T) -> int</code></td>
</tr>
<tr>
<td align="left"><code>anyMatch</code></td>
<td align="left">Terminal</td>
<td align="left"><code>boolean</code></td>
<td align="left"><code>Predicate<T></code></td>
<td align="left"><code>T -> boolean</code></td>
</tr>
<tr>
<td align="left"><code>noneMatch</code></td>
<td align="left">Terminal</td>
<td align="left"><code>boolean</code></td>
<td align="left"><code>Predicate<T></code></td>
<td align="left"><code>T -> boolean</code></td>
</tr>
<tr>
<td align="left"><code>allMatch</code></td>
<td align="left">Terminal</td>
<td align="left"><code>boolean</code></td>
<td align="left"><code>Predicate<T></code></td>
<td align="left"><code>T -> boolean</code></td>
</tr>
<tr>
<td align="left"><code>findAny</code></td>
<td align="left">Terminal</td>
<td align="left"><code>Optional<T></code></td>
<td align="left"></td>
<td align="left"></td>
</tr>
<tr>
<td align="left"><code>findFirst</code></td>
<td align="left">Terminal</td>
<td align="left"><code>Optional<T></code></td>
<td align="left"></td>
<td align="left"></td>
</tr>
<tr>
<td align="left"><code>forEach</code></td>
<td align="left">Terminal</td>
<td align="left"><code>void</code></td>
<td align="left"><code>Consumer<T></code></td>
<td align="left"><code>T -> void</code></td>
</tr>
<tr>
<td align="left"><code>collect</code></td>
<td align="left">Terminal</td>
<td align="left"><code>R</code></td>
<td align="left"><code>Collector<T, A, R></code></td>
<td align="left"></td>
</tr>
<tr>
<td align="left"><code>reduce</code></td>
<td align="left">Terminal</td>
<td align="left"><code>Optional<T></code></td>
<td align="left"><code>BinaryOperator<T></code></td>
<td align="left"><code>(T, T) -> T</code></td>
</tr>
<tr>
<td align="left"><code>count</code></td>
<td align="left">Terminal</td>
<td align="left"><code>long</code></td>
<td align="left"></td>
<td align="left"></td>
</tr>
</tbody></table>
<h2 id="Collecting-data-with-streams"><a href="#Collecting-data-with-streams" class="headerlink" title="Collecting data with streams"></a>Collecting data with streams</h2><p>The <code>collect</code> is a reduction operation, like <code>reduce</code>, that takes as an argument<br>various recipes for accumulating the elements of a stream into a summary result.<br>These recipes are defined by a new <code>Collector</code> interface, so it’s important to<br>distinguish <code>Collection</code>, <code>Collector</code>, and <code>collect</code>.</p>
<p>Here are some example queries of what you’ll be able to do using <code>collect</code> and<br>collectors:</p>
<ul>
<li>Group a list of transactions by currency to obtain the sum of the values of all<br>transactions with that currency (returning a <code>Map<Currency, Integer></code>)</li>
<li>Partition a list of transactions into two groups: expensive and not expensive<br>(returnting a <code>Map<Boolean, List<Transaction>></code>)</li>
<li>Create multilevel groupings, such as grouping transactions by cities and then<br>further categorizing by whether they’re expensive of not (returning a <code>Map<String, Map<Boolean, List<Transaction>>></code>)</li>
</ul>
<p>Grouping transactions by currency in imperative style</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">Map<Currency, List<Transaction>> transactionsByCurrencies = <span class="keyword">new</span> HashMap<>();</span><br><span class="line"><span class="keyword">for</span> (Transaction transaction : transactions) {</span><br><span class="line"> Currency currency = transaction.getCurrency(); <span class="comment">// Extracts the Transaction’s currency</span></span><br><span class="line"> List<Transaction> transactionsForCurrency = transactionsByCurrencies.get(currency);</span><br><span class="line"> <span class="keyword">if</span> (transactionsForCurrency == <span class="keyword">null</span>) {</span><br><span class="line"> transactionsForCurrency = <span class="keyword">new</span> ArrayList<>();</span><br><span class="line"> transactionsByCurrencies.put(currency, transactionsForCurrency);</span><br><span class="line"> }</span><br><span class="line"> transactionsForCurrency.add(transaction);</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>Using a more general <code>Collector</code> paramter to the <code>collect</code> method on stream rather<br>than the <code>toList</code> special case used in the previous chapter:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">Map<Currency, List<Transaction>> transactionsByCurrencies =</span><br><span class="line"> transactions.stream().collect(groupingBy(Transaction::getCurrency));</span><br></pre></td></tr></table></figure>
<h3 id="Grouping"><a href="#Grouping" class="headerlink" title="Grouping"></a>Grouping</h3><p>A common database operation is to group items in a set, based on one or more properties.<br>You can easily perform this task using a collector returned by the Collectors.groupBy factory method, as follows:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Map<Dish.Type, List<Dish>> dishesByType = menu.stream().collect(groupingBy(Dish::getType));</span><br></pre></td></tr></table></figure>
<p>Here, you pass to the <code>groupingBy</code> method a <code>Function</code> (expressed in the form of a<br>method reference) extracting the corresponding <code>Dish.Type</code> for each <code>Dish</code> in the stream. We call this <code>Function</code> a <em>classification</em> function specifically because it’s used to classify the elements of the stream into different groups.</p>
<h3 id="Partitioning"><a href="#Partitioning" class="headerlink" title="Partitioning"></a>Partitioning</h3><p>Partitioning is a special case of grouping: having a predicate called a <em>partitioning function</em> as a classification fuction. The fact that the partitioning<br>fuction returns a boolean means the resulting grouping <code>Map</code> will have <code>Boolean</code> as<br>a key type, and therefore, there can be at most two different groups – one for <code>true</code> and one for <code>false</code>.</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">Map<Boolean, List<Dish>> partitionedMenu =</span><br><span class="line"> menu.stream().collect(partitioningBy(Dish::isVegetarian));</span><br></pre></td></tr></table></figure>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article itemscope itemtype="http://schema.org/Article" class="post-block" lang="en">
<link itemprop="mainEntityOfPage" href="https://namhogim.github.io/2020/10/03/layered-architecture/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/Tiffany_Blue.png">
<meta itemprop="name" content="Namho Kim">
<meta itemprop="description" content="">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="A Software Guy Blog">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">
<a href="/2020/10/03/layered-architecture/" class="post-title-link" itemprop="url">Layered Architecture</a>
</h2>
<div class="post-meta-container">
<div class="post-meta">
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar"></i>
</span>
<span class="post-meta-item-text">Posted on</span>
<time title="Created: 2020-10-03 11:31:23 / Modified: 11:40:22" itemprop="dateCreated datePublished" datetime="2020-10-03T11:31:23+09:00">2020-10-03</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-folder"></i>
</span>
<span class="post-meta-item-text">In</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/Web-Architecture/" itemprop="url" rel="index"><span itemprop="name">Web Architecture</span></a>
</span>
</span>
</div>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h2 id="Background-MVC-model"><a href="#Background-MVC-model" class="headerlink" title="Background: MVC model"></a>Background: MVC model</h2><blockquote>
<p>Model-view-controller (usually known as MVC) is a software design pattern<br>commonly used for developing user interfaces that divides the related program logic<br>into three interconnected elements.</p>
</blockquote>
<h3 id="Components"><a href="#Components" class="headerlink" title="Components"></a>Components</h3><p><img src="/images/MVC-Process-crop.pdf" alt="MVC Process"></p>
<h4 id="Model"><a href="#Model" class="headerlink" title="Model"></a>Model</h4><blockquote>
<p>The central component of the pattern. It is the application’s dynamic data structure,<br>independent of the user interface. It directly manages the data, logic and rules of the application.</p>
</blockquote>
<h4 id="View"><a href="#View" class="headerlink" title="View"></a>View</h4><blockquote>
<p>Any representation of information such as chart, diagram or table.<br>Mulitple view of the same informantion are possible,<br>such as a bar chart for management and tabular view for accountants.</p>
</blockquote>
<h4 id="Controller"><a href="#Controller" class="headerlink" title="Controller"></a>Controller</h4><blockquote>
<p>Accepts input and converts it to commands for the model or view</p>
<ul>
<li>The model is responsible for managing the data of the application. It receives user input from the controller.</li>
<li>The view means presentation of the model in a particular format.</li>
<li>The controller responds to the user input and performs interactions on the data model objects.<br>The controller receives the input, optionally validates it and then passes the input to the model.</li>
</ul>
</blockquote>
<p>웹 페이지는 중복 개발되는 요소가 존재한다.</p>
<p>Controller에서 중복으로 호출 되는 부분을 처리하기 위해서</p>
<ul>
<li>별도의 객체로 분리한다.</li>
<li>별도의 메소드로 분리한다.</li>
</ul>
<h2 id="Controller와-Service"><a href="#Controller와-Service" class="headerlink" title="Controller와 Service"></a>Controller와 Service</h2><p>Business method를 별도의 Service 객체에서 구현하도록 하고 컨트롤러는 Service객체를 사용하도록 한다.</p>
<h3 id="Service-객체"><a href="#Service-객체" class="headerlink" title="Service 객체"></a>Service 객체</h3><blockquote>
<p>Between the controller and the model, a layer called a <em>service</em> is sometimes interposed.<br>It fetches data from the model and lets the controller use the fetched data.<br>This layer allows a cleaner separation of data storage (model), data fetching (service) and data manipulation (controller).<br>Since this layer is not part of the original MVC concept, it is optional,<br>but can be useful for code management and reusability purposes in some cases.</p>
</blockquote>
<p>Business logic을 수행하는 method를 가지고 있는 객체를 서비스 객체라고 한다.<br>보통 하나의 Business logic은 하나의 <em>transaction</em> 으로 동작한다.</p>
<h3 id="Transaction"><a href="#Transaction" class="headerlink" title="Transaction"></a>Transaction</h3><ol>
<li><p>원자성 (Atomicity)<br>모든 과정이 성공했을 때만 정보를 반영한다</p>
</li>
<li><p>일관성 (Consistency)<br>Transaction 작업 처리 결과가 항상 일관성이 있어야 한다. 처음에 참조한 데이터로 transaction이 진행 되어야 한다. 각 사용자가 일관된 데이터를 볼 수 있다.</p>
</li>
<li><p>독립성 (Isolation)<br>독립성은 둘 이상의 Transaction이 동시에 병행 실행되고 있을 경우, 어느 하나의 transaction이라도 다른 transaction의 연산에 끼어들 수 없다.</p>
</li>
<li><p>지속성 (Durability)<br>지속성은 transaction이 성공적으로 완료되었을 경우, 결과는 영구적으로 반영되어야 한다는 점 입니다.</p>
</li>
</ol>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article itemscope itemtype="http://schema.org/Article" class="post-block" lang="en">
<link itemprop="mainEntityOfPage" href="https://namhogim.github.io/2020/10/03/REST-API/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/Tiffany_Blue.png">
<meta itemprop="name" content="Namho Kim">
<meta itemprop="description" content="">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="A Software Guy Blog">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">
<a href="/2020/10/03/REST-API/" class="post-title-link" itemprop="url">REST API</a>
</h2>
<div class="post-meta-container">
<div class="post-meta">
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar"></i>
</span>
<span class="post-meta-item-text">Posted on</span>
<time title="Created: 2020-10-03 11:30:57 / Modified: 11:46:04" itemprop="dateCreated datePublished" datetime="2020-10-03T11:30:57+09:00">2020-10-03</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-folder"></i>
</span>
<span class="post-meta-item-text">In</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/REST/" itemprop="url" rel="index"><span itemprop="name">REST</span></a>
</span>
,
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/REST/REST-API/" itemprop="url" rel="index"><span itemprop="name">REST API</span></a>
</span>
</span>
</div>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h2 id="Background"><a href="#Background" class="headerlink" title="Background"></a>Background</h2><p>2000년도에 Roy Fielding 박사의 학위논문에 REST라는 개념이 처음 등장<br><strong>REST</strong>는 ‘Representational State Transfer’의 약자로 자원을 이름으로 구분 하여<br>해당 자원의 상태(정보)를 주고 받는 것을 의미한다.<br>Roy Fielding은 Web Architecture의 요구사항과 해결해야할 문제를 설명하고 이를 해결하기 위한<br>접근 방법을 논문에서 제시 하며, 이를 위한 아키텍처 스타일인 REST를 소개 합니다.</p>
<p>REST는 기본적으로 Web의 기존 기술과 HTTP protocol을 그대로 활용하기 때문에 Web의 장점을 최대한<br>활용할 수 있는 아키텍처 스타일이다.</p>
<h3 id="REST-구성"><a href="#REST-구성" class="headerlink" title="REST 구성"></a>REST 구성</h3><p>REST API는 다음과 같은 3가지 구분으로 구성 됩니다.</p>
<ul>
<li>자원(Resource): 자원은 Data, Meta Data, HATEOAS</li>
<li>행위(Verb): HTTP Method로 표현됩니다.</li>
<li>표현(Representations)</li>
</ul>
<h3 id="REST의-특징"><a href="#REST의-특징" class="headerlink" title="REST의 특징"></a>REST의 특징</h3><ol>
<li><p>Uniform Interface<br>구성 요소(Client, Server 등) 사이의 인터페이스는 균일(uniform) 해야한다.<br>인터페이스를 일반화함으로써, 전체 시스템의 아키텍처가 단순해지고, 상호 작용의 가시성이 개선되며,<br>구현과 서비스가 분리되므로 독립적인 진화가 가능해질 수 있다.</p>
</li>
<li><p>Stateless<br>클라이언트와 서버의 통신에는 상태가 없어야한다. 모든 요청은 필요한 모든 정보를 담고 있어야 한다.<br>요청 하나만 봐도 뭔지 알 수 있으므로 가시성이 개선되고, task 실패시 복원이 쉬우므로 신뢰성이 개선되며,<br>상태를 저장할 필요가 없으므로 규모 확장성이 개선될 수 있다.</p>
</li>
<li><p>Cacheable<br>캐시가 가능해야한다. 즉, 모든 서버 응답은 캐시가 가능한지 아닌지 알 수 있어야함<br>효율, 규모 확장성, 사용자 입장에서의 성능이 개선 된다.</p>
</li>
<li><p>Self-descriptiveness<br>REST의 또 다른 큰 특징 중 하나는 REST API 메시지만 보고도 이를 쉽게 이해 할 수 있는 자체 표현 구조로 되어 있다는 것</p>
</li>
<li><p>Client - Server 구조<br>클라이언트 - 서버 스타일은 사용자 인터페이스에 대한 관심(concern)을 데이터 저장에 대한 관심으로 부터 분리<br>함으로써 클라이언트의 이식성과 서버의 규모 확장성을 개선할 수 있다.</p>
</li>
<li><p>Layered System<br>REST 서버는 다중 계층으로 구성될 수 있으며 보안, 로드 밸런싱, 암호화 계층을 추가해 구조상의 유연성을 둘 수 있고 PROXY, 게이트웨이 같은 네트워크 기반의 중간 매체를 사용할 수 있게 합니다.</p>
</li>
</ol>
<h3 id="REST-API-설계-가이드"><a href="#REST-API-설계-가이드" class="headerlink" title="REST API 설계 가이드"></a>REST API 설계 가이드</h3><p>REST API 설계시 가장 중요한 항목은 다음의 2가지로 요약 할 수 있다.<br>첫 번째, URI는 정보의 자원을 표현해야 한다.<br>두 번째, 자원에 대한 행위는 HTTP Method (GET, POST, PUT, DELETE)로 표현한다.</p>
<ol>
<li>URI는 정보의 자원을 표현해야 한다.</li>
<li>자원에 대한 행위는 HTTP Method (GET, POST, PUT, DELETE)로 표현 한다.</li>
<li>URI에 HTTP Method가 들어가면 안된다.</li>
<li>URI에 행위에 대한 동사 표현이 들어가면 안된다.</li>
<li>경로 부분 중 변하는 부분은 유일한 값으로 대체 한다.</li>
<li>슬래시 구분자(/)는 계층 관계를 나타내는데 사용한다.</li>
<li>URI 마지막 문제로 (/)를 포함하지 않는다.</li>
<li>URI에 포함되는 모든 글자는 리소스의 유일한 식별자로 사용되어야 하며,<br>URI가 다르다는 것은 리소스가 다르다는 것이고, 역으로 리소스가 다르면 URI도 달라져야 한다.</li>
<li>하이픈(-)은 URI 가독성을 높이는데 사용할 수 있다.</li>
<li>under score는 URI에 사용하지 않는다.</li>
<li>URI 경로에는 소문자가 적합하다.</li>
<li>파일 확장자는 URI에 포함하지 않는다 Accept header를 사용하도록 한다.</li>
<li>리소스 간에 연관 관계가 있는 경우 다음과 같은 방법으로 표현 한다.<br>ex) GET: /books/{bookid}/viewers (일반적으로 소유 ‘has’ 관계를 표현할 때)</li>
<li>자원을 표현하는 Collection과 Document<br>Collection은 객체의 집합, Document는 객체라고 생각하면 된다. Collection과 Document모두 리소스로 표현 할 수 있으며 URI로 표현 할 수 있다.</li>
</ol>
<p>-> REST API를 완벽하게 구현하지 못할 경우 Web API라 한다.</p>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article itemscope itemtype="http://schema.org/Article" class="post-block" lang="en">
<link itemprop="mainEntityOfPage" href="https://namhogim.github.io/2020/09/09/Java-collection-type/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/Tiffany_Blue.png">
<meta itemprop="name" content="Namho Kim">
<meta itemprop="description" content="">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="A Software Guy Blog">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">
<a href="/2020/09/09/Java-collection-type/" class="post-title-link" itemprop="url">Java Collections Framework</a>
</h2>
<div class="post-meta-container">
<div class="post-meta">
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar"></i>
</span>
<span class="post-meta-item-text">Posted on</span>
<time title="Created: 2020-09-09 21:46:14" itemprop="dateCreated datePublished" datetime="2020-09-09T21:46:14+09:00">2020-09-09</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-folder"></i>
</span>
<span class="post-meta-item-text">In</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/Java/" itemprop="url" rel="index"><span itemprop="name">Java</span></a>
</span>
,
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/Java/collections/" itemprop="url" rel="index"><span itemprop="name">collections</span></a>
</span>
</span>
</div>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h2 id="Java-Collections-Framework"><a href="#Java-Collections-Framework" class="headerlink" title="Java Collections Framework"></a>Java Collections Framework</h2><ul>
<li><code>java.utils</code>에 속한 일련의 클래스로, 자료구조를 담당</li>
<li>잘 짜여진 <code>interface</code>를 기반으로 다양한 자료구조를 구현</li>
<li>Generic class로 되어 있어, 다양한 객체를 요소로 담을 수 있다.</li>
</ul>
<blockquote>
<p>some are excerpted from <a target="_blank" rel="noopener" href="https://docs.oracle.com/javase/8/docs/technotes/guides/collections/overview.html">Java collections overview</a></p>
</blockquote>
<h2 id="Advantages-of-a-collections-framework"><a href="#Advantages-of-a-collections-framework" class="headerlink" title="Advantages of a collections framework"></a>Advantages of a collections framework</h2><ul>
<li>Reduces programming effort</li>
<li>Increase performance</li>
<li>Provide interoperability between unrelated APIs</li>
<li>Reduces the effort required to learn APIs</li>
<li>Reduces the effort required to design and implement APIs</li>
<li>Fosters software reuse</li>
</ul>
<h2 id="Collection-Interfaces"><a href="#Collection-Interfaces" class="headerlink" title="Collection Interfaces"></a>Collection Interfaces</h2><ul>
<li><a target="_blank" rel="noopener" href="https://docs.oracle.com/javase/8/docs/api/java/util/Collection.html">Collection</a><ul>
<li>A group of objects. No assumptions are made about the order of the collection (if any) or whether it can contain duplicate elements.</li>
</ul>
</li>
<li><a target="_blank" rel="noopener" href="https://docs.oracle.com/javase/8/docs/api/java/util/Set.html">Set</a><ul>
<li>The familiar set abstraction. No duplicate elements permitted. May or may not be ordered. Extends the Collection interface.</li>
</ul>
</li>
<li><a target="_blank" rel="noopener" href="https://docs.oracle.com/javase/8/docs/api/java/util/List.html">List</a><ul>
<li>Ordered collection, also known as a sequence. Duplicates are generally permitted. Allows positional access. Extends the Collection interface.</li>
</ul>
</li>
<li><a target="_blank" rel="noopener" href="https://docs.oracle.com/javase/8/docs/api/java/util/Queue.html">Queue</a><ul>
<li>A collection designed for holding elements before processing. Besides basic Collection operations, queues provide additional insertion, extraction, and inspection operations.</li>
</ul>
</li>
<li><a target="_blank" rel="noopener" href="https://docs.oracle.com/javase/8/docs/api/java/util/Deque.html">Deque</a><ul>
<li>A double ended queue, supporting element insertion and removal at both ends. Extends the Queue interface.</li>
</ul>
</li>
<li><a target="_blank" rel="noopener" href="https://docs.oracle.com/javase/8/docs/api/java/util/Map.html">Map</a><ul>
<li>A mapping from keys to values. Each key can map to one value.</li>
</ul>
</li>
</ul>
<h2 id="Method-of-Collection-Interfaces"><a href="#Method-of-Collection-Interfaces" class="headerlink" title="Method of Collection Interfaces"></a>Method of Collection Interfaces</h2><table>
<thead>
<tr>
<th align="left">Method</th>
<th align="left">Description</th>
</tr>
</thead>
<tbody><tr>
<td align="left"><code>boolean add(E e)</code></td>
<td align="left">Ensures that collection contains the specified element.</td>
</tr>
<tr>
<td align="left"><code>boolean addAll(Collection<? exteionds E> c)</code></td>
<td align="left">Adds all of the elements in the specified collection to this collection.</td>
</tr>
<tr>
<td align="left"><code>void clear()</code></td>
<td align="left">Removes all of the elements from this collection.</td>
</tr>
<tr>
<td align="left"><code>boolean contains(Object o)</code></td>
<td align="left">Returns <code>true</code> if this collection contains the specified element.</td>
</tr>
<tr>
<td align="left"><code>boolean containsAll(Collection<?> c)</code></td>
<td align="left">Returns <code>true</code> if this collection contains all of the elements in the specified collection.</td>
</tr>
<tr>
<td align="left"><code>boolean equals(Object o)</code></td>
<td align="left">Compares the specified object with this collection for equality.</td>
</tr>
<tr>
<td align="left"><code>boolean isEmpty()</code></td>
<td align="left">Returns <code>true</code> if this collection contains no elements.</td>
</tr>
<tr>
<td align="left"><code>Iterator<E> iterator()</code></td>
<td align="left">Returns <code>true</code> if this collection contains on elements.</td>
</tr>
<tr>
<td align="left"><code>boolean remove(Object o)</code></td>
<td align="left">Remove a single instance of the specified elemtns from this collection, if it is present.</td>
</tr>
<tr>
<td align="left"><code>boolean removeAll(Collection<?> c)</code></td>
<td align="left">Removes all of this collection’s elements that are also contained in the specified collection.</td>
</tr>
<tr>
<td align="left"><code>boolean retainAll(Collection<?> c)</code></td>
<td align="left">Retains only the elements in this collection that are contained in the specified collection.</td>
</tr>
<tr>
<td align="left"><code>int size()</code></td>
<td align="left">Returns the number of elements in this collection.</td>
</tr>
<tr>
<td align="left"><code>T[] toArray(T[] a)</code></td>
<td align="left">Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array.</td>
</tr>
</tbody></table>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>