Skip to content

Commit 989671f

Browse files
committed
feat: Java 8 Streams map() examples.
1 parent d4d00e4 commit 989671f

File tree

4 files changed

+286
-0
lines changed

4 files changed

+286
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package br.com.somejava8examples.commons.entities;
2+
3+
import java.math.BigDecimal;
4+
import java.util.Objects;
5+
6+
import br.com.somejava8examples.commons.BaseEntity;
7+
8+
public class Staff implements BaseEntity {
9+
10+
private String name;
11+
private int age;
12+
private BigDecimal salary;
13+
14+
public Staff(String name, int age, BigDecimal salary) {
15+
this.name = name;
16+
this.age = age;
17+
this.salary = salary;
18+
}
19+
20+
public String getName() {
21+
return name;
22+
}
23+
24+
public void setName(String name) {
25+
this.name = name;
26+
}
27+
28+
public int getAge() {
29+
return age;
30+
}
31+
32+
public void setAge(int age) {
33+
this.age = age;
34+
}
35+
36+
public BigDecimal getSalary() {
37+
return salary;
38+
}
39+
40+
public void setSalary(BigDecimal salary) {
41+
this.salary = salary;
42+
}
43+
44+
@Override
45+
public int hashCode() {
46+
return Objects.hash(age, name, salary);
47+
}
48+
49+
@Override
50+
public boolean equals(Object obj) {
51+
if (this == obj) {
52+
return true;
53+
}
54+
if (!(obj instanceof Staff)) {
55+
return false;
56+
}
57+
Staff other = (Staff) obj;
58+
return age == other.age && Objects.equals(name, other.name) && Objects.equals(salary, other.salary);
59+
}
60+
61+
@Override
62+
public String toString() {
63+
return "Staff [" + (name != null ? "name=" + name + ", " : "") + "age=" + age + ", "
64+
+ (salary != null ? "salary=" + salary : "") + "]";
65+
}
66+
67+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package br.com.somejava8examples.commons.entities;
2+
3+
import java.util.Objects;
4+
5+
import br.com.somejava8examples.commons.BaseEntity;
6+
7+
public class StaffPublic implements BaseEntity {
8+
9+
private String name;
10+
private int age;
11+
private String extra;
12+
13+
public StaffPublic() {
14+
}
15+
16+
public StaffPublic(String name, int age, String extra) {
17+
this.name = name;
18+
this.age = age;
19+
this.extra = extra;
20+
}
21+
22+
public String getName() {
23+
return name;
24+
}
25+
26+
public void setName(String name) {
27+
this.name = name;
28+
}
29+
30+
public int getAge() {
31+
return age;
32+
}
33+
34+
public void setAge(int age) {
35+
this.age = age;
36+
}
37+
38+
public String getExtra() {
39+
return extra;
40+
}
41+
42+
public void setExtra(String extra) {
43+
this.extra = extra;
44+
}
45+
46+
@Override
47+
public int hashCode() {
48+
return Objects.hash(age, extra, name);
49+
}
50+
51+
@Override
52+
public boolean equals(Object obj) {
53+
if (this == obj) {
54+
return true;
55+
}
56+
if (!(obj instanceof StaffPublic)) {
57+
return false;
58+
}
59+
StaffPublic other = (StaffPublic) obj;
60+
return age == other.age && Objects.equals(extra, other.extra) && Objects.equals(name, other.name);
61+
}
62+
63+
@Override
64+
public String toString() {
65+
return "StaffPublic [" + (name != null ? "name=" + name + ", " : "") + "age=" + age + ", "
66+
+ (extra != null ? "extra=" + extra : "") + "]";
67+
}
68+
69+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package br.com.somejava8examples.streams;
2+
3+
import java.math.BigDecimal;
4+
import java.util.ArrayList;
5+
import java.util.Arrays;
6+
import java.util.List;
7+
import java.util.stream.Collectors;
8+
9+
import org.springframework.stereotype.Service;
10+
11+
import br.com.somejava8examples.commons.entities.Staff;
12+
import br.com.somejava8examples.commons.entities.StaffPublic;
13+
14+
@Service
15+
public class StreamsFilterMapService {
16+
17+
public static final String DANIEL = "daniel";
18+
public static final String JACK = "jack";
19+
public static final String LAWRENCE = "lawrence";
20+
21+
public List<Integer> listOfStringsToUppercase() {
22+
List<String> alpha = Arrays.asList("a", "b", "c", "d");
23+
24+
// Before Java8
25+
List<String> alphaUpper = new ArrayList<>();
26+
for (String s : alpha) {
27+
alphaUpper.add(s.toUpperCase());
28+
}
29+
30+
System.out.println(alpha); // [a, b, c, d]
31+
System.out.println(alphaUpper); // [A, B, C, D]
32+
33+
// Java 8
34+
List<String> collect = alpha.stream().map(String::toUpperCase).collect(Collectors.toList());
35+
System.out.println(collect); // [A, B, C, D]
36+
37+
// Extra, streams apply to any data type.
38+
List<Integer> num = Arrays.asList(1, 2, 3, 4, 5);
39+
List<Integer> collect1 = num.stream().map(n -> n * 2).collect(Collectors.toList());
40+
System.out.println(collect1); // [2, 4, 6, 8, 10]
41+
42+
return collect1;
43+
}
44+
45+
public List<String> listOfObjectsListOfString() {
46+
List<Staff> staff = Arrays.asList(new Staff(DANIEL, 30, new BigDecimal(10000)),
47+
new Staff(JACK, 27, new BigDecimal(20000)), new Staff(LAWRENCE, 33, new BigDecimal(30000)));
48+
49+
// Before Java 8
50+
List<String> result = new ArrayList<>();
51+
for (Staff x : staff) {
52+
result.add(x.getName());
53+
}
54+
System.out.println(result); // [daniel, jack, lawrence]
55+
56+
// Java 8
57+
List<String> collect = staff.stream().map(x -> x.getName()).collect(Collectors.toList());
58+
System.out.println(collect); // [daniel, jack, lawrence]
59+
60+
return collect;
61+
}
62+
63+
public List<StaffPublic> listOfObjectsListOfOtherObjects() {
64+
List<Staff> staff = Arrays.asList(new Staff(DANIEL, 30, new BigDecimal(10000)),
65+
new Staff(JACK, 27, new BigDecimal(20000)), new Staff(LAWRENCE, 33, new BigDecimal(30000)));
66+
67+
List<StaffPublic> result = convertToStaffPublic(staff);
68+
System.out.println(result);
69+
70+
return result;
71+
}
72+
73+
private List<StaffPublic> convertToStaffPublic(List<Staff> staff) {
74+
List<StaffPublic> result = new ArrayList<>();
75+
76+
for (Staff temp : staff) {
77+
78+
StaffPublic obj = new StaffPublic();
79+
obj.setName(temp.getName());
80+
obj.setAge(temp.getAge());
81+
if (DANIEL.equals(temp.getName())) {
82+
obj.setExtra("this field is for daniel only!");
83+
}
84+
85+
result.add(obj);
86+
}
87+
88+
return result;
89+
}
90+
91+
public List<StaffPublic> convertInsideTheMapMethodDirectly() {
92+
List<Staff> staff = Arrays.asList(new Staff(DANIEL, 30, new BigDecimal(10000)),
93+
new Staff(JACK, 27, new BigDecimal(20000)), new Staff(LAWRENCE, 33, new BigDecimal(30000)));
94+
95+
// convert inside the map() method directly.
96+
List<StaffPublic> result = staff.stream().map(temp -> {
97+
StaffPublic obj = new StaffPublic();
98+
obj.setName(temp.getName());
99+
obj.setAge(temp.getAge());
100+
if (DANIEL.equals(temp.getName())) {
101+
obj.setExtra("this field is for daniel only!");
102+
}
103+
return obj;
104+
}).collect(Collectors.toList());
105+
106+
System.out.println(result);
107+
108+
return result;
109+
}
110+
111+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package br.com.somejava8examples.comparator;
2+
3+
import static org.springframework.util.Assert.isTrue;
4+
5+
import org.junit.jupiter.api.Test;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.boot.test.context.SpringBootTest;
8+
import org.springframework.test.context.ActiveProfiles;
9+
10+
import br.com.somejava8examples.streams.StreamsFilterMapService;
11+
12+
@SpringBootTest
13+
@ActiveProfiles("test")
14+
public class StreamsFilterMapsTest {
15+
16+
@Autowired
17+
private StreamsFilterMapService service;
18+
19+
@Test
20+
public void listOfStringsToUppercaseTest() {
21+
isTrue(!service.listOfStringsToUppercase().isEmpty(), "List is empty");
22+
}
23+
24+
@Test
25+
public void listOfObjectsListOfStringTest() {
26+
isTrue(!service.listOfObjectsListOfString().isEmpty(), "List is empty");
27+
}
28+
29+
@Test
30+
public void listOfObjectsListOfOtherObjectsTest() {
31+
isTrue(!service.listOfObjectsListOfOtherObjects().isEmpty(), "List is empty");
32+
}
33+
34+
@Test
35+
public void convertInsideTheMapMethodDirectlyTest() {
36+
isTrue(!service.convertInsideTheMapMethodDirectly().isEmpty(), "List is empty");
37+
}
38+
39+
}

0 commit comments

Comments
 (0)