Skip to content

Commit 5c0fba6

Browse files
committed
feat: Java 8 – Convert List to Map.
1 parent bada3c2 commit 5c0fba6

4 files changed

Lines changed: 199 additions & 4 deletions

File tree

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package br.com.somejava8examples.commons.entities;
2+
3+
import java.util.Objects;
4+
5+
public class Hosting {
6+
7+
private int Id;
8+
private String name;
9+
private long websites;
10+
11+
public Hosting(int id, String name, long websites) {
12+
Id = id;
13+
this.name = name;
14+
this.websites = websites;
15+
}
16+
17+
public int getId() {
18+
return Id;
19+
}
20+
21+
public void setId(int id) {
22+
Id = id;
23+
}
24+
25+
public String getName() {
26+
return name;
27+
}
28+
29+
public void setName(String name) {
30+
this.name = name;
31+
}
32+
33+
public long getWebsites() {
34+
return websites;
35+
}
36+
37+
public void setWebsites(long websites) {
38+
this.websites = websites;
39+
}
40+
41+
@Override
42+
public boolean equals(Object o) {
43+
if (this == o) return true;
44+
if (!(o instanceof Hosting)) return false;
45+
Hosting hosting = (Hosting) o;
46+
return getId() == hosting.getId() &&
47+
getWebsites() == hosting.getWebsites() &&
48+
Objects.equals(getName(), hosting.getName());
49+
}
50+
51+
@Override
52+
public int hashCode() {
53+
return Objects.hash(getId(), getName(), getWebsites());
54+
}
55+
56+
@Override
57+
public String toString() {
58+
return "Hosting{" +
59+
"Id=" + Id +
60+
", name='" + name + '\'' +
61+
", websites=" + websites +
62+
'}';
63+
}
64+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package br.com.somejava8examples.map;
2+
3+
import br.com.somejava8examples.commons.entities.Hosting;
4+
import org.springframework.stereotype.Service;
5+
6+
import java.util.*;
7+
import java.util.stream.Collectors;
8+
9+
@Service
10+
public class ConvertListToMapService {
11+
12+
private List<Hosting> getList() {
13+
List<Hosting> list = new ArrayList<>();
14+
list.add(new Hosting(1, "liquidweb.com", 80000));
15+
list.add(new Hosting(2, "linode.com", 90000));
16+
list.add(new Hosting(3, "digitalocean.com", 120000));
17+
list.add(new Hosting(4, "aws.amazon.com", 200000));
18+
list.add(new Hosting(5, "google.com", 1));
19+
return list;
20+
}
21+
22+
public Map<Integer, String> listToMapCollectorsToMap() {
23+
List<Hosting> list = getList();
24+
25+
// key = id, value - websites
26+
Map<Integer, String> result1 = list.stream().collect(Collectors.toMap(Hosting::getId, Hosting::getName));
27+
28+
System.out.println("Result 1 : " + result1);
29+
30+
// key = name, value - websites
31+
Map<String, Long> result2 = list.stream().collect(Collectors.toMap(Hosting::getName, Hosting::getWebsites));
32+
33+
System.out.println("Result 2 : " + result2);
34+
35+
// Same with result1, just different syntax
36+
// key = id, value = name
37+
Map<Integer, String> result3 = list.stream().collect(Collectors.toMap(x -> x.getId(), x -> x.getName()));
38+
39+
System.out.println("Result 3 : " + result3);
40+
41+
return result3;
42+
}
43+
44+
public Map<String, Long> listToMapDuplicatedKeyDuplicated() {
45+
List<Hosting> list = getList();
46+
47+
list.add(new Hosting(6, "linode.com", 100000)); // new line
48+
49+
// key = name, value - websites , but the key 'linode' is duplicated!?
50+
Map<String, Long> result1 = list.stream().collect(Collectors.toMap(Hosting::getName, Hosting::getWebsites));
51+
52+
System.out.println("Result 1 : " + result1);
53+
54+
return result1;
55+
}
56+
57+
public Map<String, Long> listToMapDuplicatedKeyMerge() {
58+
List<Hosting> list = getList();
59+
60+
list.add(new Hosting(6, "linode.com", 100000)); // new line
61+
62+
Map<String, Long> result1 = list.stream().collect(
63+
Collectors.toMap(Hosting::getName, Hosting::getWebsites,
64+
(oldValue, newValue) -> oldValue
65+
)
66+
);
67+
68+
System.out.println("Result 1 : " + result1);
69+
70+
return result1;
71+
}
72+
73+
public Map listToMapSortCollect() {
74+
List<Hosting> list = getList();
75+
list.add(new Hosting(6, "linode.com", 100000));
76+
77+
//example 1
78+
Map result1 = list.stream()
79+
.sorted(Comparator.comparingLong(Hosting::getWebsites).reversed())
80+
.collect(
81+
Collectors.toMap(
82+
Hosting::getName, Hosting::getWebsites, // key = name, value = websites
83+
(oldValue, newValue) -> oldValue, // se a mesma chave, pegue a chave antiga
84+
LinkedHashMap::new // returns a LinkedHashMap, keep order
85+
));
86+
87+
System.out.println("Result 1 : " + result1);
88+
89+
return result1;
90+
}
91+
92+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package br.com.somejava8examples.map;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.boot.test.context.SpringBootTest;
6+
import org.springframework.test.context.ActiveProfiles;
7+
8+
import static org.junit.jupiter.api.Assertions.*;
9+
10+
@SpringBootTest
11+
@ActiveProfiles("test")
12+
public class ConvertListToMapTest {
13+
14+
@Autowired
15+
private ConvertListToMapService service;
16+
17+
@Test
18+
public void listToMapCollectorsToMapTest() {
19+
assertFalse(service.listToMapCollectorsToMap().isEmpty(), "List is empty");
20+
}
21+
22+
@Test
23+
public void listToMapDuplicatedKeyDuplicatedTest() {
24+
Exception exception = assertThrows(IllegalStateException.class, () -> {service.listToMapDuplicatedKeyDuplicated();});
25+
26+
String expectedMessage = "Duplicate key linode.com (attempted merging values 90000 and 100000)";
27+
String actualMessage = exception.getMessage();
28+
29+
assertEquals(actualMessage, expectedMessage);
30+
}
31+
32+
@Test
33+
public void listToMapDuplicatedKeyMergeTest() {
34+
assertFalse(service.listToMapDuplicatedKeyMerge().isEmpty(), "List is empty");
35+
}
36+
37+
@Test
38+
public void listToMapSortCollectTest() {
39+
assertFalse(service.listToMapSortCollect().isEmpty(), "List is empty");
40+
}
41+
42+
}

src/test/java/br/com/somejava8examples/stream/StreamHasAlreadyBeenOperatedUponOrClosedTest.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,8 @@ public class StreamHasAlreadyBeenOperatedUponOrClosedTest {
1818
private StreamHasAlreadyBeenOperatedUponOrClosedService service;
1919

2020
@Test
21-
@DisplayName("Test Spring @Autowired Integration")
2221
public void streamIsClosedTest() {
23-
Exception exception = assertThrows(IllegalStateException.class, () -> {
24-
service.streamIsClosed();
25-
});
22+
Exception exception = assertThrows(IllegalStateException.class, () -> {service.streamIsClosed();});
2623

2724
String expectedMessage = "stream has already been operated upon or closed";
2825
String actualMessage = exception.getMessage();

0 commit comments

Comments
 (0)