Skip to content

Commit 0587c1d

Browse files
authored
Add files via upload
1 parent d80f0bb commit 0587c1d

4 files changed

Lines changed: 119 additions & 0 deletions

File tree

mapVsflatMap/Customer.java

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

mapVsflatMap/EkartDataBase.java

Lines changed: 21 additions & 0 deletions
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", "john@gmail.com", Arrays.asList("397937955", "21654725")),
14+
new Customer(102, "smith", "smith@gmail.com", Arrays.asList("89563865", "2487238947")),
15+
new Customer(103, "peter", "peter@gmail.com", Arrays.asList("38946328654", "3286487236")),
16+
new Customer(104, "kely", "kely@gmail.com", Arrays.asList("389246829364", "948609467"))
17+
).collect(Collectors.toList());
18+
}
19+
20+
21+
}

mapVsflatMap/MapVsFlatMap.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.javatechie;
2+
3+
import java.util.List;
4+
import java.util.stream.Collectors;
5+
6+
public class MapVsFlatMap {
7+
public static void main(String[] args) {
8+
9+
List<Customer> customers = EkartDataBase.getAll();
10+
11+
//List<Customer> convert List<String> -> Data Transformation
12+
//mapping : customer -> customer.getEmail()
13+
//customer -> customer.getEmail() one to one mapping
14+
List<String> emails = customers.stream()
15+
.map(customer -> customer.getEmail())
16+
.collect(Collectors.toList());
17+
System.out.println(emails);
18+
19+
//customer -> customer.getPhoneNumbers() ->> one to many mapping
20+
//customer -> customer.getPhoneNumbers() ->> one to many mapping
21+
List<List<String>> phoneNumbers = customers.
22+
stream().map(customer -> customer.getPhoneNumbers())
23+
.collect(Collectors.toList());
24+
System.out.println(phoneNumbers);
25+
26+
//List<Customer> convert List<String> -> Data Transformation
27+
//mapping : customer -> phone Numbers
28+
//customer -> customer.getPhoneNumbers() ->> one to many mapping
29+
List<String> phones = customers.stream()
30+
.flatMap(customer -> customer.getPhoneNumbers().stream())
31+
.collect(Collectors.toList());
32+
System.out.println(phones);
33+
}
34+
}

mapVsflatMap/readme.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Differences between Java 8 Map() Vs flatMap() :
2+
3+
map() | flatMap() |
4+
--- | --- |
5+
It processes stream of values. | It processes stream of stream of values.
6+
It does only mapping. | It performs mapping as well as flattening.
7+
It’s mapper function produces single value for each input value. | It’s mapper function produces multiple values for each input value.
8+
It is a One-To-One mapping. | It is a One-To-Many mapping.
9+
Data Transformation : From Stream<T> to Stream<R> | Data Transformation : From Stream<Stream<T> to Stream<R>
10+
Use this method when the mapper function is producing a single value for each input value. | Use this method when the mapper function is producing multiple values for each input value.
11+

0 commit comments

Comments
 (0)