最近在學習的時候接觸到了lambda表達式,功能是從商品列表中,獲取類目類型列表數據
常規的Java代碼:
List<Integer> categoryTypeList = new ArrayList<>();
for (ProductInfo productInfo : productInfoList) {
categoryTypeList.add(productInfo.getCategoryType());
}
使用lambda表達式的寫法:
List<Integer> categoryTypeList = productInfoList.stream()
.map(e -> e.getCategoryType())
.collect(Collectors.toList());
通過這個案例,來學一下lambda表達式結合stream的語法。