问题描述
将获取到的product集合中的每一个产品的categoryType属性取出来作为一个集合
问题解决
List<Product> productList = productService.findUpAll();
List<Integer> typeList = productList.stream()
.map(Product::getCategoryType)
.collect(Collectors.toList());
-
productList:这是一个包含Product对象的列表,假设每个Product对象都有一个getCategoryType方法用于获取其categoryType属性。
-
.stream():这将productList转换为一个Stream对象,使你可以使用Stream API进行操作。
-
.map(Product::getCategoryType):这是一个中间操作,它使用Product::getCategoryType方法引用来提取每个产品的categoryType属性,并将其转换为一个新的Stream对象。
-
.collect(Collectors.toList()):这是一个终端操作,它将最终的结果收集到一个列表中。Collectors.toList()是一个收集器,它将Stream中的元素收集到一个新的List对象中。