728x90
반응형
source 는 Github 에 있습니다.
목차는 Java series 에 있습니다.
[Java 7편] stream list to map<String, Dto>
Stream List to Map<String, DTO>
- list 를 Map<String, DTO> 변환하는 예제를 정리했습니다.
Source
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
public class StreamListToMapObjectTest { | |
@Test | |
@DisplayName("list를Map<String, DTO> 변환") | |
public void streamListToMapObjectTest01() throws Exception { | |
List<DtoA> list = new ArrayList<>(); | |
list.add(new DtoA("key1", "bcde")); | |
list.add(new DtoA("key2", "bcde123")); | |
Map<String, DtoB> map = | |
list.stream().map(a -> new DtoB(a.getKey(), a.getValue())) | |
.collect(Collectors.toMap(DtoB::getKey, Function.identity())); | |
Assertions.assertEquals(2, map.keySet().size()); | |
Assertions.assertEquals("bcde", map.get("key1").getValue()); | |
Assertions.assertEquals("key1", map.get("key1").getKey()); | |
} | |
private static class DtoA { | |
private String key; | |
private String value; | |
public DtoA(String key, String value) { | |
this.key = key; | |
this.value = value; | |
} | |
public String getKey() { | |
return key; | |
} | |
public String getValue() { | |
return value; | |
} | |
} | |
private static class DtoB { | |
private String key; | |
private String value; | |
public DtoB(String key, String value) { | |
this.key = key; | |
this.value = value; | |
} | |
public String getKey() { | |
return key; | |
} | |
public String getValue() { | |
return value; | |
} | |
} | |
} |
'Java > series' 카테고리의 다른 글
[Java 10편] Functional Interface 정리 (0) | 2022.12.01 |
---|---|
[Java 9편] 자바 BigDecimal 소수점 계산 주의사항 (0) | 2022.11.19 |
[Java 6편] 절차지향적, 객체지향적 설계 (0) | 2022.08.05 |
[Java 5편] 객체, 엔티티, 테이블 설계 (0) | 2022.08.04 |
[Java 4편] 객체지향, 함수형프로그래밍, 람다식 (0) | 2022.07.27 |
댓글