gpt4 book ai didi

java - 获取键匹配条件的所有映射值的有效方法

转载 作者:行者123 更新时间:2023-11-30 03:22:19 28 4
gpt4 key购买 nike

我有以下 map Map<IndexDerivedKey, Collection<Data<D>>> indexedData在可索引的数据结构中。

IndexDerivedKey符合Index ,索引由键和对应的值组成。键用于从可索引数据结构中的元素中提取值。

例如有一个索引,它由键 firstName 组成。和lastName ,我们使用此索引从拥有这些属性的对象中提取值,此操作会产生 IndexDerivedKey每个对象。

那个IndexDerivedKey包含从上述键到各自值的映射,用于存储映射中的对象 indexedData ,我之前提到过。映射值是集合类型,因为多个对象可能相等(相对于索引)。

问题如何使用 firstName = "John" 提取所有对象,特此忽略 lastName 的值。显然我可以迭代所有键并检查 firstName 的值在O(n) .

但是自从 IndexDerivedKey {firstName = "John"}是所有其他键的子集 firstName = "John" ,例如{firstName = "John", lastName = "Smith"} ,我想必须有一种更有效的方法。也许利用TreeSet

<小时/>

1

  public IndexDerivedKeyImpl(Index index, Map<String, String> keyValues)
{
this.keyValues = keyValues;
this.index = index;

for (String key : keyValues.keySet())
if (!index.supportsKey(key))
throw new IndexKeyMismatchException(key, index);
}

2

  // in data.Index.index(Indexable)
@Override
public IndexDerivedKey index(Indexable data)
{
Map<String, String> keyValues = new HashMap<String, String>();
IndexDerivedKey key = new IndexDerivedKeyImpl(this, keyValues);

for (String k : keys)
{
String value = data.get(k);
if (value != null)
keyValues.put(k, value);
}

return key;
}

最佳答案

如果搜索始终按特定顺序进行,请使用 Comparable 和 equals 创建一个键类。

然后使用SortedMap,例如TreeMap。这也是可导航的。例如,您可以使用 subMap(fromKey, toKey)

关于java - 获取键匹配条件的所有映射值的有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31052439/

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