├── .gitignore
├── .idea
├── compiler.xml
├── encodings.xml
├── kotlinc.xml
├── libraries
│ ├── Maven__junit_junit_4_12.xml
│ └── Maven__org_hamcrest_hamcrest_core_1_3.xml
├── misc.xml
├── modules.xml
├── vcs.xml
└── workspace.xml
├── README.md
├── java8.iml
├── pom.xml
└── src
├── main
└── java
│ └── com
│ └── caysever
│ └── java8
│ ├── App.java
│ ├── base64
│ └── PersonEncode.java
│ ├── collections
│ └── Sort.java
│ ├── concurrency
│ ├── CallableExample.java
│ ├── ExecutorsExample.java
│ ├── FutureTimeoutsExample.java
│ ├── MultipleCallablesExample.java
│ ├── ScheduledExecutorsExample.java
│ └── ScheduledFixedRateExecutorsExample.java
│ ├── datetime
│ ├── DateTimeExample.java
│ ├── InstantExample.java
│ └── ZonedTimeExample.java
│ ├── func
│ └── interfaces
│ │ ├── CallableFunc.java
│ │ ├── ConsumerFunc.java
│ │ ├── FunctionFunc.java
│ │ └── PredicateFunc.java
│ ├── lambda
│ └── MathOperation.java
│ ├── method
│ └── reference
│ │ └── MethodReference.java
│ ├── model
│ └── Person.java
│ ├── optional
│ └── OptioanlInterfaceExample.java
│ ├── scripting
│ └── NashornEngineExample.java
│ ├── stream
│ ├── CollectExample.java
│ ├── FilterExample.java
│ ├── LimitExample.java
│ ├── SortExample.java
│ └── StatisticsExample.java
│ └── utils
│ └── PersonUtil.java
└── test
└── java
└── com
└── caysever
└── java8
└── AppTest.java
/.gitignore:
--------------------------------------------------------------------------------
1 | target/
2 | pom.xml.tag
3 | pom.xml.releaseBackup
4 | pom.xml.versionsBackup
5 | pom.xml.next
6 | release.properties
7 | dependency-reduced-pom.xml
8 | buildNumber.properties
9 | .mvn/timing.properties
10 | .classpath
11 | .project
12 | .settings/
13 |
14 |
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
--------------------------------------------------------------------------------
/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/kotlinc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/.idea/libraries/Maven__junit_junit_4_12.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 | 1.8
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/workspace.xml:
--------------------------------------------------------------------------------
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 | true
119 | DEFINITION_ORDER
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 | project
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 | 1495206919382
784 |
785 |
786 | 1495206919382
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 |
1001 |
1002 |
1003 |
1004 |
1005 |
1006 |
1007 |
1008 |
1009 |
1010 |
1011 |
1012 |
1013 |
1014 |
1015 |
1016 |
1017 |
1018 |
1019 |
1020 |
1021 |
1022 |
1023 |
1024 |
1025 |
1026 |
1027 |
1028 |
1029 |
1030 |
1031 |
1032 |
1033 |
1034 |
1035 |
1036 |
1037 |
1038 |
1039 |
1040 |
1041 |
1042 |
1043 |
1044 |
1045 |
1046 |
1047 |
1048 |
1049 |
1050 |
1051 |
1052 |
1053 |
1054 |
1055 |
1056 |
1057 |
1058 |
1059 |
1060 |
1061 |
1062 |
1063 |
1064 |
1065 |
1066 |
1067 |
1068 |
1069 |
1070 |
1071 |
1072 |
1073 |
1074 |
1075 |
1076 |
1077 |
1078 |
1079 |
1080 |
1081 |
1082 |
1083 |
1084 |
1085 |
1086 |
1087 |
1088 |
1089 |
1090 |
1091 |
1092 |
1093 |
1094 |
1095 |
1096 |
1097 |
1098 |
1099 |
1100 |
1101 |
1102 |
1103 |
1104 |
1105 |
1106 |
1107 |
1108 |
1109 |
1110 |
1111 |
1112 |
1113 |
1114 |
1115 |
1116 |
1117 |
1118 |
1119 |
1120 |
1121 |
1122 |
1123 |
1124 |
1125 |
1126 |
1127 |
1128 |
1129 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # java8
2 | contains a several samples for java 8
3 |
--------------------------------------------------------------------------------
/java8.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
3 | 4.0.0
4 |
5 | com.caysever
6 | java8
7 | 0.0.1
8 | jar
9 |
10 | java8
11 | http://maven.apache.org
12 |
13 |
14 | UTF-8
15 | 1.8
16 | 1.8
17 |
18 |
19 |
20 |
21 | junit
22 | junit
23 | 4.12
24 | test
25 |
26 |
27 |
28 |
--------------------------------------------------------------------------------
/src/main/java/com/caysever/java8/App.java:
--------------------------------------------------------------------------------
1 | package com.caysever.java8;
2 |
3 | /**
4 | * Hello world!
5 | */
6 | public class App {
7 | public static void main(String[] args) {
8 | System.out.println("Java8 Tutorials!");
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/src/main/java/com/caysever/java8/base64/PersonEncode.java:
--------------------------------------------------------------------------------
1 | package com.caysever.java8.base64;
2 |
3 | import com.caysever.java8.model.Person;
4 | import com.caysever.java8.utils.PersonUtil;
5 |
6 | import java.util.Base64;
7 | import java.util.List;
8 | import java.util.function.Consumer;
9 | import java.util.function.Function;
10 |
11 | public class PersonEncode {
12 |
13 | public static void main(String[] args) {
14 | List persons = PersonUtil.mockPersons();
15 |
16 | System.err.println("Printing encoded person as list;");
17 | process(persons, (p) -> {
18 | p.setFirstname(Base64.getEncoder().encodeToString(p.getFirstname().getBytes()));
19 | return p;
20 | }, (p) -> System.out.println("Encoded firstname attr : " + p));
21 |
22 | System.err.println("Printing decoded person as list;");
23 | process(persons, (p) -> {
24 | p.setFirstname(new String(Base64.getDecoder().decode(p.getFirstname().getBytes())));
25 | return p;
26 | }, (p) -> System.out.println("Decoded firstname attr : " + p));
27 |
28 | }
29 |
30 | private static void process(List persons, Function function, Consumer consumer) {
31 | for (Person p : persons) {
32 | p = function.apply(p);
33 | consumer.accept(p);
34 | }
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/src/main/java/com/caysever/java8/collections/Sort.java:
--------------------------------------------------------------------------------
1 | package com.caysever.java8.collections;
2 |
3 | import com.caysever.java8.model.Person;
4 | import com.caysever.java8.utils.PersonUtil;
5 |
6 | import java.util.Arrays;
7 | import java.util.Collections;
8 | import java.util.Comparator;
9 | import java.util.List;
10 |
11 | public class Sort {
12 |
13 | public static void main(String[] args) {
14 | Sort sort = new Sort();
15 |
16 | List persons = PersonUtil.mockPersons();
17 |
18 | sort.sortUsingJava7(persons);
19 | System.out.println(Arrays.toString(persons.toArray()));
20 |
21 | sort.sortUsingJava8(persons);
22 | System.out.println(Arrays.toString(persons.toArray()));
23 |
24 | }
25 |
26 | // sort using java 7
27 | private void sortUsingJava7(List persons) {
28 | Collections.sort(persons, new Comparator() {
29 | @Override
30 | public int compare(Person p1, Person p2) {
31 | return p1.getFirstname().compareTo(p2.getFirstname());
32 | }
33 | });
34 | }
35 |
36 | // sort using java 8
37 | private void sortUsingJava8(List persons) {
38 | Collections.sort(persons, (p1, p2) -> p1.getFirstname().compareTo(p2.getFirstname()));
39 | }
40 |
41 | }
42 |
--------------------------------------------------------------------------------
/src/main/java/com/caysever/java8/concurrency/CallableExample.java:
--------------------------------------------------------------------------------
1 | package com.caysever.java8.concurrency;
2 |
3 | import com.caysever.java8.model.Person;
4 | import com.caysever.java8.utils.PersonUtil;
5 |
6 | import java.util.List;
7 | import java.util.concurrent.*;
8 | import java.util.stream.Collectors;
9 |
10 | /**
11 | * @author alican
12 | */
13 | public class CallableExample {
14 |
15 | public static void main(String[] args) {
16 |
17 | try {
18 | List persons = PersonUtil.mockPersons();
19 |
20 | //Callables are functional interfaces just like runnables but instead of being void they return a value.
21 | Callable> personsSortingAndFilteringTask = () -> {
22 | try {
23 | TimeUnit.SECONDS.sleep(3);
24 | return persons.stream().sorted((p1, p2) -> p1.getBirthday().compareTo(p2.getBirthday())).filter((p) -> p.getFirstname() != null)
25 | .collect(Collectors.toList());
26 | } catch (InterruptedException e) {
27 | throw new IllegalStateException("task interrupted", e);
28 | }
29 | };
30 |
31 | ExecutorService executor = Executors.newFixedThreadPool(1);
32 | Future> future = executor.submit(personsSortingAndFilteringTask);
33 |
34 | //executor.shutdownNow(); dont call this method in this point. executor and callable-future is tightly coupled.
35 |
36 | System.out.println("future done? " + future.isDone());
37 |
38 | List resultOfPersonsTask = future.get();
39 | executor.shutdownNow();// we can shutdown now
40 |
41 | System.out.println("future done? " + future.isDone());
42 |
43 | resultOfPersonsTask.forEach(System.out::println);
44 |
45 | } catch (Exception e) {
46 | e.printStackTrace();
47 | }
48 | }
49 |
50 | }
51 |
--------------------------------------------------------------------------------
/src/main/java/com/caysever/java8/concurrency/ExecutorsExample.java:
--------------------------------------------------------------------------------
1 | package com.caysever.java8.concurrency;
2 |
3 | import com.caysever.java8.model.Person;
4 | import com.caysever.java8.utils.PersonUtil;
5 |
6 | import java.util.List;
7 | import java.util.concurrent.ExecutorService;
8 | import java.util.concurrent.Executors;
9 | import java.util.concurrent.TimeUnit;
10 |
11 | /**
12 | * @author alican
13 | */
14 | public class ExecutorsExample {
15 |
16 | public static void main(String[] args) {
17 |
18 | try {
19 | List persons = PersonUtil.mockPersons();
20 |
21 | ExecutorService executor = Executors.newSingleThreadExecutor();
22 | executor.submit(() -> {
23 | String threadName = Thread.currentThread().getName();// pool-1-thread-1
24 | persons.forEach((p) -> System.out.println(threadName + " | " + p));
25 | });
26 |
27 | executor.submit(() -> {
28 | String threadName = Thread.currentThread().getName();// pool-1-thread-1
29 | persons.forEach((p) -> System.err.println(threadName + " | " + p));
30 | });
31 |
32 | // the java process never stops!
33 |
34 | // executor.shutdown();// Executors have to be stopped explicitly -
35 | // waits
36 | // for currently running tasks to finish
37 |
38 | // executor.shutdownNow();// interrupts all running tasks and shut
39 | // the executor down immediately.
40 |
41 | // or you can this;
42 | System.out.println("attempt to shutdown executor in 15 seconds");
43 | executor.awaitTermination(15, TimeUnit.SECONDS);
44 |
45 | } catch (InterruptedException e) {
46 | e.printStackTrace();
47 | }
48 | }
49 |
50 | }
51 |
--------------------------------------------------------------------------------
/src/main/java/com/caysever/java8/concurrency/FutureTimeoutsExample.java:
--------------------------------------------------------------------------------
1 | package com.caysever.java8.concurrency;
2 |
3 | import com.caysever.java8.model.Person;
4 | import com.caysever.java8.utils.PersonUtil;
5 |
6 | import java.util.List;
7 | import java.util.concurrent.*;
8 | import java.util.stream.Collectors;
9 |
10 | /**
11 | * @author alican
12 | */
13 | public class FutureTimeoutsExample {
14 |
15 | public static void main(String[] args) {
16 |
17 | try {
18 | List persons = PersonUtil.mockPersons();
19 |
20 | //Callables are functional interfaces just like runnables but instead of being void they return a value.
21 | Callable> personsSortingAndFilteringTask = () -> {
22 | try {
23 | TimeUnit.SECONDS.sleep(3);
24 | return persons.stream().sorted((p1, p2) -> p1.getBirthday().compareTo(p2.getBirthday())).filter((p) -> p.getFirstname() != null)
25 | .collect(Collectors.toList());
26 | } catch (InterruptedException e) {
27 | throw new IllegalStateException("task interrupted", e);
28 | }
29 | };
30 |
31 | ExecutorService executor = Executors.newFixedThreadPool(1);
32 | Future> future = executor.submit(personsSortingAndFilteringTask);
33 |
34 |
35 | try {
36 | List resultOfPersonsTask = future.get(1, TimeUnit.SECONDS);//we should getting timeout exception
37 | resultOfPersonsTask.forEach(System.out::println);
38 | } catch (TimeoutException te) {
39 | te.printStackTrace();
40 | //java.util.concurrent.TimeoutException
41 | // at java.util.concurrent.FutureTask.get(FutureTask.java:205)
42 | // at
43 | // com.caysever.java8.concurrency.FutureTimeoutsExample.main(FutureTimeoutsExample.java:43)
44 |
45 | } finally {
46 | executor.shutdownNow();
47 | }
48 |
49 |
50 | } catch (Exception e) {
51 | e.printStackTrace();
52 | }
53 | }
54 |
55 | }
56 |
--------------------------------------------------------------------------------
/src/main/java/com/caysever/java8/concurrency/MultipleCallablesExample.java:
--------------------------------------------------------------------------------
1 | package com.caysever.java8.concurrency;
2 |
3 | import com.caysever.java8.model.Person;
4 | import com.caysever.java8.utils.PersonUtil;
5 |
6 | import java.util.Arrays;
7 | import java.util.List;
8 | import java.util.concurrent.Callable;
9 | import java.util.concurrent.ExecutorService;
10 | import java.util.concurrent.Executors;
11 | import java.util.stream.Collectors;
12 |
13 | /**
14 | * @author alican
15 | */
16 | public class MultipleCallablesExample {
17 |
18 | public static void main(String[] args) {
19 |
20 | try {
21 | List persons = PersonUtil.mockPersons();
22 |
23 | //create callable list which return sorted persons
24 | List>> callables = Arrays.asList(() -> {
25 | System.out.println("Persons sorting by birthday..");
26 | return persons.stream().sorted((p1, p2) -> p1.getBirthday().compareTo(p2.getBirthday())).collect(Collectors.toList());
27 | }, () -> {
28 | System.out.println("Persons sorting by firstname..");
29 | return persons.stream().sorted((p1, p2) -> p1.getFirstname().compareTo(p2.getFirstname())).collect(Collectors.toList());
30 | }, () -> {
31 | System.out.println("Persons sorting by lastname..");
32 | return persons.stream().sorted((p1, p2) -> p1.getLastname().compareTo(p2.getLastname())).collect(Collectors.toList());
33 | });
34 |
35 | //create work stealing pool for more than one tasks
36 | ExecutorService executor = Executors.newWorkStealingPool();
37 |
38 | executor.invokeAll(callables).stream().map(future -> {
39 | try {
40 | return future.get();
41 | } catch (Exception e) {
42 | throw new IllegalStateException(e);
43 | }
44 | }).forEach(System.out::println);
45 |
46 | //executor.invokeAny(callables).forEach(System.out::println); // call the invokeAny function for returns the string result of the fastest callable
47 |
48 | } catch (Exception e) {
49 | e.printStackTrace();
50 | }
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/src/main/java/com/caysever/java8/concurrency/ScheduledExecutorsExample.java:
--------------------------------------------------------------------------------
1 | package com.caysever.java8.concurrency;
2 |
3 | import java.time.LocalDateTime;
4 | import java.util.concurrent.Executors;
5 | import java.util.concurrent.ScheduledExecutorService;
6 | import java.util.concurrent.TimeUnit;
7 |
8 | /**
9 | * @author alican
10 | */
11 | public class ScheduledExecutorsExample {
12 |
13 | public static void main(String[] args) {
14 |
15 | try {
16 | ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
17 |
18 | //print time with every second
19 | Runnable task = () -> {
20 | System.out.println("Current date time: " + LocalDateTime.now());
21 | };
22 |
23 | executor.scheduleAtFixedRate(task, 0, 1, TimeUnit.SECONDS);//0: initial delay, 1: period
24 |
25 | } catch (Exception e) {
26 | e.printStackTrace();
27 | }
28 | }
29 |
30 | }
31 |
--------------------------------------------------------------------------------
/src/main/java/com/caysever/java8/concurrency/ScheduledFixedRateExecutorsExample.java:
--------------------------------------------------------------------------------
1 | package com.caysever.java8.concurrency;
2 |
3 | import com.caysever.java8.model.Person;
4 | import com.caysever.java8.utils.PersonUtil;
5 |
6 | import java.util.List;
7 | import java.util.concurrent.Executors;
8 | import java.util.concurrent.ScheduledExecutorService;
9 | import java.util.concurrent.ScheduledFuture;
10 | import java.util.concurrent.TimeUnit;
11 |
12 | /**
13 | * @author alican
14 | */
15 | public class ScheduledFixedRateExecutorsExample {
16 |
17 | public static void main(String[] args) {
18 |
19 | try {
20 | List persons = PersonUtil.mockPersons();
21 |
22 | ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
23 |
24 | Runnable task = () -> {
25 | String threadName = Thread.currentThread().getName();// pool-1-thread-1
26 | persons.forEach((p) -> System.out.println(threadName + " | " + p));
27 | };
28 |
29 | ScheduledFuture> future = executor.schedule(task, 3, TimeUnit.SECONDS);
30 |
31 | long remainingDelay = future.getDelay(TimeUnit.MILLISECONDS);
32 | System.out.printf("Remaining Delay: %sms", remainingDelay);//remaining time almost is 2.99 second
33 | System.out.println();
34 |
35 | executor.shutdown();
36 |
37 | } catch (Exception e) {
38 | e.printStackTrace();
39 | }
40 | }
41 |
42 | }
43 |
--------------------------------------------------------------------------------
/src/main/java/com/caysever/java8/datetime/DateTimeExample.java:
--------------------------------------------------------------------------------
1 | package com.caysever.java8.datetime;
2 |
3 | import java.time.LocalDate;
4 | import java.time.LocalDateTime;
5 | import java.time.LocalTime;
6 | import java.time.Month;
7 |
8 | public class DateTimeExample {
9 |
10 | public static void main(String[] args) {
11 |
12 | // Get the current date and time
13 | LocalDateTime currentTime = LocalDateTime.now();
14 | System.out.println("Current DateTime: " + currentTime);
15 |
16 | LocalDate date1 = currentTime.toLocalDate();
17 | System.out.println("date1: " + date1);
18 |
19 | Month month = currentTime.getMonth();
20 | int day = currentTime.getDayOfMonth();
21 | int seconds = currentTime.getSecond();
22 |
23 | System.out.println("Month: " + month + " day: " + day + " seconds: " + seconds);
24 |
25 | LocalDateTime graduatedDate = currentTime.withMonth(6).withDayOfMonth(15).withYear(2016);
26 | System.out.println("graduatedDate: " + graduatedDate);
27 |
28 | // 6 march 2017
29 | LocalDate armBrokedDate = LocalDate.of(2017, Month.MARCH, 6);
30 | System.out.println("armBrokedDate: " + armBrokedDate);
31 |
32 | // 22 hour 15 minutes
33 | LocalTime sampleTime = LocalTime.of(22, 15);
34 | System.out.println("sampleTime: " + sampleTime);
35 |
36 | // parse a string
37 | LocalTime sampleTimePaarsing = LocalTime.parse("22:15:30");
38 | System.out.println("sampleTimePaarsing: " + sampleTimePaarsing);
39 |
40 | }
41 |
42 | }
43 |
--------------------------------------------------------------------------------
/src/main/java/com/caysever/java8/datetime/InstantExample.java:
--------------------------------------------------------------------------------
1 | package com.caysever.java8.datetime;
2 |
3 | import com.caysever.java8.model.Person;
4 | import com.caysever.java8.utils.PersonUtil;
5 |
6 | import java.time.Instant;
7 | import java.util.List;
8 | import java.util.Optional;
9 |
10 | public class InstantExample {
11 |
12 | public static void main(String[] args) {
13 | List persons = PersonUtil.mockPersons();
14 |
15 | Optional firstPerson = Optional.of(persons.get(0));
16 |
17 | Instant instantBirthDay = Instant.ofEpochMilli(firstPerson.get().getBirthday());
18 | System.out.println("First Person birthday : " + instantBirthDay);
19 | }
20 |
21 | }
22 |
--------------------------------------------------------------------------------
/src/main/java/com/caysever/java8/datetime/ZonedTimeExample.java:
--------------------------------------------------------------------------------
1 | package com.caysever.java8.datetime;
2 |
3 | import java.time.ZoneId;
4 | import java.time.ZonedDateTime;
5 |
6 | public class ZonedTimeExample {
7 |
8 | public static void main(String[] args) {
9 |
10 | // Get the current date and time
11 | ZonedDateTime islambolTime = ZonedDateTime.parse("2017-06-20T10:15:30+03:30[Europe/Istanbul]");
12 | System.out.println("islambolTime: " + islambolTime);
13 |
14 | ZoneId id = ZoneId.of("Europe/Istanbul");
15 | System.out.println("ZoneId: " + id);
16 |
17 | ZoneId currentZone = ZoneId.systemDefault();
18 | System.out.println("CurrentZone: " + currentZone);
19 |
20 | }
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/src/main/java/com/caysever/java8/func/interfaces/CallableFunc.java:
--------------------------------------------------------------------------------
1 | package com.caysever.java8.func.interfaces;
2 |
3 | import com.caysever.java8.model.Person;
4 | import com.caysever.java8.utils.PersonUtil;
5 |
6 | import java.util.List;
7 | import java.util.concurrent.Callable;
8 | import java.util.stream.Collectors;
9 |
10 | public class CallableFunc {
11 |
12 | public static void main(String[] args) throws Exception {
13 |
14 | Callable> callableForPersonAgeGreaterThan22 = () -> PersonUtil.mockPersons().stream().filter(p -> p.getAge() > 22).collect(Collectors.toList());
15 |
16 | List personList = callableForPersonAgeGreaterThan22.call();
17 |
18 | personList.stream().forEach(System.out::println);
19 |
20 | }
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/src/main/java/com/caysever/java8/func/interfaces/ConsumerFunc.java:
--------------------------------------------------------------------------------
1 | package com.caysever.java8.func.interfaces;
2 |
3 | import com.caysever.java8.model.Person;
4 | import com.caysever.java8.utils.PersonUtil;
5 |
6 | import java.util.List;
7 | import java.util.function.Consumer;
8 | import java.util.function.Predicate;
9 |
10 | public class ConsumerFunc {
11 |
12 | public static void main(String[] args) {
13 |
14 | List persons = PersonUtil.mockPersons();
15 |
16 | // Predicate predicate = p -> true
17 | // p is passed as parameter to test method of Predicate interface
18 | // test method will always return true no matter what value p has.
19 |
20 | System.out.println("Print all persons to system out stream:");
21 |
22 | // pass p as parameter
23 | process(persons, p -> true, System.out::println);
24 |
25 | // Predicate predicate1 = p ->
26 | // p.getGender().equalsIgnoreCase("male")
27 | // p is passed as parameter to test method of Predicate interface
28 | // test method will return true if person has gender as male
29 |
30 | System.out.println("Print male persons to system err stream:");
31 | process(persons, p -> p.getGender().equalsIgnoreCase("male"), System.err::println);
32 |
33 | // Predicate predicate2 = p -> p.getFirstname().startsWith("a")
34 | // n is passed as parameter to test method of Predicate interface
35 | // test method will return true if p.firstname start with 'a'
36 |
37 | System.out.println("Print persons beginning with 'a' to system out stream:");
38 | process(persons, p -> p.getFirstname().startsWith("a"), System.out::println);
39 | }
40 |
41 | private static void process(List persons, Predicate predicate, Consumer consume) {
42 | for (Person p : persons) {
43 | if (predicate.test(p)) {
44 | consume.accept(p);
45 | }
46 | }
47 | }
48 |
49 | }
50 |
--------------------------------------------------------------------------------
/src/main/java/com/caysever/java8/func/interfaces/FunctionFunc.java:
--------------------------------------------------------------------------------
1 | package com.caysever.java8.func.interfaces;
2 |
3 | import com.caysever.java8.model.Person;
4 | import com.caysever.java8.utils.PersonUtil;
5 |
6 | import java.util.List;
7 | import java.util.function.Function;
8 |
9 | public class FunctionFunc {
10 |
11 | public static void main(String[] args) {
12 |
13 | List persons = PersonUtil.mockPersons();
14 |
15 | System.out.println("Print all person with firsname upper case:");
16 | // pass p as parameter, all firstname will be upper case
17 | process(persons, (p) -> {
18 | p.setFirstname(p.getFirstname().toUpperCase());
19 | return p;
20 | });
21 |
22 | System.out.println("Print all person with lastname upper case:");
23 | process(persons, (p) -> {
24 | p.setLastname(p.getLastname().toUpperCase());
25 | return p;
26 | });
27 |
28 | }
29 |
30 | private static void process(List persons, Function function) {
31 | for (Person p : persons) {
32 | function.apply(p);
33 | System.out.println(p);
34 | }
35 | }
36 |
37 | }
38 |
--------------------------------------------------------------------------------
/src/main/java/com/caysever/java8/func/interfaces/PredicateFunc.java:
--------------------------------------------------------------------------------
1 | package com.caysever.java8.func.interfaces;
2 |
3 | import com.caysever.java8.model.Person;
4 | import com.caysever.java8.utils.PersonUtil;
5 |
6 | import java.util.List;
7 | import java.util.function.Predicate;
8 |
9 | public class PredicateFunc {
10 |
11 | public static void main(String[] args) {
12 |
13 | List persons = PersonUtil.mockPersons();
14 |
15 | // Predicate predicate = p -> true
16 | // p is passed as parameter to test method of Predicate interface
17 | // test method will always return true no matter what value p has.
18 |
19 | System.out.println("Print all persons:");
20 |
21 | // pass p as parameter
22 | process(persons, p -> true);
23 |
24 | // Predicate predicate1 = p -> p.getGender().equalsIgnoreCase("male")
25 | // p is passed as parameter to test method of Predicate interface
26 | // test method will return true if person has gender as male
27 |
28 | System.out.println("Print male persons:");
29 | process(persons, p -> p.getGender().equalsIgnoreCase("male"));
30 |
31 | // Predicate predicate2 = p -> p.getFirstname().startsWith("a")
32 | // n is passed as parameter to test method of Predicate interface
33 | // test method will return true if p.firstname start with 'a'
34 |
35 | System.out.println("Print persons beginning with 'a':");
36 | process(persons, p -> p.getFirstname().startsWith("a"));
37 | }
38 |
39 | private static void process(List persons, Predicate predicate) {
40 | for (Person p : persons) {
41 | if (predicate.test(p)) {
42 | System.out.println(p);
43 | }
44 | }
45 | }
46 |
47 | }
48 |
--------------------------------------------------------------------------------
/src/main/java/com/caysever/java8/lambda/MathOperation.java:
--------------------------------------------------------------------------------
1 | package com.caysever.java8.lambda;
2 |
3 | public class MathOperation {
4 |
5 | public static void main(String args[]) {
6 | MathOperation mathOperation = new MathOperation();
7 |
8 | // with type declaration
9 | IMathOperation addition = (int a, int b) -> a + b;
10 |
11 | // with out type declaration
12 | IMathOperation subtraction = (a, b) -> a - b;
13 |
14 | // with return statement along with curly braces
15 | IMathOperation multiplication = (int a, int b) -> {
16 | return a * b;
17 | };
18 |
19 | // without return statement and without curly braces
20 | IMathOperation division = (int a, int b) -> a / b;
21 |
22 | System.out.println("10 + 5 = " + mathOperation.operate(10, 5, addition));
23 | System.out.println("10 - 5 = " + mathOperation.operate(10, 5, subtraction));
24 | System.out.println("10 x 5 = " + mathOperation.operate(10, 5, multiplication));
25 | System.out.println("10 / 5 = " + mathOperation.operate(10, 5, division));
26 |
27 | // with parenthesis
28 | IGreetingService greetServiceForEnglishUser = message -> System.out.println("Hello " + message);
29 |
30 | // without parenthesis
31 | IGreetingService greetServiceForTurkishUser = (message) -> System.out.println("Merhaba " + message);
32 |
33 | greetServiceForEnglishUser.sayMessage("Mahesh");
34 | greetServiceForTurkishUser.sayMessage("Alican");
35 | }
36 |
37 | // functional interface, have a single method
38 | interface IMathOperation {
39 | int operation(int a, int b);
40 | }
41 |
42 | // functional interface, have a single method
43 | interface IGreetingService {
44 | void sayMessage(String message);
45 | }
46 |
47 | private int operate(int a, int b, IMathOperation mathOperation) {
48 | return mathOperation.operation(a, b);
49 | }
50 | }
--------------------------------------------------------------------------------
/src/main/java/com/caysever/java8/method/reference/MethodReference.java:
--------------------------------------------------------------------------------
1 | package com.caysever.java8.method.reference;
2 |
3 | import com.caysever.java8.model.Person;
4 | import com.caysever.java8.utils.PersonUtil;
5 |
6 | import java.util.List;
7 | import java.util.function.Consumer;
8 |
9 | public class MethodReference {
10 |
11 | public static void main(String[] args) {
12 |
13 | List persons = PersonUtil.mockPersons();
14 |
15 | process(persons, System.out::println);//print to system out , method reference
16 | process(persons, System.err::println);//print to system err , method reference
17 | }
18 |
19 | private static void process(List persons, Consumer consumer) {
20 |
21 | for (Person person : persons) {
22 | consumer.accept(person);
23 | }
24 |
25 | }
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/src/main/java/com/caysever/java8/model/Person.java:
--------------------------------------------------------------------------------
1 | package com.caysever.java8.model;
2 |
3 | public class Person {
4 | private String firstname;
5 | private String lastname;
6 | private String gender;
7 | private Integer age;
8 | private Long birthday;
9 |
10 | public Person(String firstname, String lastname, String gender, Integer age, Long birthday) {
11 | super();
12 | this.firstname = firstname;
13 | this.lastname = lastname;
14 | this.gender = gender;
15 | this.age = age;
16 | this.birthday = birthday;
17 | }
18 |
19 | public String getFirstname() {
20 | return firstname;
21 | }
22 |
23 | public void setFirstname(String firstname) {
24 | this.firstname = firstname;
25 | }
26 |
27 | public String getLastname() {
28 | return lastname;
29 | }
30 |
31 | public void setLastname(String lastname) {
32 | this.lastname = lastname;
33 | }
34 |
35 | public String getGender() {
36 | return gender;
37 | }
38 |
39 | public void setGender(String gender) {
40 | this.gender = gender;
41 | }
42 |
43 | public Integer getAge() {
44 | return age;
45 | }
46 |
47 | public void setAge(Integer age) {
48 | this.age = age;
49 | }
50 |
51 | public Long getBirthday() {
52 | return birthday;
53 | }
54 |
55 | public void setBirthday(Long birthday) {
56 | this.birthday = birthday;
57 | }
58 |
59 | @Override
60 | public String toString() {
61 | return "Person [firstname=" + firstname + ", lastname=" + lastname + ", gender=" + gender + ", age=" + age + ", birthday=" + birthday + "]";
62 | }
63 |
64 | }
65 |
--------------------------------------------------------------------------------
/src/main/java/com/caysever/java8/optional/OptioanlInterfaceExample.java:
--------------------------------------------------------------------------------
1 | package com.caysever.java8.optional;
2 |
3 | import com.caysever.java8.model.Person;
4 | import com.caysever.java8.utils.PersonUtil;
5 |
6 | import java.util.Date;
7 | import java.util.List;
8 | import java.util.Optional;
9 | import java.util.Random;
10 |
11 | public class OptioanlInterfaceExample {
12 |
13 | public static void main(String[] args) {
14 |
15 | List persons = PersonUtil.mockPersons();
16 |
17 | Optional person = Optional.of(persons.get(0));// persons have a
18 | // more than one
19 | // person. don't
20 | // allows passed
21 | // parameter to be
22 | // null
23 |
24 | // should print person
25 | person.ifPresent(System.out::println);// Person [firstname=alican,
26 | // lastname=akkuş, gender=male,
27 | // age=23, birthday=1489024012864]
28 |
29 | try {
30 | person = Optional.of(null);// throws NullPointerException if passed
31 | // parameter is null
32 | } catch (NullPointerException e) {
33 | System.err.println("An error occured while person creating..");
34 | }
35 |
36 | person = Optional.ofNullable(null);// could't throw any exception in
37 | // this point.
38 | // should't print person
39 | person.ifPresent(System.out::println);// no output
40 |
41 | // Optional.orElse - returns the value if present otherwise returns
42 | // the default value passed.
43 | Person p = person.orElse(new Person("hatun", "hatun", "female", 25, new Date().getTime() + new Random().nextInt()));
44 |
45 | person = Optional.of(p);
46 | // should print person
47 | person.ifPresent(System.out::println);
48 | }
49 |
50 | }
51 |
--------------------------------------------------------------------------------
/src/main/java/com/caysever/java8/scripting/NashornEngineExample.java:
--------------------------------------------------------------------------------
1 | package com.caysever.java8.scripting;
2 |
3 | import com.caysever.java8.model.Person;
4 | import com.caysever.java8.utils.PersonUtil;
5 |
6 | import javax.script.Invocable;
7 | import javax.script.ScriptEngine;
8 | import javax.script.ScriptEngineManager;
9 | import java.util.List;
10 |
11 | public class NashornEngineExample {
12 |
13 | public static void main(String[] args) throws Exception {
14 |
15 | List persons = PersonUtil.mockPersons();
16 |
17 | ScriptEngineManager engineManager = new ScriptEngineManager();
18 | ScriptEngine engine = engineManager.getEngineByName("nashorn");
19 |
20 | engine.eval("var System = Java.type('java.lang.System');"
21 | + " function printPersons(persons){"
22 | + " persons.forEach(function(e){"
23 | + " System.out.println(e);"
24 | + " });"
25 | + " }");
26 |
27 |
28 | Invocable invocable = (Invocable) engine;
29 | invocable.invokeFunction("printPersons", persons);
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/src/main/java/com/caysever/java8/stream/CollectExample.java:
--------------------------------------------------------------------------------
1 | package com.caysever.java8.stream;
2 |
3 | import com.caysever.java8.model.Person;
4 | import com.caysever.java8.utils.PersonUtil;
5 |
6 | import java.util.Comparator;
7 | import java.util.List;
8 | import java.util.Map;
9 | import java.util.Optional;
10 | import java.util.stream.Collectors;
11 |
12 | /**
13 | * @author alican
14 | */
15 | public class CollectExample {
16 |
17 | /**
18 | * @param args
19 | */
20 | public static void main(String[] args) {
21 | List persons = PersonUtil.mockPersons();
22 |
23 | // streaming and pipeling sorting and then system out consuming
24 | // sorted by birthday, result will be different when different time
25 | // limit with 3
26 | // and collect (result of processing on the elements of a stream. )
27 | List sortedAndLimitedPersons = persons.stream().sorted((p1, p2) -> p1.getBirthday().compareTo(p2.getBirthday())).limit(3)
28 | .collect(Collectors.toList());
29 | System.err.println("0. Print sorted and limited persons: ");
30 | sortedAndLimitedPersons.forEach(System.out::println);
31 |
32 | Map> groupByGenderList = persons.stream().collect(Collectors.groupingBy(Person::getGender));
33 | // Group by gender List : Female-> Persons and Male -> Persons
34 | System.err.println("1. Group persons by gender - get result in List: ");
35 | System.out.println(groupByGenderList.toString());
36 |
37 | Map> personByMaxAgeForEachGender = persons.stream().collect(
38 | Collectors.groupingBy(Person::getGender, Collectors.maxBy(Comparator.comparing(Person::getAge))));
39 | System.err.println("2. Group person objects by gender and get person with max age: ");
40 | System.out.println(personByMaxAgeForEachGender.toString());
41 |
42 | }
43 |
44 | }
45 |
--------------------------------------------------------------------------------
/src/main/java/com/caysever/java8/stream/FilterExample.java:
--------------------------------------------------------------------------------
1 | package com.caysever.java8.stream;
2 |
3 | import com.caysever.java8.model.Person;
4 | import com.caysever.java8.utils.PersonUtil;
5 |
6 | import java.util.List;
7 |
8 | /**
9 | * @author alican
10 | */
11 | public class FilterExample {
12 |
13 | /**
14 | * @param args
15 | */
16 | public static void main(String[] args) {
17 | List persons = PersonUtil.mockPersons();
18 |
19 | // streaming and pipeling and then system out consuming
20 | // pring all person begingging with 'a'
21 | persons.stream().filter(p -> p.getFirstname().startsWith("a")).forEach(System.out::println);
22 |
23 | // streaming and pipeling and then system err consuming
24 | // pring all person begingging with 'a'
25 | // hatunları err'de yazdiralim :)
26 | persons.stream().filter(p -> p.getGender().equalsIgnoreCase("female")).forEach(System.err::println);
27 |
28 | }
29 |
30 | }
31 |
--------------------------------------------------------------------------------
/src/main/java/com/caysever/java8/stream/LimitExample.java:
--------------------------------------------------------------------------------
1 | package com.caysever.java8.stream;
2 |
3 | import com.caysever.java8.model.Person;
4 | import com.caysever.java8.utils.PersonUtil;
5 |
6 | import java.util.List;
7 |
8 | /**
9 | * @author alican
10 | */
11 | public class LimitExample {
12 |
13 | /**
14 | * @param args
15 | */
16 | public static void main(String[] args) {
17 | List persons = PersonUtil.mockPersons();
18 |
19 | // streaming and pipeling and then system out consuming
20 | // pring top 2 male person
21 | persons.stream().filter(p -> p.getGender().equalsIgnoreCase("male")).limit(2).forEach(System.out::println);
22 |
23 | }
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/src/main/java/com/caysever/java8/stream/SortExample.java:
--------------------------------------------------------------------------------
1 | package com.caysever.java8.stream;
2 |
3 | import com.caysever.java8.model.Person;
4 | import com.caysever.java8.utils.PersonUtil;
5 |
6 | import java.util.List;
7 |
8 | /**
9 | * @author alican
10 | */
11 | public class SortExample {
12 |
13 | /**
14 | * @param args
15 | */
16 | public static void main(String[] args) {
17 | List persons = PersonUtil.mockPersons();
18 |
19 | // streaming and pipeling sorting and then system out consuming
20 | // sorted by birthday, result will be different when different time
21 | persons.stream().sorted((p1, p2) -> p1.getBirthday().compareTo(p2.getBirthday())).forEach(System.out::println);
22 |
23 | }
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/src/main/java/com/caysever/java8/stream/StatisticsExample.java:
--------------------------------------------------------------------------------
1 | package com.caysever.java8.stream;
2 |
3 | import com.caysever.java8.model.Person;
4 | import com.caysever.java8.utils.PersonUtil;
5 |
6 | import java.util.IntSummaryStatistics;
7 | import java.util.List;
8 | import java.util.Map;
9 | import java.util.stream.Collectors;
10 |
11 | /**
12 | * @author alican
13 | */
14 | public class StatisticsExample {
15 |
16 | /**
17 | * @param args
18 | */
19 | public static void main(String[] args) {
20 | List persons = PersonUtil.mockPersons();
21 |
22 | Map groupPersonsByAge = persons.stream().collect(
23 | Collectors.groupingBy(Person::getGender, Collectors.summarizingInt(Person::getAge)));
24 | System.err.println("0. Group person objects by gender and get age statistics: ");
25 | System.out.println(groupPersonsByAge.toString());
26 | IntSummaryStatistics malesAge = groupPersonsByAge.get("male");
27 | System.out.println("Avgerage male age:" + malesAge.getAverage());
28 | System.out.println("Max male age:" + malesAge.getMax());
29 | System.out.println("Min male age:" + malesAge.getMin());
30 | }
31 |
32 | }
33 |
--------------------------------------------------------------------------------
/src/main/java/com/caysever/java8/utils/PersonUtil.java:
--------------------------------------------------------------------------------
1 | package com.caysever.java8.utils;
2 |
3 | import com.caysever.java8.model.Person;
4 |
5 | import java.util.Arrays;
6 | import java.util.Date;
7 | import java.util.List;
8 | import java.util.Random;
9 |
10 | public class PersonUtil {
11 |
12 | public static List mockPersons() {
13 | List persons = Arrays.asList(
14 | new Person("alican", "akkuş", "male", 23, new Date().getTime() + new Random().nextInt()),
15 | new Person("mustafa", "demir", "male", 22, new Date().getTime() + new Random().nextInt()),
16 | new Person("enes", "açıkoğlu", "male", 25, new Date().getTime() + new Random().nextInt()),
17 | new Person("ilkay", "günel", "male", 23, new Date().getTime() + new Random().nextInt()),
18 | new Person("hatun", "hatun", "female", 25, new Date().getTime() + new Random().nextInt()));
19 | return persons;
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/src/test/java/com/caysever/java8/AppTest.java:
--------------------------------------------------------------------------------
1 | package com.caysever.java8;
2 |
3 | import junit.framework.Test;
4 | import junit.framework.TestCase;
5 | import junit.framework.TestSuite;
6 |
7 | /**
8 | * Unit test for simple App.
9 | */
10 | public class AppTest
11 | extends TestCase
12 | {
13 | /**
14 | * Create the test case
15 | *
16 | * @param testName name of the test case
17 | */
18 | public AppTest( String testName )
19 | {
20 | super( testName );
21 | }
22 |
23 | /**
24 | * @return the suite of tests being tested
25 | */
26 | public static Test suite()
27 | {
28 | return new TestSuite( AppTest.class );
29 | }
30 |
31 | /**
32 | * Rigourous Test :-)
33 | */
34 | public void testApp()
35 | {
36 | assertTrue( true );
37 | }
38 | }
39 |
--------------------------------------------------------------------------------