在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Java/ 【Java8求助】 一個(gè)有關(guān)List<Map>對(duì)象進(jìn)行flatMap

【Java8求助】 一個(gè)有關(guān)List<Map>對(duì)象進(jìn)行flatMap()方法而后分組的問題

問題描述:有個(gè)List對(duì)象,如下:
List<Map<String,Object>> query = new ArrayList<Map<String,Object>>
Json視圖下的例子數(shù)據(jù)為:
[

{
    "customer_attribute": "1",
    "customer_attribute_no": "0101019",
    "group_no": 1
},
{
    "customer_attribute": "2",
    "customer_attribute_no": "A001",
    "group_no": 1
},
{
    "customer_attribute": "3",
    "customer_attribute_no": "AA001",
    "group_no": 2
}

]
現(xiàn)在需求是將原List中數(shù)據(jù)構(gòu)建成:
[

{
    "attributelist": [
        {
            "customer_attribute": "1",
            "customer_attribute_no": "0101019"
        },
        {
            "customer_attribute": "2",
            "customer_attribute_no": "A001"
        }
    ],
    "group_no": 1
},
{
    "attributelist": [
        {
            "customer_attribute": "3",
            "customer_attribute_no": "AA001"
        }
    ],
    "group_no": 2
}

],
我的想法是將原來的List中的多個(gè)Map先用stream中的flatMap()合成一個(gè)Map,讓后對(duì)這個(gè)Map中以“group_no”為key進(jìn)行分組,分組后將各自新Map中的“customer_attribute”構(gòu)建新的key為“attributelist”的List,從而實(shí)現(xiàn)。但我太清楚具體該如何編寫代碼,有前輩可以指導(dǎo)寫一下參考嗎?萬分感激!

回答
編輯回答
孤島

分組可以用GroupingBy方法,以下是參考代碼

List<Map<String, Object>> result = query.stream().collect(Collectors.groupingBy(e -> e.get("group_no")))
                .entrySet().stream().map(e -> {
                    Map<String, Object> map = new HashMap<>();
                    map.put("attributelist", e.getValue());
                    map.put("group_no", e.getKey());
                    return map;
                }).collect(Collectors.toList());
2018年4月4日 15:49
編輯回答
落殤

我java8的stream也不是很熟,這樣能滿足要求,但是不知道還有沒有更簡(jiǎn)單的寫法。

// init data
List<Map<String,Object>> query = new ArrayList<>();
HashMap<String, Object> e1 = new HashMap<>();
e1.put("group_no",1);
e1.put("customer_attribute","1");
e1.put("customer_attribute_no","0101019");
query.add(e1);

HashMap<String, Object> e2 = new HashMap<>();
e2.put("group_no",1);
e2.put("customer_attribute","2");
e2.put("customer_attribute_no","A001");
query.add(e2);

HashMap<String, Object> e3 = new HashMap<>();
e3.put("group_no",2);
e3.put("customer_attribute","3");
e3.put("customer_attribute_no","A002");
query.add(e3);

// stream
List<HashMap<String, Object>> group_no = query.stream()
        .collect(Collectors.groupingBy(q -> q.get("group_no")))
        .entrySet()
        .stream()
        .map(e -> {
            HashMap<String, Object> inner = new HashMap<>();
            List<Map<String, Object>> newList = e.getValue().stream().map(v -> {
                v.remove("group_no");
                return v;
            }).collect(Collectors.toList());
            inner.put("attributelist", newList);
            inner.put("group_no",e.getKey());
            return inner;
        }).collect(Collectors.toList());

System.out.println(JSON.toJSONString(group_no));
2017年5月26日 12:02