【问题标题】:General merger of two hashmaps两个hashmap的一般合并
【发布时间】:2011-11-25 23:58:10
【问题描述】:

在 java 中,如果我有两个对象 A 和 B,并且都包含引用 id 的类变量,并且 A 也有一个类变量类型,则 B 有一个类变量位置。我正在尝试构建一个以键为类型、以值为位置的地图。目前我正在通过构建两个单独的地图来做到这一点,一个地图(Map1)将引用ID链接到类型,并通过迭代A类型的对象列表来构建,另一个地图(Map2)将引用ID链接到位置并构建通过遍历类型 B 的对象列表。然后通过遍历 Map1 的 keySet 并找到引用 id 的值,将其作为键放在新映射中,然后获取位置的值,从而合并映射来自 Map2 并将其用作类型的值。实现如下所示。我的问题是:有没有更有效的方法来做这个算法?这似乎不是最好的实现。很抱歉含糊不清 - 希望代码能让问题更清楚。

Map<String, String> referenceIdToType = new HashMap<String, String>();
Map<String, String> referenceIdToLocation = new HashMap<String, String>();

for(Info info : infoList) {
     referenceIdToType.put(info.getReferenceId(), info.getType());
}
for(Location loc : locationList) {
     referenceIdToLocation.put(loc.getReferenceId(), loc.getLocation());
}

Map<String, String> typeToLocation = new HashMap<String, String>();
for(String referenceId : referenceIdToType.keySet()) {
    typeToLocation.put(referenceIdToType.get(referenceId), referenceIdToLocation.get(referenceId));
}

【问题讨论】:

    标签: java hashmap


    【解决方案1】:

    您可以通过删除其中一个 HashMap 对其进行一些优化。您只需要为您的列表之一制作一个 HashMap。然后通过循环第二个列表来构建最终的 HashMap,使用另一个列表的 HasMap 来获取匹配值。

    Map<String, String> referenceIdToLocation = new HashMap<String, String>();
    
    for(Location loc : locationList) {
         referenceIdToLocation.put(loc.getReferenceId(), loc.getLocation());
    }
    
    Map<String, String> typeToLocation = new HashMap<String, String>();
    for(Info info : infoList) {
        typeToLocation.put(info.getType(), referenceIdToLocation.get(info.getReferenceId()));
    }
    

    【讨论】:

      【解决方案2】:

      为什么不直接通过referenceId 查找LocationInfo 对象,然后将它们放入HashMap

      ArrayList<String> referenceIds = //all reference ids;
      
      public Location getLocationByReferenceId(String referenceId)
      {
          for(Location loc : locationList)
          {
              if(loc.getReferenceId().equals(referenceId))
                  return loc;
          }
      }
      
      public Info getInfoByReferenceId(String referenceId)
      {
          for(Info info : infoList)
          {
              if(info.getReferenceId().equals(referenceId))
                  return info;
          }
      }
      

      然后你只需要创建一个地图并调用getType()getLocation()

      Map<String, String> typeToLocation = new HashMap<String, String>();
      
      for(String refID : referenceIds)
      {
         Location loc = getLocationByReferenceId(refID);
         Info    info = getInfoByReferenceId(refID);
      
         typeToLocation.put(info.getType(), loc.getLocation());
      }
      

      我知道这不是您想要的,但我希望它有所帮助。

      【讨论】:

        【解决方案3】:

        我的问题是:有没有更有效的方法来做这个算法?

        我认为没有更有效的方法来执行该操作。对于最终的typeToLocation 映射,我什至想不出更好的表示/实现方式,除非键/值有什么特别之处可以让你走捷径。

        (顺便说一句,我不会将您正在执行的操作称为“合并”。从数学的角度来看,它更像是映射的“组合”,尽管严格来说并非如此。对我来说,“合并”地图只是创建它们条目的联合,这就是我认为你最初的意思......)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-11-11
          • 1970-01-01
          • 1970-01-01
          • 2019-01-13
          • 2021-06-22
          • 2021-05-06
          • 1970-01-01
          相关资源
          最近更新 更多