Skip to content

Commit 43f05a3

Browse files
Merge pull request #76 from sayed-moin-ahmed/develop
Develop
2 parents fa76658 + 71c041e commit 43f05a3

File tree

6 files changed

+123
-23
lines changed

6 files changed

+123
-23
lines changed

example.iml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,5 +127,8 @@
127127
<orderEntry type="library" name="Maven: org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.4.10" level="project" />
128128
<orderEntry type="library" scope="TEST" name="Maven: org.jetbrains.kotlin:kotlin-test:1.4.10" level="project" />
129129
<orderEntry type="library" scope="TEST" name="Maven: org.jetbrains.kotlin:kotlin-test-common:1.4.10" level="project" />
130+
<orderEntry type="library" name="Maven: org.testng:testng:7.4.0" level="project" />
131+
<orderEntry type="library" name="Maven: com.beust:jcommander:1.78" level="project" />
132+
<orderEntry type="library" name="Maven: org.webjars:jquery:3.5.1" level="project" />
130133
</component>
131134
</module>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.java15.example.java8inaction.Java8_9_10_11_whatshappening;
1+
package com.java15.example.java8inaction;
22

33
public class Apple {
44

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.java15.example.java8inaction.Java8_9_10_11_whatshappening;
1+
package com.java15.example.java8inaction;
22

33
public interface Colors {
44
String GREEN ="GREEN";
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.java15.example.java8inaction;
2+
3+
import java.util.ArrayList;
4+
import java.util.Comparator;
5+
import java.util.List;
6+
7+
public class Data {
8+
public static List<Apple> getData() {
9+
Apple apple1 = new Apple(10, Colors.GREEN);
10+
Apple apple2 = new Apple(1,Colors.RED);
11+
Apple apple3 = new Apple(5,Colors.GREEN);
12+
Apple apple4 = new Apple(3,Colors.RED);
13+
Apple apple5 = new Apple(3,Colors.RED);
14+
Apple apple6 = new Apple(5,Colors.GREEN);
15+
List<Apple> apples = List.of(apple1,apple2,apple3,apple4,apple5,apple6);
16+
return apples;
17+
}
18+
19+
public static List<Apple> getData1() {
20+
Apple apple1 = new Apple(10,Colors.GREEN);
21+
Apple apple2 = new Apple(1,Colors.RED);
22+
Apple apple3 = new Apple(5,Colors.GREEN);
23+
Apple apple4 = new Apple(3,Colors.RED);
24+
Apple apple5 = new Apple(3,Colors.RED);
25+
Apple apple6 = new Apple(5,Colors.GREEN);
26+
List<Apple> apples = new ArrayList<>();
27+
apples.add(apple1);
28+
apples.add(apple2);
29+
apples.add(apple3);
30+
apples.add(apple4);
31+
apples.add(apple5);
32+
apples.add(apple6);
33+
apples.sort(Comparator.comparing(Apple::getWeight));
34+
return apples;
35+
}
36+
}
Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package com.java15.example.java8inaction.Java8_9_10_11_whatshappening;
22

3+
import com.java15.example.java8inaction.Apple;
4+
import com.java15.example.java8inaction.Colors;
5+
import com.java15.example.java8inaction.Data;
6+
37
import java.io.File;
4-
import java.util.Comparator;
5-
import java.util.List;
6-
import java.util.Map;
7-
import java.util.Optional;
8+
import java.util.*;
89
import java.util.function.Predicate;
910
import java.util.stream.Collectors;
1011

@@ -13,13 +14,14 @@
1314
public class Test {
1415

1516
public static void main(String[] args) {
16-
List<Apple> apples = getData();
17-
//filterApples(apples,(Apple a) -> Colors.GREEN.equals(a.getColor())).stream().forEach(System.out::println);
17+
List<Apple> apples = Data.getData();
1818
//filterApples(apples,(Apple a) -> Colors.GREEN.equals(a.getColor())).stream().forEach(System.out::println);
19-
//filterGroupApples(apples,(Apple a) -> Colors.RED.equals(a.getColor())).forEach((i,list) ->{System.out.println(i+""+list);});
20-
//filterGroupApples(apples,(Apple a) -> {return true;}).forEach((i,list) ->{System.out.println(i+""+list);});
21-
minAppleByWeight(apples).ifPresent(System.out::println);
22-
maxAppleByWeight(apples).ifPresent(System.out::println);
19+
// filterApples(apples,(Apple a) -> Colors.GREEN.equals(a.getColor())).stream().forEach(System.out::println);
20+
// filterGroupApples(apples,(Apple a) -> Colors.RED.equals(a.getColor())).forEach((i,list) ->{System.out.println(i+""+list);});
21+
// filterGroupApples(apples,(Apple a) -> {return true;}).forEach((i,list) ->{System.out.println(i+""+list);});
22+
// minAppleByWeight(apples).ifPresent(System.out::println);
23+
// maxAppleByWeight(apples).ifPresent(System.out::println);
24+
// getData1().forEach(System.out::println);
2325
}
2426

2527
static List<Apple> filterApples(List<Apple> inventory, Predicate<Apple> p) {
@@ -38,20 +40,11 @@ static Optional<Apple> maxAppleByWeight(List<Apple> inventory) {
3840
return inventory.stream().max(Comparator.comparing(Apple::getWeight));
3941
}
4042

41-
private static List<Apple> getData() {
42-
Apple apple1 = new Apple(10,Colors.GREEN);
43-
Apple apple2 = new Apple(1,Colors.RED);
44-
Apple apple3 = new Apple(5,Colors.GREEN);
45-
Apple apple4 = new Apple(3,Colors.RED);
46-
Apple apple5 = new Apple(3,Colors.RED);
47-
Apple apple6 = new Apple(5,Colors.GREEN);
48-
List<Apple> apples = List.of(apple1,apple2,apple3,apple4,apple5,apple6);
49-
return apples;
50-
}
51-
5243
static File[] handlingFiles(){
5344
File[] hiddenFiles = new File(".").listFiles(File::isHidden);
5445
return hiddenFiles;
5546
}
5647

5748
}
49+
50+
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.java15.example.java8inaction.passingcodewithbehaviorparameterization;
2+
3+
import com.java15.example.java8inaction.Apple;
4+
import com.java15.example.java8inaction.Colors;
5+
import com.java15.example.java8inaction.Data;
6+
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
import java.util.Map;
10+
import java.util.concurrent.*;
11+
import java.util.function.Function;
12+
import java.util.function.Predicate;
13+
import java.util.stream.Collectors;
14+
15+
import static java.util.stream.Collectors.groupingBy;
16+
17+
public class Test {
18+
public static void main(String[] args) throws ExecutionException, InterruptedException {
19+
List<Apple> apples = Data.getData();
20+
// beforeJava8(apples);
21+
//filter(apples,(Apple e) -> Colors.GREEN.equals(e.getColor())).forEach(System.out::println);
22+
// groupBy(apples,apple -> apple.getWeight()).forEach((i, list)->System.out.println(i+""+list));
23+
executorService(()->{ int i=0; while(i<10){ i++;} return i;});
24+
}
25+
26+
27+
private static <V> void executorService(Callable<V> task) throws InterruptedException, ExecutionException {
28+
ExecutorService executorService = Executors.newCachedThreadPool();
29+
Future<V> threadName = executorService.submit(task);
30+
System.out.println(threadName.get());
31+
executorService.shutdown();
32+
}
33+
34+
//More Generic
35+
static public<T> List<T> filter(List<T> list,Predicate<T> predicate){
36+
return list.stream().filter(predicate).collect(Collectors.toList());
37+
}
38+
39+
static public <T,K> Map<? extends K, List<T>> groupBy(List<T> list, Function<? super T, ? extends K> classifier){
40+
return list.stream().collect(groupingBy(classifier));
41+
}
42+
43+
private static void beforeJava8(List<Apple> apples) {
44+
final List<Apple> filteredApple = new ArrayList<>();
45+
apples.forEach(e->
46+
{
47+
if(new CustomPredicateImpl().test(e))
48+
filteredApple.add(e);
49+
});
50+
filteredApple.forEach(System.out::println);
51+
}
52+
53+
}
54+
55+
56+
interface CustomPredicate<T>{
57+
boolean test(T t);
58+
}
59+
60+
class CustomPredicateImpl implements CustomPredicate{
61+
62+
@Override
63+
public boolean test(Object o) {
64+
if(Colors.GREEN.equals(((Apple)o).getColor()))
65+
return true;
66+
return false;
67+
}
68+
}

0 commit comments

Comments
 (0)