|
2 | 2 | Current Directory : /in28Minutes/git/functional-programming-with-java
|
3 | 3 | -->
|
4 | 4 |
|
| 5 | +## JShell Commands Executed |
| 6 | + |
| 7 | +```java |
| 8 | + |
| 9 | +System.out.println("Ranga") |
| 10 | + |
| 11 | +List<Integer> numbers = List.of(12, 9, 13, 4, 6, 2, 4, 12, 15); |
| 12 | + |
| 13 | +numbers.stream().reduce(0, (x,y)->x+y) |
| 14 | +numbers.stream().reduce(0, (x,y)->x) |
| 15 | +numbers.stream().reduce(0, (x,y)->y) |
| 16 | +numbers.stream().reduce(0, (x,y)-> x>y ? x:y) |
| 17 | + |
| 18 | +numbers.stream().reduce(Integer.MIN_VALUE, (x,y)-> x>y ? x:y) |
| 19 | +numbers.stream().reduce(Integer.MIN_VALUE, (x,y)-> x>y ? y:x) |
| 20 | +numbers.stream().reduce(Integer.MAX_VALUE, (x,y)-> x>y ? y:x) |
| 21 | + |
| 22 | +numbers.stream().reduce(0, (x,y) -> x*x + y*y) |
| 23 | +numbers.stream().map(x -> x*x).reduce(0, Integer::sum) |
| 24 | +numbers.stream().map(x -> x*x*x).reduce(0, Integer::sum) |
| 25 | + |
| 26 | +numbers.stream().filter(x -> x%2==1).reduce(0, Integer::sum) |
| 27 | +numbers.stream().filter(x -> x%2==0).reduce(0, Integer::sum) |
| 28 | + |
| 29 | +numbers.stream().distinct().forEach(System.out::println) |
| 30 | + |
| 31 | +numbers.stream().sorted().forEach(System.out::println) |
| 32 | + |
| 33 | +numbers.stream().distinct().sorted().forEach(System.out::println) |
| 34 | + |
| 35 | +List<String> courses = List.of("Spring", "Spring Boot", "API" , "Microservices","AWS", "PCF","Azure", "Docker", "Kubernetes"); |
| 36 | + |
| 37 | +courses.stream().sorted().forEach(System.out::println) |
| 38 | + |
| 39 | +courses.stream().sorted(Comparator.naturalOrder()).forEach(System.out::println) |
| 40 | +courses.stream().sorted(Comparator.reverseOrder()).forEach(System.out::println) |
| 41 | +courses.stream().sorted(Comparator.comparing(str -> str.length())).forEach(System.out::println) |
| 42 | + |
| 43 | +courses.stream().map(x -> x.length()).collect(Collectors.toList()) |
| 44 | + |
| 45 | +numbers.stream().map(x -> x*x).collect(Collectors.toList()) |
| 46 | + |
| 47 | +Supplier<String> supplier = () -> {return "Ranga";}; |
| 48 | + |
| 49 | +Consumer<String> consumer = (str) -> { System.out.println(str);System.out.println(str);}; |
| 50 | + |
| 51 | +List<Integer> numbers = List.of(12, 9, 13, 4, 6, 2, 4, 12, 15); |
| 52 | +numbers.stream() |
| 53 | + |
| 54 | +Stream.of(12, 9, 13, 4, 6, 2, 4, 12, 15).count() |
| 55 | +Stream.of(12, 9, 13, 4, 6, 2, 4, 12, 15).reduce(0, Integer::sum) |
| 56 | +Stream.of(12, 9, 13, 4, 6, 2, 4, 12, 15) |
| 57 | + |
| 58 | +int[] numberArray = {12, 9, 13, 4, 6, 2, 4, 12, 15}; |
| 59 | +Arrays.stream(numberArray) |
| 60 | +Arrays.stream(numberArray).sum() |
| 61 | +Arrays.stream(numberArray).average() |
| 62 | +Arrays.stream(numberArray).min() |
| 63 | +Arrays.stream(numberArray).max() |
| 64 | + |
| 65 | +IntStream.range(1,10) |
| 66 | +IntStream.range(1,10).sum() |
| 67 | +IntStream.rangeClosed(1,10).sum() |
| 68 | + |
| 69 | +IntStream.iterate(1, e -> e + 2).limit(10).sum() |
| 70 | +IntStream.iterate(1, e -> e + 2).limit(10).peek(System.out::println).sum() |
| 71 | +IntStream.iterate(2, e -> e + 2).limit(10).peek(System.out::println).sum() |
| 72 | +IntStream.iterate(2, e -> e * 2).limit(10).peek(System.out::println).sum() |
| 73 | +IntStream.iterate(2, e -> e * 2).limit(10).boxed().collect(Collectors.toList()) |
| 74 | + |
| 75 | +Integer.MAX_VALUE |
| 76 | +Long.MAX_VALUE |
| 77 | + |
| 78 | +IntStream.rangeClosed(1,50).reduce(1, (x,y)->x*y) |
| 79 | +LongStream.rangeClosed(1,50).reduce(1, (x,y)->x*y) |
| 80 | +LongStream.rangeClosed(1,50).reduce(1L, (x,y)->x*y) |
| 81 | +LongStream.rangeClosed(1,10).reduce(1, (x,y)->x*y) |
| 82 | +LongStream.rangeClosed(1,20).reduce(1, (x,y)->x*y) |
| 83 | +LongStream.rangeClosed(1,40).reduce(1, (x,y)->x*y) |
| 84 | +LongStream.rangeClosed(1,50).mapToObj(BigInteger::valueOf).reduce(BigInteger.ONE, BigInteger::multiply) |
| 85 | + |
| 86 | +courses.stream().collect(Collectors.joining(" ")) |
| 87 | +courses.stream().collect(Collectors.joining(",")) |
| 88 | + |
| 89 | +"Spring".split("") |
| 90 | + |
| 91 | +courses.stream().map(course -> course.split("")).collect(Collectors.toList()) |
| 92 | + |
| 93 | +courses.stream().map(course -> course.split("")) |
| 94 | + |
| 95 | +courses.stream().map(course -> course.split("")).flatMap(Arrays::stream).collect(Collectors.toList()) |
| 96 | + |
| 97 | +courses.stream().map(course -> course.split("")).flatMap(Arrays::stream).distinct().collect(Collectors.toList()) |
| 98 | + |
| 99 | +List<String> courses = List.of("Spring", "Spring Boot", "API" , "Microservices","AWS", "PCF","Azure", "Docker", "Kubernetes"); |
| 100 | + |
| 101 | +List<String> courses2 = List.of("Spring", "Spring Boot", "API" , "Microservices"," |
| 102 | + AWS", "PCF","Azure", "Docker", "Kubernetes"); |
| 103 | + |
| 104 | +courses.stream().flatMap(course -> courses2.stream().map(course2 -> List.of(course,course2))).collect(Collectors.toList()) |
| 105 | + |
| 106 | +courses.stream().flatMap(course -> courses2.stream().map(course2 -> List.of(course,course2))).filter(list -> list.get(0).equals(list.get(1))).collect(Collectors.toList()) |
| 107 | + |
| 108 | +courses.stream().flatMap(course -> courses2.stream().map(course2 -> List.of(course,course2))).filter(list -> !list.get(0).equals(list.get(1))).collect(Collectors.toList()) |
| 109 | + |
| 110 | +courses.stream().flatMap(course -> courses2.stream().filter(course2 -> course2.length()==course.length()).map(course2 -> List.of(course,course2))).filter(list -> !list.get(0).equals(list.get(1))).collect(Collectors.toList()) |
| 111 | + |
| 112 | +courses.stream().filter(courses -> courses.length()>11).map(String::toUpperCase).findFirst() |
| 113 | + |
| 114 | +courses.stream().peek(System.out::println).filter(courses -> courses.length()>11).map(String::toUpperCase).peek(System.out::println).findFirst() |
| 115 | + |
| 116 | +courses.stream().peek(System.out::println).filter(courses -> courses.length()>11).map(String::toUpperCase).peek(System.out::println) |
| 117 | + |
| 118 | +$4.findFirst()//Change $4 to your variable name |
| 119 | + |
| 120 | +List<String> courses = List.of("Spring", "Spring Boot", "API" , "Microservices","AWS", "PCF","Azure", "Docker", "Kubernetes"); |
| 121 | + |
| 122 | +courses.replaceAll( str -> str.toUpperCase()) //Error |
| 123 | + |
| 124 | +List<String> modifyableCourses = new ArrayList(courses); |
| 125 | + |
| 126 | +modifyableCourses.replaceAll(str -> str.toUpperCase()) |
| 127 | +modifyableCourses.removeIf(course -> course.length()<6) |
| 128 | + |
| 129 | +``` |
| 130 | + |
5 | 131 | ## Complete Code Examples
|
6 | 132 |
|
7 | 133 |
|
@@ -971,101 +1097,3 @@ public class FP03LambdaExpressions {
|
971 | 1097 | }
|
972 | 1098 | }
|
973 | 1099 | ```
|
974 |
| ---- |
975 |
| - |
976 |
| -### JShell Commands Executed |
977 |
| - |
978 |
| -```java |
979 |
| -System.out.println("Ranga") |
980 |
| -List<Integer> numbers = List.of(12, 9, 13, 4, 6, 2, 4, 12, 15); |
981 |
| -numbers.stream().reduce(0, (x,y)->x+y) |
982 |
| -numbers.stream().reduce(0, (x,y)->x) |
983 |
| -numbers.stream().reduce(0, (x,y)->y) |
984 |
| -numbers.stream().reduce(0, (x,y)-> x>y ? x:y) |
985 |
| -numbers.stream().reduce(Integer.MIN_VALUE, (x,y)-> x>y ? x:y) |
986 |
| -numbers.stream().reduce(Integer.MIN_VALUE, (x,y)-> x>y ? y:x) |
987 |
| -numbers.stream().reduce(Integer.MAX_VALUE, (x,y)-> x>y ? y:x) |
988 |
| -numbers |
989 |
| -numbers |
990 |
| -numbers.stream().reduce(0, (x,y) -> x*x + y*y) |
991 |
| -numbers.stream().map(x -> x*x).reduce(0, Integer::sum) |
992 |
| -numbers.stream().map(x -> x*x*x).reduce(0, Integer::sum) |
993 |
| -numbers.stream().filter(x -> x%2==1).reduce(0, Integer::sum) |
994 |
| -numbers.stream().filter(x -> x%2==0).reduce(0, Integer::sum) |
995 |
| -numbers.stream().distinct().forEach(System.out::println) |
996 |
| -numbers |
997 |
| -numbers.stream().sorted().forEach(System.out::println) |
998 |
| -numbers.stream().distinct().sorted().forEach(System.out::println) |
999 |
| -List<String> courses = List.of("Spring", "Spring Boot", "API" , "Microservices","AWS", "PCF","Azure", "Docker", "Kubernetes"); |
1000 |
| -courses |
1001 |
| -courses.stream().sorted().forEach(System.out::println) |
1002 |
| -courses |
1003 |
| -courses.stream().sorted().forEach(System.out::println) |
1004 |
| -courses.stream().sorted(Comparator.naturalOrder()).forEach(System.out::println) |
1005 |
| -courses.stream().sorted(Comparator.reverseOrder()).forEach(System.out::println) |
1006 |
| -courses.stream().sorted(Comparator.comparing(str -> str.length())).forEach(System.out::println) |
1007 |
| -numbers |
1008 |
| -courses |
1009 |
| -courses.stream().map(x -> x.length()).collect(Collectors.toList()) |
1010 |
| -numbers.stream().map(x -> x*x).collect(Collectors.toList()) |
1011 |
| -Supplier<String> supplier = () -> {return "Ranga";}; |
1012 |
| -Consumer<String> consumer = (str) -> { System.out.println(str);System.out.println(str);}; |
1013 |
| - |
1014 |
| - |
1015 |
| -numbers.stream() |
1016 |
| -List<Integer> numbers = List.of(12, 9, 13, 4, 6, 2, 4, 12, 15); |
1017 |
| -numbers.stream() |
1018 |
| -Stream.of(12, 9, 13, 4, 6, 2, 4, 12, 15).count() |
1019 |
| -Stream.of(12, 9, 13, 4, 6, 2, 4, 12, 15).reduce(0, Integer::sum) |
1020 |
| -Stream.of(12, 9, 13, 4, 6, 2, 4, 12, 15) |
1021 |
| -int[] numberArray = {12, 9, 13, 4, 6, 2, 4, 12, 15}; |
1022 |
| -Arrays.stream(numberArray) |
1023 |
| -Arrays.stream(numberArray).sum() |
1024 |
| -Arrays.stream(numberArray).average() |
1025 |
| -Arrays.stream(numberArray).min() |
1026 |
| -Arrays.stream(numberArray).max() |
1027 |
| -IntStream.range(1,10) |
1028 |
| -IntStream.range(1,10).sum() |
1029 |
| -IntStream.rangeClosed(1,10).sum() |
1030 |
| -IntStream.iterate(1, e -> e + 2).limit(10).sum() |
1031 |
| -IntStream.iterate(1, e -> e + 2).limit(10).peek(System.out::println).sum() |
1032 |
| -IntStream.iterate(2, e -> e + 2).limit(10).peek(System.out::println).sum() |
1033 |
| -IntStream.iterate(2, e -> e * 2).limit(10).peek(System.out::println).sum() |
1034 |
| -IntStream.iterate(2, e -> e * 2).limit(10).boxed().collect(Collectors.toList()) |
1035 |
| -Integer.MAX_VALUE |
1036 |
| -Long.MAX_VALUE |
1037 |
| -IntStream.rangeClosed(1,50).reduce(1, (x,y)->x*y) |
1038 |
| -LongStream.rangeClosed(1,50).reduce(1, (x,y)->x*y) |
1039 |
| -LongStream.rangeClosed(1,50).reduce(1L, (x,y)->x*y) |
1040 |
| -LongStream.rangeClosed(1,10).reduce(1, (x,y)->x*y) |
1041 |
| -LongStream.rangeClosed(1,20).reduce(1, (x,y)->x*y) |
1042 |
| -LongStream.rangeClosed(1,40).reduce(1, (x,y)->x*y) |
1043 |
| -LongStream.rangeClosed(1,50).mapToObj(BigInteger::valueOf).reduce(BigInteger.ONE, BigInteger::multiply) |
1044 |
| - |
1045 |
| -courses.stream().collect(Collectors.joining(" ")) |
1046 |
| -courses.stream().collect(Collectors.joining(",")) |
1047 |
| -"Spring".split("") |
1048 |
| -courses.stream().map(course -> course.split("")).collect(Collectors.toList()) |
1049 |
| -courses.stream().map(course -> course.split("")) |
1050 |
| -courses.stream().map(course -> course.split("")).flatMap(Arrays::stream).collect(Collectors.toList()) |
1051 |
| -courses.stream().map(course -> course.split("")).flatMap(Arrays::stream).distinct().collect(Collectors.toList()) |
1052 |
| -List<String> courses = List.of("Spring", "Spring Boot", "API" , "Microservices","AWS", "PCF","Azure", "Docker", "Kubernetes"); |
1053 |
| -List<String> courses2 = List.of("Spring", "Spring Boot", "API" , "Microservices","AWS", "PCF","Azure", "Docker", "Kubernetes"); |
1054 |
| -courses.stream().flatMap(course -> courses2.stream().map(course2 -> List.of(course,course2))).collect(Collectors.toList()) |
1055 |
| -courses.stream().flatMap(course -> courses2.stream().map(course2 -> List.of(course,course2))).filter(list -> list.get(0).equals(list.get(1))).collect(Collectors.toList()) |
1056 |
| -courses.stream().flatMap(course -> courses2.stream().map(course2 -> List.of(course,course2))).filter(list -> !list.get(0).equals(list.get(1))).collect(Collectors.toList()) |
1057 |
| -courses.stream().flatMap(course -> courses2.stream().filter(course2 -> course2.length()==course.length()).map(course2 -> List.of(course,course2))).filter(list -> !list.get(0).equals(list.get(1))).collect(Collectors.toList()) |
1058 |
| -courses.stream().filter(courses -> courses.length()>11).map(String::toUpperCase).findFirst() |
1059 |
| -courses.stream().peek(System.out::println).filter(courses -> courses.length()>11).map(String::toUpperCase).peek(System.out::println).findFirst() |
1060 |
| -courses.stream().peek(System.out::println).filter(courses -> courses.length()>11).map(String::toUpperCase).peek(System.out::println) |
1061 |
| -$4.findFirst() |
1062 |
| -List<String> courses = List.of("Spring", "Spring Boot", "API" , "Microservices","AWS", "PCF","Azure", "Docker", "Kubernetes"); |
1063 |
| -courses.replaceAll( str -> str.toUpperCase()) |
1064 |
| -List<String> modifyableCourses = new ArrayList(courses); |
1065 |
| -modifyableCourses.replaceAll(str -> str.toUpperCase()) |
1066 |
| -modifyableCourses |
1067 |
| -modifyableCourses.removeIf(course -> course.length()<6) |
1068 |
| -modifyableCourses |
1069 |
| - |
1070 |
| - |
1071 |
| -``` |
|
0 commit comments