Skip to content

Commit e4e4a2b

Browse files
BAEL-2936
1 parent ebd2d59 commit e4e4a2b

3 files changed

Lines changed: 132 additions & 0 deletions

File tree

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.baeldung.convertToMap;
2+
3+
public class Book {
4+
private String name;
5+
private int releaseYear;
6+
private String isbn;
7+
8+
9+
@Override
10+
public String toString() {
11+
return "Book{" +
12+
"name='" + name + '\'' +
13+
", releaseYear=" + releaseYear +
14+
", isbn='" + isbn + '\'' +
15+
'}';
16+
}
17+
18+
public String getName() {
19+
return name;
20+
}
21+
22+
public void setName(String name) {
23+
this.name = name;
24+
}
25+
26+
public int getReleaseYear() {
27+
return releaseYear;
28+
}
29+
30+
public void setReleaseYear(int releaseYear) {
31+
this.releaseYear = releaseYear;
32+
}
33+
34+
public String getIsbn() {
35+
return isbn;
36+
}
37+
38+
public void setIsbn(String isbn) {
39+
this.isbn = isbn;
40+
}
41+
42+
public Book(String name, int releaseYear, String isbn) {
43+
this.name = name;
44+
this.releaseYear = releaseYear;
45+
this.isbn = isbn;
46+
}
47+
}
48+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.baeldung.convertToMap;
2+
3+
import java.util.*;
4+
import java.util.concurrent.ConcurrentHashMap;
5+
import java.util.function.Function;
6+
import java.util.stream.Collectors;
7+
8+
public class ConvertToMap {
9+
public Map<String, String> listToMap(List<Book> books) {
10+
return books.stream().collect(Collectors.toMap(Book::getIsbn, Book::getName));
11+
}
12+
13+
public Map<Integer, Book> listToMapWithDupKeyError(List<Book> books) {
14+
return books.stream().collect(Collectors.toMap(Book::getReleaseYear, Function.identity()));
15+
}
16+
17+
public Map<Integer, Book> listToMapWithDupKey(List<Book> books) {
18+
return books.stream().collect(Collectors.toMap(Book::getReleaseYear, Function.identity(),
19+
(o1, o2) -> o1));
20+
}
21+
22+
public Map<Integer, Book> listToConcurrentMap(List<Book> books) {
23+
return books.stream().collect(Collectors.toMap(Book::getReleaseYear, Function.identity(), (o1, o2) -> o1, ConcurrentHashMap::new));
24+
}
25+
26+
public TreeMap<String, Book> listToSortedMap(List<Book> books) {
27+
return books.stream()
28+
.sorted(Comparator.comparing(Book::getName))
29+
.collect(Collectors.toMap(Book::getName, Function.identity(), (o1, o2) -> o1, TreeMap::new));
30+
}
31+
32+
33+
}
34+
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.baeldung.convertToMap;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.concurrent.ConcurrentHashMap;
6+
7+
import static org.junit.Assert.*;
8+
9+
import org.junit.Before;
10+
import org.junit.Test;
11+
12+
13+
public class ConvertToMapUnitTest {
14+
15+
private List<Book> bookList;
16+
private ConvertToMap convertToMap = new ConvertToMap();
17+
18+
@Before
19+
public void init() {
20+
bookList = new ArrayList<>();
21+
bookList.add(new Book("The Fellowship of the Ring", 1954, "0395489318"));
22+
bookList.add(new Book("The Two Towers", 1954, "0345339711"));
23+
bookList.add(new Book("The Return of the King", 1955, "0618129111"));
24+
}
25+
26+
@Test
27+
public void whenConvertFromListToMap() {
28+
assertTrue(convertToMap.listToMap(bookList).size() == 3);
29+
}
30+
31+
@Test(expected = IllegalStateException.class)
32+
public void whenMapHasDuplicateKey_without_merge_function_then_runtime_exception() {
33+
convertToMap.listToMapWithDupKeyError(bookList);
34+
}
35+
36+
@Test
37+
public void whenMapHasDuplicateKey_with_merge_function() {
38+
assertTrue(convertToMap.listToMapWithDupKey(bookList).size() == 2);
39+
}
40+
41+
@Test
42+
public void whenCreateConcurrentHashMap() {
43+
assertTrue(convertToMap.listToConcurrentMap(bookList) instanceof ConcurrentHashMap);
44+
}
45+
46+
@Test
47+
public void whenMapisSorted() {
48+
assertTrue(convertToMap.listToSortedMap(bookList).firstKey().equals("The Fellowship of the Ring"));
49+
}
50+
}

0 commit comments

Comments
 (0)