10月16 JDK1.8新特性函数式编程
public static void main(String[] args) { List<String> alpha = Arrays.asList("a","b","c"); List<String> collect = alpha.stream().map(String::toUpperCase).collect(Collectors.toList()); for (String str : collect) { System.out.println(str); } // A B C List<Integer> num = Arrays.asList(2,4,5); List<Integer> coll1 = num.stream().map(n -> n*2).collect(Collectors.toList()); for (Integer i : coll1) { System.out.println(i); } // 4 8 10 //1. 查询所有在架的商品 List<ProductInfo> productInfoList = new ArrayList<>(); ProductInfo info1 = new ProductInfo(); info1.setCategoryType(1); info1.setProductName("name1"); ProductInfo info2 = new ProductInfo(); info2.setCategoryType(1); info2.setProductName("name2"); ProductInfo info3 = new ProductInfo(); info3.setCategoryType(2); info3.setProductName("name3"); productInfoList.add(info1); productInfoList.add(info2); productInfoList.add(info3); //2. 获取类目type列表 List<Integer> categoryTypeList = productInfoList.stream() .map(ProductInfo::getCategoryType) .collect(Collectors.toList()); for (Integer integer : categoryTypeList) { System.out.println(integer); } // 1 1 2 就是将type 放到集合里 }