我们可以将哈希表放在哈希表中吗?

时间:2015-02-06 09:29:18

标签: java hashtable spreadsheet

我们可以在哈希表中为每个键链接另一个哈希表吗?

我的目标是拥有一个非常快速的二维数据结构来存储电子表格中的单元格。

我会在哈希表中存储“一行中的所有列”。然后对所有“r”行进行此操作。接下来为行创建最终哈希表,并在新哈希表中存储所有'r'个哈希表。这种方式有效吗?还是有更好的方法?

1 个答案:

答案 0 :(得分:3)

怎么样

Map<String, Map<Integer,Integer>> asdf = new HashMap<String, Map<Integer, Integer>>();

但说实话,你应该把它包装在一个对象里面。在这种结构中,全球将非常不方便。

你可以尝试这个BiHashMap

public class BiHashMap<K1, K2, V> {

private final Map<K1, Map<K2, V>> mMap;

public BiHashMap() {
    mMap = new HashMap<K1, Map<K2, V>>();
}

/**
 * Associates the specified value with the specified keys in this map (optional operation). If the map previously
 * contained a mapping for the key, the old value is replaced by the specified value.
 * 
 * @param key1
 *            the first key
 * @param key2
 *            the second key
 * @param value
 *            the value to be set
 * @return the value previously associated with (key1,key2), or <code>null</code> if none
 * @see Map#put(Object, Object)
 */
public V put(K1 key1, K2 key2, V value) {
    Map<K2, V> map;
    if (mMap.containsKey(key1)) {
        map = mMap.get(key1);
    } else {
        map = new HashMap<K2, V>();
        mMap.put(key1, map);
    }

    return map.put(key2, value);
}

/**
 * Returns the value to which the specified key is mapped, or <code>null</code> if this map contains no mapping for
 * the key.
 * 
 * @param key1
 *            the first key whose associated value is to be returned
 * @param key2
 *            the second key whose associated value is to be returned
 * @return the value to which the specified key is mapped, or <code>null</code> if this map contains no mapping for
 *         the key
 * @see Map#get(Object)
 */
public V get(K1 key1, K2 key2) {
    if (mMap.containsKey(key1)) {
        return mMap.get(key1).get(key2);
    } else {
        return null;
    }
}

/**
 * Returns <code>true</code> if this map contains a mapping for the specified key
 * 
 * @param key1
 *            the first key whose presence in this map is to be tested
 * @param key2
 *            the second key whose presence in this map is to be tested
 * @return Returns true if this map contains a mapping for the specified key
 * @see Map#containsKey(Object)
 */
public boolean containsKeys(K1 key1, K2 key2) {
    return mMap.containsKey(key1) && mMap.get(key1).containsKey(key2);
}

public void clear() {
    mMap.clear();
}

}

然后创建使用它:

BiHashMap<String,String,String> bigBoard = new BiHashMap<String,String,String>();