Skip to content

Commit 742badf

Browse files
author
Saurabh Oza
committed
Adding java 8 features code
0 parents commit 742badf

File tree

14 files changed

+612
-0
lines changed

14 files changed

+612
-0
lines changed

java-8-features/pom.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.onlyfullstack</groupId>
5+
<artifactId>java-8-features</artifactId>
6+
<packaging>jar</packaging>
7+
<version>1.0-SNAPSHOT</version>
8+
<name>java-8-features</name>
9+
<url>http://maven.apache.org</url>
10+
<dependencies>
11+
<dependency>
12+
<groupId>junit</groupId>
13+
<artifactId>junit</artifactId>
14+
<version>3.8.1</version>
15+
<scope>test</scope>
16+
</dependency>
17+
</dependencies>
18+
</project>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.onlyfullstack.bean;
2+
3+
public class GraphicsCard {
4+
5+
private String size;
6+
private GraphicsMemory graphicsMemory;
7+
8+
9+
public GraphicsCard(String size, GraphicsMemory graphicsMemory) {
10+
super();
11+
this.size = size;
12+
this.graphicsMemory = graphicsMemory;
13+
}
14+
15+
public GraphicsMemory getGraphicsMemory() {
16+
return graphicsMemory;
17+
}
18+
19+
public void setGraphicsMemory(GraphicsMemory graphicsMemory) {
20+
this.graphicsMemory = graphicsMemory;
21+
}
22+
23+
public GraphicsCard() {
24+
super();
25+
}
26+
27+
public String getSize() {
28+
return size;
29+
}
30+
31+
public void setSize(String size) {
32+
this.size = size;
33+
}
34+
35+
@Override
36+
public String toString() {
37+
return "GraphicsCard [size=" + size + "]";
38+
}
39+
40+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.onlyfullstack.bean;
2+
3+
public class GraphicsMemory {
4+
5+
private String dedicatedMemory;
6+
private String memoryType;
7+
8+
public GraphicsMemory(String dedicatedMemory, String memoryType) {
9+
super();
10+
this.dedicatedMemory = dedicatedMemory;
11+
this.memoryType = memoryType;
12+
}
13+
14+
public String getDedicatedMemory() {
15+
return dedicatedMemory;
16+
}
17+
18+
public void setDedicatedMemory(String dedicatedMemory) {
19+
this.dedicatedMemory = dedicatedMemory;
20+
}
21+
22+
public String getMemoryType() {
23+
return memoryType;
24+
}
25+
26+
public void setMemoryType(String memoryType) {
27+
this.memoryType = memoryType;
28+
}
29+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.onlyfullstack.bean;
2+
3+
import java.util.Optional;
4+
5+
public class SmartPhone {
6+
7+
private Optional<GraphicsCard> graphicsCard;
8+
9+
public Optional<GraphicsCard> getGraphicsCard() {
10+
return graphicsCard;
11+
}
12+
public void setGraphicsCard(Optional<GraphicsCard> graphicsCard) {
13+
this.graphicsCard = graphicsCard;
14+
}
15+
@Override
16+
public String toString() {
17+
return "SmartPhone [graphicsCard=" + graphicsCard
18+
+ "]";
19+
}
20+
21+
22+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.onlyfullstack.bean;
2+
3+
public class Student {
4+
5+
private String name;
6+
private String city;
7+
private int age;
8+
private String[] courses;
9+
10+
public Student(String name, String city, int age, String[] courses) {
11+
super();
12+
this.name = name;
13+
this.city = city;
14+
this.age = age;
15+
this.courses = courses;
16+
}
17+
public String[] getCourses() {
18+
return courses;
19+
}
20+
public void setCourses(String[] courses) {
21+
this.courses = courses;
22+
}
23+
public int getAge() {
24+
return age;
25+
}
26+
public void setAge(int age) {
27+
this.age = age;
28+
}
29+
public String getName() {
30+
return name;
31+
}
32+
public void setName(String name) {
33+
this.name = name;
34+
}
35+
public String getCity() {
36+
return city;
37+
}
38+
public void setCity(String city) {
39+
this.city = city;
40+
}
41+
42+
@Override
43+
public String toString() {
44+
return "Student [name=" + name + ", city=" + city + ", age=" + age + "]";
45+
}
46+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.onlyfullstack.lambdas;
2+
3+
@FunctionalInterface
4+
public interface IConnection {
5+
6+
public void connect();
7+
8+
default void print() {
9+
System.out.println("in default print method");
10+
}
11+
12+
static void description() {
13+
System.out.println("in static method");
14+
}
15+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.onlyfullstack.lambdas;
2+
3+
public class Lambda {
4+
5+
public static void main(String[] args) {
6+
IConnection connection = () -> {System.out.println("Implementing the connect method");};
7+
8+
connection.connect();
9+
connection.print();
10+
IConnection.description();
11+
simpleConceptWithRunnable();
12+
}
13+
14+
private static void simpleConceptWithRunnable() {
15+
Runnable runnable = new Runnable() { // boilerplate code which needs to be written everytime
16+
@Override
17+
public void run() {
18+
System.out.println("Inside annonymos inner class");
19+
}
20+
};
21+
22+
Runnable runnable2 = () -> { // Lambda expression
23+
System.out.println("Inside lambda expression");
24+
};
25+
26+
Thread thread = new Thread(runnable);
27+
thread.start();
28+
29+
thread = new Thread(runnable2);
30+
thread.start();
31+
32+
SingleParam singleParam = (param) -> {System.out.printf("Hello %s\n",param);};
33+
singleParam.print("saurabh");
34+
35+
DoubleParam doubleParam = (param1, param2) -> System.out.printf("param1 : %s, param2: %s\n", param1, param2);
36+
doubleParam.print("OnlyFullstack", "Development");
37+
}
38+
39+
interface SingleParam {
40+
public void print(String param);
41+
}
42+
43+
interface DoubleParam {
44+
public void print(String param1, String param2);
45+
}
46+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.onlyfullstack.lambdas;
2+
3+
import java.util.List;
4+
import java.util.Set;
5+
import java.util.stream.Collectors;
6+
import java.util.stream.Stream;
7+
8+
import com.onlyfullstack.bean.Student;
9+
import com.onlyfullstack.util.JavaInputFixture;
10+
11+
public class MethodReference {
12+
13+
public static void main(String[] args) {
14+
staticReference_WithoutMethodRef();
15+
staticReference_WithMethodRef();
16+
17+
instanceReferenceWithClass_WithMethodRef();
18+
instanceReferenceWithClass_WithoutMethodRef();
19+
20+
instanceReference_WithoutMethodRef();
21+
instanceReference_WithMethodRef();
22+
}
23+
24+
private static void printMe(Integer input) {
25+
System.out.println("Printing "+ input);
26+
}
27+
28+
private static void staticReference_WithoutMethodRef() {
29+
Stream<Integer> stream = Stream.of(1,2,3,4,5,6,7,8,9);
30+
stream.filter(i -> i%2==0 )
31+
.forEach(i -> MethodReference.printMe(i));
32+
}
33+
34+
private static void staticReference_WithMethodRef() {
35+
Stream<Integer> stream = Stream.of(1,2,3,4,5,6,7,8,9);
36+
stream.filter(i -> i%2==0 )
37+
.forEach(MethodReference::printMe);
38+
}
39+
40+
private static void instanceReference_WithoutMethodRef() {
41+
Stream<Integer> stream = Stream.of(1,2,3,4,5,6,7,8,9);
42+
stream.filter(i -> i%2==0 )
43+
.forEach((i -> System.out.println(i)));
44+
}
45+
46+
private static void instanceReference_WithMethodRef() {
47+
Stream<Integer> stream = Stream.of(1,2,3,4,5,6,7,8,9);
48+
stream.filter(i -> i%2==0 )
49+
.forEach(System.out::println);
50+
}
51+
52+
private static void instanceReferenceWithClass_WithoutMethodRef() {
53+
List<Student> students = JavaInputFixture.createList();
54+
Set<String> names = students.stream()
55+
.map(student -> student.getName()) // this will convert the Student Stream into String Stream by
56+
// applying the getName()
57+
.collect(Collectors.toSet());
58+
59+
System.out.printf("Names of all students %s\n", names);
60+
}
61+
62+
private static void instanceReferenceWithClass_WithMethodRef() {
63+
List<Student> students = JavaInputFixture.createList();
64+
Set<String> names = students.stream()
65+
.map(Student::getName) // this will convert the Student Stream into String Stream by
66+
// applying the getName()
67+
.collect(Collectors.toSet());
68+
69+
System.out.printf("Names of all students %s\n", names);
70+
}
71+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.onlyfullstack.optional;
2+
3+
import java.util.ArrayDeque;
4+
import java.util.ArrayList;
5+
import java.util.Collection;
6+
import java.util.LinkedHashSet;
7+
import java.util.LinkedList;
8+
import java.util.List;
9+
import java.util.Optional;
10+
import java.util.PriorityQueue;
11+
12+
import com.onlyfullstack.bean.GraphicsCard;
13+
import com.onlyfullstack.bean.GraphicsMemory;
14+
import com.onlyfullstack.bean.SmartPhone;
15+
16+
public class OptionalFeature {
17+
18+
public static void main(String[] args) {
19+
getGraphicsCardSize(createSmartPhoneWithGraphicsCard());
20+
getGraphicsCardSize(createSmartPhoneWithoutGraphicsCard());
21+
getGraphicsCardSize(createSmartPhoneWithEmptyGraphicsCard());
22+
}
23+
24+
private static void getGraphicsCardSize(SmartPhone smartPhone) {
25+
String size = smartPhone.getGraphicsCard()
26+
.map(GraphicsCard::getGraphicsMemory) // map returns the Optional of type passed as a parameter (here its string)
27+
.map(GraphicsMemory::getDedicatedMemory)
28+
.orElse("unkonwn");
29+
System.out.println("Size : " + size + ", for Object : "+smartPhone.toString());
30+
}
31+
32+
private static void ofVsOfNullable() {
33+
Optional<GraphicsCard> graphicsCard = Optional.of(new GraphicsCard());
34+
if(graphicsCard.isPresent()) {
35+
System.out.println(graphicsCard.get());
36+
}
37+
38+
graphicsCard.ifPresent(System.out::println);
39+
GraphicsCard newCard = graphicsCard.get();
40+
}
41+
42+
private static SmartPhone createSmartPhoneWithGraphicsCard() {
43+
SmartPhone smartPhone = new SmartPhone();
44+
GraphicsMemory memory = new GraphicsMemory("4 GB", "");
45+
Optional<GraphicsCard> card = Optional.ofNullable(new GraphicsCard("4 gb", memory));
46+
smartPhone.setGraphicsCard(card);
47+
return smartPhone;
48+
}
49+
50+
private static SmartPhone createSmartPhoneWithoutGraphicsCard() {
51+
SmartPhone smartPhone = new SmartPhone();
52+
Optional<GraphicsCard> card = Optional.ofNullable(null);
53+
smartPhone.setGraphicsCard(card);
54+
return smartPhone;
55+
}
56+
57+
private static SmartPhone createSmartPhoneWithEmptyGraphicsCard() {
58+
SmartPhone smartPhone = new SmartPhone();
59+
Optional<GraphicsCard> card = Optional.ofNullable(new GraphicsCard());
60+
smartPhone.setGraphicsCard(card);
61+
return smartPhone;
62+
}
63+
64+
private static void traditionalApproach(SmartPhone smartPhone) {
65+
String size = "unknown";
66+
if(smartPhone.getGraphicsCard()!=null) {
67+
GraphicsCard graphicsCard = smartPhone.getGraphicsCard().get();
68+
if(graphicsCard != null) {
69+
GraphicsMemory graphicsMemory = graphicsCard.getGraphicsMemory();
70+
if(graphicsMemory != null && graphicsMemory.getDedicatedMemory() != null) {
71+
size = graphicsMemory.getDedicatedMemory();
72+
}
73+
}
74+
}
75+
System.out.println("Size : " + size + ", for Object : "+smartPhone.toString());
76+
}
77+
}

0 commit comments

Comments
 (0)