Skip to content

Commit c22cf6b

Browse files
authored
Add files via upload
1 parent 48a1047 commit c22cf6b

File tree

3 files changed

+121
-0
lines changed

3 files changed

+121
-0
lines changed

Optional/Customer.java

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.javatechie;
2+
3+
import java.util.List;
4+
import java.util.Optional;
5+
6+
public class Customer {
7+
8+
private int id;
9+
private String name;
10+
private String email;
11+
private List<String> phoneNumbers;
12+
13+
public Customer() {
14+
}
15+
16+
public Customer(int id, String name, String email, List<String> phoneNumbers) {
17+
this.id = id;
18+
this.name = name;
19+
this.email = email;
20+
this.phoneNumbers = phoneNumbers;
21+
}
22+
23+
public int getId() {
24+
return id;
25+
}
26+
27+
public void setId(int id) {
28+
this.id = id;
29+
}
30+
31+
public String getName() {
32+
return name;
33+
}
34+
35+
public void setName(String name) {
36+
this.name = name;
37+
}
38+
39+
public Optional<String> getEmail() {
40+
return Optional.ofNullable(email);
41+
}
42+
43+
public void setEmail(String email) {
44+
this.email = email;
45+
}
46+
47+
public List<String> getPhoneNumbers() {
48+
return phoneNumbers;
49+
}
50+
51+
public void setPhoneNumbers(List<String> phoneNumbers) {
52+
this.phoneNumbers = phoneNumbers;
53+
}
54+
}

Optional/EkartDataBase.java

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.javatechie;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
import java.util.stream.Collectors;
6+
import java.util.stream.Stream;
7+
8+
public class EkartDataBase {
9+
10+
11+
public static List<Customer> getAll() {
12+
return Stream.of(
13+
new Customer(101, "john", "[email protected]", Arrays.asList("397937955", "21654725")),
14+
new Customer(102, "smith", "[email protected]", Arrays.asList("89563865", "2487238947")),
15+
new Customer(103, "peter", "[email protected]", Arrays.asList("38946328654", "3286487236")),
16+
new Customer(104, "kely", "[email protected]", Arrays.asList("389246829364", "948609467"))
17+
).collect(Collectors.toList());
18+
}
19+
20+
21+
}

Optional/OptionalDemo.java

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.javatechie;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
import java.util.Optional;
6+
7+
public class OptionalDemo {
8+
9+
10+
11+
public static Customer getCustomerByEmailId(String email) throws Exception {
12+
List<Customer> customers = EkartDataBase.getAll();
13+
return customers.stream()
14+
.filter(customer -> customer.getEmail().equals(email))
15+
.findAny().orElseThrow(()->new Exception("no customer present with this email id"));
16+
17+
}
18+
19+
public static void main(String[] args) throws Exception {
20+
21+
Customer customer=new Customer(101, "john", "[email protected]", Arrays.asList("397937955", "21654725"));
22+
23+
//empty
24+
//of
25+
//ofNullable
26+
27+
Optional<Object> emptyOptional = Optional.empty();
28+
System.out.println(emptyOptional);
29+
30+
//Optional<String> emailOptional = Optional.of(customer.getEmail());
31+
//System.out.println(emailOptional);
32+
33+
Optional<String> emailOptional2 = Optional.ofNullable(customer.getEmail());
34+
/* if(emailOptional2.isPresent()){
35+
System.out.println(emailOptional2.get());
36+
}*/
37+
// System.out.println(emailOptional2.orElse("[email protected]"));
38+
39+
// System.out.println(emailOptional2.orElseThrow(()->new IllegalArgumentException("email not present")));
40+
41+
42+
System.out.println(emailOptional2.map(String::toUpperCase).orElseGet(()->"default email..."));
43+
44+
getCustomerByEmailId("pqr");
45+
}
46+
}

0 commit comments

Comments
 (0)