public class Apple { private Integer id; private String name; private BigDecimal money; private Integer num; /*構造函數*/ }
List<Apple> appleList = new ArrayList<>();//存放apple對象集合 Apple apple1 = new Apple(1,"蘋果1",new BigDecimal("3.25"),10); Apple apple12 = new Apple(1,"蘋果2",new BigDecimal("1.35"),20); Apple apple2 = new Apple(2,"香蕉",new BigDecimal("2.89"),30); Apple apple3 = new Apple(3,"荔枝",new BigDecimal("9.99"),40); appleList.add(apple1); appleList.add(apple12); appleList.add(apple2); appleList.add(apple3);
Map<Integer, Apple> appleMap = appleList.stream().collect(Collectors.toMap(Apple::getId, a -> a,(k1,k2)->k1));
做者:Tommmmm
連接:https://www.jianshu.com/p/ae14101989f2
來源:簡書
簡書著做權歸做者全部,任何形式的轉載都請聯繫做者得到受權並註明出處。app