【问题标题】:Can we put hash tables inside a hash table?我们可以将哈希表放在哈希表中吗?
【发布时间】:2015-02-06 09:29:18
【问题描述】:

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

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

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

【问题讨论】:

  • 你真的试过了吗? :) 那你就不用问了。是的,你当然可以。
  • 你的意思是HashTable<String, HashTable<String, String>>?是的,为什么不呢。
  • 我试过了,它奏效了,但由于内存浪费,我不确定这是否是有效的方法。我只需要你们的建议。
  • 您可以将任何对象放入哈希表。哈希表也是一个对象:P.
  • @malit.tilak 你有多少内存,需要加载多少?即少量浪费是个问题吗?如果您想要 2D 地图,为什么不拥有 Map<Pair<Integer, Integer>, ValueType>

标签: java hashtable spreadsheet


【解决方案1】:

怎么样

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>();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-19
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-12
    • 2010-12-22
    • 2023-03-27
    相关资源
    最近更新 更多