totalPrice
필드가 ProductInventory
으로 선언 된 경우 다음을 수행 할 수 있습니다.
public class Product {
String type;
Integer price;
// getters, setters, constructors
}
및
public class Inventory {
Product product;
String country;
Integer quantity;
Integer totalPrice;
// getters, setters, and constructors
}
결과 값이 어느 쪽 Optional.empty()
, 또는 당신이 최종 엔터티 형식으로 결과 값이있을 것이다, 나는 마지막 map to new object (country, quantity remaining, total price)
을 생략
private Optional<FinalEntity> doLogic(String inputCountry, String inputType, Integer inputQuantity, Integer inputTax) {
return Stream.of(new Inventory(new Product("cola", 15), "germany", 1000))
.filter(inv -> inv.getProduct().getType().equals(inputType) && inv.getQuantity() > inputQuantity)
.peek(inv -> {
Integer tax = inv.getCountry().equals(inputCountry) ? 0 : inputTax;
inv.setTotalPrice((inv.getProduct().getPrice() * inputQuantity) + tax);
})
.sorted(Comparator.comparing(Inventory::getTotalPrice))
.findFirst()
.map(Util::mapToFinalEntity);
}
이있는 인 그 시점에서 간단한 단계.
이 필드를 Inventory
에 넣고 싶지 않은 경우 totalPrice
을 포함하는 래퍼 클래스를 만들고 스트림 시작 부분의 인벤토리에 매핑 할 수 있습니다.
당신은'totalPrice' 값을 어디서나 저장할 수 없습니다. 왜 그 값을'ProductInventory'에 추가하지 않으시겠습니까? –
래퍼 클래스를 사용하셨습니까? 또는 인벤토리 클래스에 직접 삽입 되었습니까? –
productInventory가 다른 목적을 가지고 있기 때문에 래퍼 클래스에 삽입하려고합니다. – user1298426