【问题标题】:Hashmap within ArrayList in JavaJava中ArrayList中的Hashmap
【发布时间】:2020-05-10 17:11:17
【问题描述】:

ArrayList 中有一个哈希映射。输出如下所示

[{A=2},{A=3},{B=1},{B=4},{A=3}]

下面我提到了我的代码示例

ArrayList<Map<String, Short>> deviceInfo = new ArrayList<>();
Map<String, Integer> rssiMapper = new HashMap<>();

rssiMapper.put(device.getName(), rssi);
deviceInfo.add(rssiMapper);

我想分别取 A 和 B 的平均值。我怎样才能做到这一点

【问题讨论】:

  • 你能分享地图列表的代码吗,你有什么尝试?
  • 更新了一些代码。实际上我正在扫描蓝牙设备并在特定距离内获取设备名称和 RSSI。我想取 RSSI 的平均值,我正在尝试

标签: java arraylist hashmap


【解决方案1】:

像这样试试。

List<Map<String,Integer>> list = List.of(
    Map.of("A", 2),
    Map.of("A", 3),
    Map.of("B", 1),
    Map.of("B", 4),
    Map.of("A", 3));

Map<String, Double> avgs = list.stream()
    .flatMap(m -> m.entrySet().stream())
    .collect(Collectors.groupingBy(
        Entry::getKey,
        Collectors.averagingInt(Entry::getValue)));
System.out.println(avgs);

打印

{A=2.6666666666666665, B=2.5}

正如建议的那样,如果您不熟悉流,这里是一种迭代方法。

Map<String,Double> avgs = new HashMap<>();
Map<String,Integer> count = new HashMap<>();
for (Map<String,Integer> map : list) {
     for (Entry<String,Integer> e : map.entrySet()) {
         String key = e.getKey();
         int value = e.getValue();
         // These just either initialize or update the appropriate
         // values.
         avgs.compute(key, (k,v)-> v == null ? value : v + value);
         count.compute(key, (k,v)->v == null ? 1 : v + 1);
     }
}
// now find the averages.
for(Entry<String,Double> e : avgs.entrySet()) {
    avgs.computeIfPresent(e.getKey(), (k,v)->v/count.get(e.getKey()));
}

System.out.println(avgs);

【讨论】:

  • 应该使用Entry::getKeyEntry::getValue而不是e-&gt;e.getKey()e-&gt;e.getValue()
  • 在生成代码方面真的比另一个更好吗?
  • 这对于随机阅读器来说可能太高级了。在此旁边还提供一个非流解决方案可能会很好。
  • 是的。 e-&gt;e.getKey() 必须在当前类中创建一个以 lambda 表达式作为方法体的隐藏方法。 Entry::getKey 不需要,即更少的代码。
  • @andreas 很高兴知道。我不知道。
【解决方案2】:

您可以创建映射来跟踪条目的sumcountmean(即sum / count),如下所示:

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class Main {
    public static void main(String[] args) {
        List<Map<String, Integer>> list = List.of(Map.of("A", 2), Map.of("A", 3), Map.of("B", 1), Map.of("B", 4),
                Map.of("A", 3));
        Map<String, Integer> sumMap = new HashMap<>();
        Map<String, Integer> countMap = new HashMap<>();
        Map<String, Double> meanMap = new HashMap<>();
        for (Map<String, Integer> map : list) {
            for (Entry<String, Integer> entry : map.entrySet()) {
                sumMap.put(entry.getKey(), sumMap.getOrDefault(entry.getKey(), 0) + entry.getValue());
                countMap.put(entry.getKey(), countMap.getOrDefault(entry.getKey(), 0) + 1);
                meanMap.put(entry.getKey(),
                        (double) sumMap.getOrDefault(entry.getKey(), 0) / countMap.getOrDefault(entry.getKey(), 1));
            }
        }

        // Display
        System.out.println(meanMap);
    }
}

输出:

{A=2.6666666666666665, B=2.5}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-24
    • 1970-01-01
    • 1970-01-01
    • 2013-12-10
    • 1970-01-01
    • 2016-07-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多