Skip to content

Commit 43de615

Browse files
author
Arpit Bhardwaj
committed
Advanced Generics topics such as intersection type
1 parent b11a7fd commit 43de615

2 files changed

Lines changed: 80 additions & 0 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.ab.generics.advanced;
2+
3+
import com.ab.generics.model.Person;
4+
5+
import java.io.*;
6+
7+
/**
8+
* @author Arpit Bhardwaj
9+
*/
10+
public class IntersectionType {
11+
public static void main(String[] args) throws FileNotFoundException {
12+
IntersectionType intersectionType = new IntersectionType();
13+
14+
DataInputStream dataInputStream = new DataInputStream(new FileInputStream("src/main/resources/person"));
15+
Person person = intersectionType.read(dataInputStream);
16+
17+
RandomAccessFile randomAccessFile = new RandomAccessFile("src/main/resources/person", "rw");
18+
person = intersectionType.read(randomAccessFile);
19+
20+
System.out.println(person);
21+
}
22+
23+
/*private Person read(DataInputStream source) {
24+
try(DataInputStream input = source){
25+
return new Person(input.readUTF(),input.readInt());
26+
}catch(IOException e){
27+
throw new RuntimeException(e);
28+
}
29+
}*/
30+
31+
private <T extends DataInput & Closeable> Person read(T source) {
32+
try(T input = source){
33+
return new Person(input.readUTF(),input.readInt());
34+
}catch(IOException e){
35+
throw new RuntimeException(e);
36+
}
37+
}
38+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.ab.generics.advanced;
2+
3+
import com.ab.generics.model.Employee;
4+
import com.ab.generics.model.Partner;
5+
import com.ab.generics.model.Person;
6+
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
import java.util.Map;
10+
import java.util.function.Predicate;
11+
import java.util.stream.Collectors;
12+
13+
import static java.util.stream.Collectors.counting;
14+
15+
/**
16+
* @author Arpit Bhardwaj
17+
*/
18+
public class LambdaExpression {
19+
20+
public static void main(String[] args) {
21+
final Partner donDrapper = new Partner("Don Drapper", 89);
22+
final Employee peggyOlson = new Employee("Peggy Olson", 65);
23+
final Partner bertCooper = new Partner("Bert Cooper", 100);
24+
25+
Predicate<Person> isOld = person -> person.getAge() > 80;
26+
System.out.println(isOld.test(donDrapper));
27+
System.out.println(isOld.test(peggyOlson));
28+
29+
List<Person> personList = new ArrayList<>();
30+
personList.add(donDrapper);
31+
personList.add(peggyOlson);
32+
personList.add(bertCooper);
33+
34+
Map<Boolean, List<Person>> collect = personList.stream()
35+
.collect(Collectors.partitioningBy(isOld));
36+
System.out.println(collect);
37+
38+
Map<Boolean, Long> collect2 = personList.stream()
39+
.collect(Collectors.partitioningBy(isOld,counting()));
40+
System.out.println(collect2);
41+
}
42+
}

0 commit comments

Comments
 (0)