本文介绍了我怎样才能制作一个有两个索引的地图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  Map< String index1,Map< String index 2,Object obj> > map = new HashMap<>(); 

我想让我的 Object 使用 index1 index2 作为查找地图。 >解决方案

最简单的方法是使用Guava的,如果您愿意使用第三方库。



它的工作原理是这样的:
$ b $ pre $ 表< String,String,Object> table = HashBasedTable.create();
table.put(index1,index2,obj);
对象retrieveObject = table.get(index1,index2);

您可以按照以下说明将其添加到您的项目中:






如果你不想使用番石榴,你会遇到很大的问题。如果尝试使用新的第一个键插入元素,则必须确保内部映射已经存在。这意味着,每次执行 put 时,都必须检索 innerMap ,看看它是否存在,然后创建它如果没有。 每次调用 Map.put 时都必须执行此操作。另外,您可能会抛出 NullPointerException 如果在内部地图上调用 get 时内层地图不存在。



如果你应该这样做,应该在外部类中包装 Map< String,Map< String,Object> 来管理这些问题,或者使用Java 8的 computeIfAbsent ,但最简单的方法就是像上面那样使用



如果你使用自己的类来代替 Table ,它会是这样的:

  public class DoubleMap  private final Map< R,Map< C,V>> backingMap; 

public DoubleMap(){
this.backingMap = new HashMap<>();
}

public V get(R row,C column){
Map< C,V> innerMap = backingMap.get(row);
if(map == null)return null;
else返回innerMap.get(column);
}

public void put(R row,C column,V value){
Map< C,V> innerMap = backingMap.get(row);
if(innerMap == null){
innerMap = new HashMap< C,V>();
backingMap.put(row,innerMap);
}
innerMap.put(column,value);


$ / code $ / pre

你可以这样做:

  DoubleMap< String,String,Object> map = new DoubleMap(); 

请注意,此答案比Guava版本的功能要少很多。


I have one Map in java like this:

Map<String index1, Map<String index 2, Object obj>> map = new HashMap<>();

I want to get my Object in the map by using index1 and index2 as lookups.

解决方案

The easiest way to do this would be to use Guava's Table, if you're willing to use a third party library.

It works like this:

Table<String, String, Object> table = HashBasedTable.create();
table.put(index1, index2, obj);
Object retrievedObject = table.get(index1, index2);

You can add it to your project by following these instructions: How to add Guava to Eclipse project


If you don't want to use Guava, you have a big problem. If you try to insert an element with new first key, you have to make sure the innermap already exists. This means, every time you do put, you have to retrieve the innerMap, see if it exists, and then create it if it does not. You will have to do this every time you call Map.put. Also, you risk throwing a NullPointerException if the inner map doesn't exist when you call get on the inner map.

If you do this, should wrap your Map<String, Map<String, Object> in an outer class to manage these problems, or use Java 8's computeIfAbsent. But the easiest way is to just use Table as above.

If you make your own class to use instead of Table, it would be something like:

public class DoubleMap<R, C, V> {
  private final Map<R, Map<C, V>> backingMap;

  public DoubleMap() {
    this.backingMap = new HashMap<>();
  }

  public V get(R row, C column) {
    Map<C, V> innerMap = backingMap.get(row);
    if(map == null) return null;
    else return innerMap.get(column);
  }

  public void put(R row, C column, V value) {
    Map<C, V> innerMap = backingMap.get(row);
    if(innerMap == null) {
      innerMap = new HashMap<C, V>();
      backingMap.put(row, innerMap);
    }
    innerMap.put(column, value);
  }
}

You would use this class by doing:

DoubleMap<String, String, Object> map = new DoubleMap();

Note that this answer has a lot less features than the Guava version.

这篇关于我怎样才能制作一个有两个索引的地图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 19:58