public static void main(String[] args) {

        List<Data> list = Lists.newArrayList(d("a", 1), d("b", 2), d("a", 3));
        System.out.println("src : "+list);

	    val mm = list.stream().collect(groupingBy(Data::getKey));

        System.out.println("mm : "+mm);
        System.out.println("mm.a : "+mm.get("a"));
        System.out.println("mm.b : "+mm.get("b"));
        System.out.println("mm.keys : "+mm.keySet());
        System.out.println("mm.values : "+mm.values());
    }

    @AllArgsConstructor
    @ToString
    @Getter
    static class Data {
        String key;
        long qty;
        public String toString() {
            return key+"-"+qty;
        }
    }

    private static Data d(String vendorId, int qty) {
        return new Data(vendorId, qty);
    }   


  • result
src : [a-1, b-2, a-3]
mm : {a=[a-1, a-3], b=[b-2]}
mm.a : [a-1, a-3]
mm.b : [b-2]
mm.keys : [a, b]
mm.values : [[a-1, a-3], [b-2]]

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2019-06-11 21:47:07
Processing time 0.0167 sec