commit
68bb07af20
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
@ -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));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -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"));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue