DTO를 타입으로 갖는 리스트에서 String 타입의 리스트로 변환하는 예제입니다.
CoffeeDTO.java
package com.java.dto;
public class CoffeeDTO {
private String name;
private int price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public CoffeeDTO(String name, int price) {
this.name = name;
this.price = price;
}
}
MyMap.java
package com.java.stream;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import com.java.dto.CoffeeDTO;
public class MyMap {
public static void listToString() {
List<CoffeeDTO> menuList = Arrays.asList(
new CoffeeDTO("아메리카노", 5000),
new CoffeeDTO("카페라떼", 6000),
new CoffeeDTO("바닐라라떼", 6500)
);
List<String> result = menuList.stream()
.map(CoffeeDTO::getName)
.collect(Collectors.toList());
System.out.println(result);
}
public static void main(String[] args) {
listToString();
}
}