Java 列表到地图的转换 List List=jtemplate.queryForList(sql,rmid);

Java 列表到地图的转换 List List=jtemplate.queryForList(sql,rmid);,java,list,dictionary,collections,Java,List,Dictionary,Collections,上述语句返回以下列表: [{date_part=3, count=1}, {date_part=11, count=1}, {date_part=10, count=2}] 现在,我希望输出是 {3=1, 11=1, 10=2} {3=1, 11=1, 10=2} 我只想提取date_部分的值,并将其存储为地图中的键,然后计算为地图的值。 我试过这样做,但没有达到预期的效果 Map hm2=newhashmap(); list.stream().flatMap(map->map.entry

上述语句返回以下列表:

[{date_part=3, count=1}, {date_part=11, count=1}, {date_part=10, count=2}]
现在,我希望输出是

{3=1, 11=1, 10=2}
{3=1, 11=1, 10=2}
我只想提取date_部分的值,并将其存储为地图中的键,然后计算为地图的值。 我试过这样做,但没有达到预期的效果

Map hm2=newhashmap();
list.stream().flatMap(map->map.entrySet().stream())
.forEach(entry->hm2.put(entry.getKey(),entry.getValue());
系统输出打印项次(hm2);
上述代码的输出为:

{count=1, date_part=11}

您可以使用
Collectors.toMap
作为地图进行收集:

Map res=list.stream()
.collect(collector.toMap)(
e->e.get(“日期/部件”),
e->e.get(“计数”);

您可以使用
收集器.toMap
作为地图进行收集:

Map res=list.stream()
.collect(collector.toMap)(
e->e.get(“日期/部件”),
e->e.get(“计数”);

如果键的顺序无关紧要,您可以使用
groupingBy
summingit
将列表上的流收集到
地图中,如下所示:

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
    
public class Main {
    public static void main(String[] args) {
        List<Map<String, Object>> list = 
                List.of(Map.of("date_part", 3, "count", 1),
                        Map.of("date_part", 11, "count", 1), 
                        Map.of("date_part", 10, "count", 2));

        Map<Integer, Integer> map = 
                
        list.stream()
            .collect(Collectors.groupingBy(e -> (Integer) e.get("date_part"),
                                    Collectors.summingInt(e -> (Integer) e.get("count"))));

        System.out.println(map);
    }
}
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class Main {
    public static void main(String[] args) {
        List<Map<String, Object>> list = 
                List.of(Map.of("date_part", 3, "count", 1),
                        Map.of("date_part", 11, "count", 1), 
                        Map.of("date_part", 10, "count", 2));

        Map<Integer, Integer> map = 
                
        list.stream()
            .collect(Collectors.toMap(e -> (Integer) e.get("date_part"),
                                        e -> (Integer) e.get("count"),
                                        (e1, e2) -> (Integer) e1 + (Integer) e2,
                                        LinkedHashMap::new));

        System.out.println(map);
    }
}
如果要保留键的顺序,可以按如下所示使用:

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
    
public class Main {
    public static void main(String[] args) {
        List<Map<String, Object>> list = 
                List.of(Map.of("date_part", 3, "count", 1),
                        Map.of("date_part", 11, "count", 1), 
                        Map.of("date_part", 10, "count", 2));

        Map<Integer, Integer> map = 
                
        list.stream()
            .collect(Collectors.groupingBy(e -> (Integer) e.get("date_part"),
                                    Collectors.summingInt(e -> (Integer) e.get("count"))));

        System.out.println(map);
    }
}
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class Main {
    public static void main(String[] args) {
        List<Map<String, Object>> list = 
                List.of(Map.of("date_part", 3, "count", 1),
                        Map.of("date_part", 11, "count", 1), 
                        Map.of("date_part", 10, "count", 2));

        Map<Integer, Integer> map = 
                
        list.stream()
            .collect(Collectors.toMap(e -> (Integer) e.get("date_part"),
                                        e -> (Integer) e.get("count"),
                                        (e1, e2) -> (Integer) e1 + (Integer) e2,
                                        LinkedHashMap::new));

        System.out.println(map);
    }
}

如果键的顺序无关紧要,您可以使用
groupingBy
summingit
将列表上的流收集到
映射中,如下所示:

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
    
public class Main {
    public static void main(String[] args) {
        List<Map<String, Object>> list = 
                List.of(Map.of("date_part", 3, "count", 1),
                        Map.of("date_part", 11, "count", 1), 
                        Map.of("date_part", 10, "count", 2));

        Map<Integer, Integer> map = 
                
        list.stream()
            .collect(Collectors.groupingBy(e -> (Integer) e.get("date_part"),
                                    Collectors.summingInt(e -> (Integer) e.get("count"))));

        System.out.println(map);
    }
}
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class Main {
    public static void main(String[] args) {
        List<Map<String, Object>> list = 
                List.of(Map.of("date_part", 3, "count", 1),
                        Map.of("date_part", 11, "count", 1), 
                        Map.of("date_part", 10, "count", 2));

        Map<Integer, Integer> map = 
                
        list.stream()
            .collect(Collectors.toMap(e -> (Integer) e.get("date_part"),
                                        e -> (Integer) e.get("count"),
                                        (e1, e2) -> (Integer) e1 + (Integer) e2,
                                        LinkedHashMap::new));

        System.out.println(map);
    }
}
如果要保留键的顺序,可以按如下所示使用:

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
    
public class Main {
    public static void main(String[] args) {
        List<Map<String, Object>> list = 
                List.of(Map.of("date_part", 3, "count", 1),
                        Map.of("date_part", 11, "count", 1), 
                        Map.of("date_part", 10, "count", 2));

        Map<Integer, Integer> map = 
                
        list.stream()
            .collect(Collectors.groupingBy(e -> (Integer) e.get("date_part"),
                                    Collectors.summingInt(e -> (Integer) e.get("count"))));

        System.out.println(map);
    }
}
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class Main {
    public static void main(String[] args) {
        List<Map<String, Object>> list = 
                List.of(Map.of("date_part", 3, "count", 1),
                        Map.of("date_part", 11, "count", 1), 
                        Map.of("date_part", 10, "count", 2));

        Map<Integer, Integer> map = 
                
        list.stream()
            .collect(Collectors.toMap(e -> (Integer) e.get("date_part"),
                                        e -> (Integer) e.get("count"),
                                        (e1, e2) -> (Integer) e1 + (Integer) e2,
                                        LinkedHashMap::new));

        System.out.println(map);
    }
}