gpt4 book ai didi

java - 具有复合键和分层默认值的集合

转载 作者:行者123 更新时间:2023-11-30 09:00:49 25 4
gpt4 key购买 nike

我想根据键存储值。

键可以是复合的,每个键最多有两个组件。

我要 map

Compound key {A,null} to Value 1

此值 1 将是所有以 A 作为其第一个组件的键查找的默认值,除非已将完全匹配的键添加到映射中。

所以如果我添加到 map

Compound key {A,Z} having Value 2

当我进行查找时,我希望所有类型为 {A,*} 的查找都返回 1,因此 {A,F} 返回 1 作为它没有指定,所以回退到默认值。异常(exception)情况是 {A,Z},它返回明确指定的 2。

我可以从第一原则开始,通过在检查一个组件匹配之前检查是否存在精确的键(两个组件)匹配。

但是,是否存在可以为我执行此操作的现有集合?

如果我有任意数量的组件怎么办?

最佳答案

“如果我有任意数量的组件怎么办?”

然后抑制一些警告,并使用隐藏的原始 map 去老派。

import java.util.HashMap;
import java.util.Map;


/**
* Map lookup with arbitrary number of keys, as set with first use of lay()
* Missing keys map to null if null key exists
*/
public class MultiKeyMap<K, V>
{
int expectedNumberOfKeys = -1;
V value;

@SuppressWarnings("rawtypes")
Map<K, Map> topMap = new HashMap<K, Map>();

/** Map to value from keys */
@SuppressWarnings({ "rawtypes", "unchecked" })
public V lay(V value, K... keys)
{
if (keys == null)
{
//there are no keys.
expectedNumberOfKeys = 0;
V oldValue = this.value;
this.value = value;
return oldValue;
}

if (expectedNumberOfKeys != -1 && expectedNumberOfKeys != keys.length)
{
throw new IllegalArgumentException("Expecting " + expectedNumberOfKeys + " keys. Was " + keys.length );
}

expectedNumberOfKeys = keys.length;

Map<K, Map> currentMap = topMap;

//all but last key
for(int i = 0; i < keys.length - 1; i++)
{
K key = keys[i];

currentMap = linkToNextMap(currentMap, key);
}

//last key
V oldValue = ((Map<K,V>)currentMap).put(keys[keys.length - 1], value);
return oldValue;

}

@SuppressWarnings({ "rawtypes", "unchecked" })
Map<K,Map> linkToNextMap(Map<K,Map> map, K key)
{
Map<K, Map> nextMap = null;

if ( ! map.containsKey(key) )
{
map.put(key, new HashMap<K, Map>() );
}

nextMap = map.get(key);

return nextMap;
}

/**
* Get value maped from keys. Must include as many keys as laid down.
* Keys not found are taken as null keys
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public V get(K... keys)
{
if (keys == null)
{
return value;
}

//System.out.println(topMap+" <- topMap");//TODO remove

if (expectedNumberOfKeys == -1)
{
return null;
}

if (expectedNumberOfKeys == 0)
{
return value;
}

if (expectedNumberOfKeys != keys.length)
{
throw new IllegalArgumentException("Expecting " + expectedNumberOfKeys + " keys. Was " + keys.length );
}

Map<K, Map> currentMap = topMap;

//All but last key
for(int i = 0; i < keys.length - 1; i++)
{
currentMap = (Map) getDefault(currentMap, keys[i]);
}

//Last key
V result = (V) getDefault(currentMap, keys[keys.length - 1]);

return result;
}

@SuppressWarnings("rawtypes")
Object getDefault(Map map, K key)
{
Object result = null;

if (map != null)
{

//Use default key (null) if not found
if ( ! map.containsKey(key) )
{
key = null;
}

result = map.get(key);
}

return result;
}

public static void main(String[] args)
{
//Build {null={D=4, null=3}, A={null=1, Z=2}}
MultiKeyMap<String, Integer> map2 = new MultiKeyMap<String, Integer>();
map2.lay(1, "A", null);
map2.lay(2, "A", "Z");
map2.lay(3, null, null);
map2.lay(4, null, "D");
System.out.println(map2.get("A", null)); //1
System.out.println(map2.get("A", "Z")); //2
System.out.println(map2.get("A", "F")); //1 F not found so treating as null
System.out.println(map2.get(null, null));//3
System.out.println(map2.get(null, "D")); //4
System.out.println(map2.get("F", "D")); //4 F not found so treating as null
System.out.println();

//Build {null={D={C=4}, null={C=3}}, A={null={B=1}, Z={B=2}}}
MultiKeyMap<String, Integer> map3 = new MultiKeyMap<String, Integer>();
map3.lay(1, "A", null, "B");
map3.lay(2, "A", "Z", "B");
map3.lay(3, null, null, "C");
map3.lay(4, null, "D", "C");
System.out.println(map3.get("A", null, "B")); //1
System.out.println(map3.get("A", "Z", "B")); //2
System.out.println(map3.get("A", "F", "B")); //1 F not found so treating as null
System.out.println(map3.get(null, null, "C"));//3
System.out.println(map3.get(null, "D", "C")); //4
System.out.println(map3.get("F", "D", "C")); //4 F not found so treating as null
}
}

显示:

1
2
1
3
4
4

1
2
1
3
4
4

我知道没有人会为此投票,但我无法休眠,直到我把它从我的脑海中抹去。

晚安。

关于java - 具有复合键和分层默认值的集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26617821/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com