链接
    public class ListGroupDemo {

    1. static class Product {<br /> private long id; // 商品Id<br /> private String name;// 商品名称<br /> private byte type;// 商品类型
    2. public Product(long id, String name, byte type) {<br /> this.id = id;<br /> this.name = name;<br /> this.type = type;<br /> }<br /> <br /> ...<br /> <br /> @Override<br /> public String toString() {<br /> return "Product{" +<br /> "id=" + id +<br /> ", name='" + name + '\'' +<br /> ", type=" + type +<br /> '}';<br /> }<br /> }
    3. public static void main(String[] args) {<br /> // 元数据<br /> Product product1 = new Product(1001L, "Java", (byte) 10);<br /> Product product2 = new Product(1002L, "Python", (byte) 20);<br /> Product product3 = new Product(1002L, "Python", (byte) 20);<br /> Product product4 = new Product(1003L, "Netty", (byte) 30);<br /> Product product5 = new Product(1003L, "Netty", (byte) 30);<br /> Product product6 = new Product(1003L, "Netty", (byte) 30);<br /> Product product7 = new Product(1002L, "Python", (byte) 20);<br /> Product product8 = new Product(1004L, "Tomcat", (byte) 40);<br /> // 存入一个List<br /> List<Product> products = Arrays.asList(new Product[]{product1, product2, product3, product4, product5, product6, product7, product8});
    4. // 以id进行分组<br /> Map<Long, List<Product>> map = new ConcurrentHashMap<>();<br /> for (int i = 0; i < products.size(); i++) {<br /> List<Product> productsTemp = map.get(products.get(i).getId());<br /> if (productsTemp == null) {<br /> productsTemp = new ArrayList<>();<br /> productsTemp.add(products.get(i));<br /> map.put(products.get(i).getId(), productsTemp);<br /> } else {<br /> productsTemp.add(products.get(i));<br /> }<br /> }<br /> // 遍历输出<br /> for (Long id : map.keySet()) {<br /> System.out.println(map.get(id));<br /> }<br /> }<br />}