forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request eugenp#7040 from rodrigolgraciano/BAEL-2936
BAEL-2936
- Loading branch information
Showing
3 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
java-collections-conversions/src/main/java/com/baeldung/convertToMap/Book.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.baeldung.convertToMap; | ||
|
||
public class Book { | ||
private String name; | ||
private int releaseYear; | ||
private String isbn; | ||
|
||
|
||
@Override | ||
public String toString() { | ||
return "Book{" + | ||
"name='" + name + '\'' + | ||
", releaseYear=" + releaseYear + | ||
", isbn='" + isbn + '\'' + | ||
'}'; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public int getReleaseYear() { | ||
return releaseYear; | ||
} | ||
|
||
public void setReleaseYear(int releaseYear) { | ||
this.releaseYear = releaseYear; | ||
} | ||
|
||
public String getIsbn() { | ||
return isbn; | ||
} | ||
|
||
public void setIsbn(String isbn) { | ||
this.isbn = isbn; | ||
} | ||
|
||
public Book(String name, int releaseYear, String isbn) { | ||
this.name = name; | ||
this.releaseYear = releaseYear; | ||
this.isbn = isbn; | ||
} | ||
} | ||
|
34 changes: 34 additions & 0 deletions
34
java-collections-conversions/src/main/java/com/baeldung/convertToMap/ConvertToMap.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.baeldung.convertToMap; | ||
|
||
import java.util.*; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
|
||
public class ConvertToMap { | ||
public Map<String, String> listToMap(List<Book> books) { | ||
return books.stream().collect(Collectors.toMap(Book::getIsbn, Book::getName)); | ||
} | ||
|
||
public Map<Integer, Book> listToMapWithDupKeyError(List<Book> books) { | ||
return books.stream().collect(Collectors.toMap(Book::getReleaseYear, Function.identity())); | ||
} | ||
|
||
public Map<Integer, Book> listToMapWithDupKey(List<Book> books) { | ||
return books.stream().collect(Collectors.toMap(Book::getReleaseYear, Function.identity(), | ||
(o1, o2) -> o1)); | ||
} | ||
|
||
public Map<Integer, Book> listToConcurrentMap(List<Book> books) { | ||
return books.stream().collect(Collectors.toMap(Book::getReleaseYear, Function.identity(), (o1, o2) -> o1, ConcurrentHashMap::new)); | ||
} | ||
|
||
public TreeMap<String, Book> listToSortedMap(List<Book> books) { | ||
return books.stream() | ||
.sorted(Comparator.comparing(Book::getName)) | ||
.collect(Collectors.toMap(Book::getName, Function.identity(), (o1, o2) -> o1, TreeMap::new)); | ||
} | ||
|
||
|
||
} | ||
|
50 changes: 50 additions & 0 deletions
50
...collections-conversions/src/test/java/com/baeldung/convertToMap/ConvertToMapUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.baeldung.convertToMap; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
|
||
public class ConvertToMapUnitTest { | ||
|
||
private List<Book> bookList; | ||
private ConvertToMap convertToMap = new ConvertToMap(); | ||
|
||
@Before | ||
public void init() { | ||
bookList = new ArrayList<>(); | ||
bookList.add(new Book("The Fellowship of the Ring", 1954, "0395489318")); | ||
bookList.add(new Book("The Two Towers", 1954, "0345339711")); | ||
bookList.add(new Book("The Return of the King", 1955, "0618129111")); | ||
} | ||
|
||
@Test | ||
public void whenConvertFromListToMap() { | ||
assertTrue(convertToMap.listToMap(bookList).size() == 3); | ||
} | ||
|
||
@Test(expected = IllegalStateException.class) | ||
public void whenMapHasDuplicateKey_without_merge_function_then_runtime_exception() { | ||
convertToMap.listToMapWithDupKeyError(bookList); | ||
} | ||
|
||
@Test | ||
public void whenMapHasDuplicateKey_with_merge_function() { | ||
assertTrue(convertToMap.listToMapWithDupKey(bookList).size() == 2); | ||
} | ||
|
||
@Test | ||
public void whenCreateConcurrentHashMap() { | ||
assertTrue(convertToMap.listToConcurrentMap(bookList) instanceof ConcurrentHashMap); | ||
} | ||
|
||
@Test | ||
public void whenMapisSorted() { | ||
assertTrue(convertToMap.listToSortedMap(bookList).firstKey().equals("The Fellowship of the Ring")); | ||
} | ||
} |