首页 > 解决方案 > 合并映射中键包含的值(Java)

问题描述

final Multimap<String, Map.Entry<Integer, Integer>>[] actionMap = new Multimap[]{null};
final boolean[] loaded = {false};

db.execute(connection -> {
    PreparedStatement statement = null;
    ResultSet resultSet = null;

    actionMap[0] = ArrayListMultimap.create();
    try {
        statement = connection.prepareStatement("Blah Blah...
        while (resultSet.next()) {
            final String name = ...

            actionMap[0].put(name, new AbstractMap.SimpleEntry<>(int1, int2));

我有一张地图,SimpleEntry用于插入两个整数值(int1,int2)。在重复键上,我想合并已经映射的值。我的想法是computeIfPresent,但我不知道 BiFunctions 因为我AbstractMap.SimpleEntry用来输入两个值。任何帮助将非常感激。

标签: javaguava

解决方案


在放置和覆盖之前,检索现有值。

  • 如果您得到 null,则没有这样的值,并且您按照最初的预期放置。
  • 如果你得到一些东西,将你的新数据合并到一些东西中。

推荐阅读