lambda表達式+stream()使用例子

最近在學習的時候接觸到了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的語法。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章