是否有可能在番石榴,問題與番石榴的BIMAP和LinkedHashMap的
要BiMap進行反向查找鍵和多個值嗎?準確地說,我有密鑰和相應的多個值,我想從一個值中獲取密鑰。
要在LinkedHashMap中存儲多個值?確切地說,我想以某種順序存儲,鍵 - 多個值,因此我可以在列表中獲得關鍵位置。
是否有可能在番石榴,問題與番石榴的BIMAP和LinkedHashMap的
要BiMap進行反向查找鍵和多個值嗎?準確地說,我有密鑰和相應的多個值,我想從一個值中獲取密鑰。
要在LinkedHashMap中存儲多個值?確切地說,我想以某種順序存儲,鍵 - 多個值,因此我可以在列表中獲得關鍵位置。
Ad。 1.我想你想BiMap
反向查找(BitMap不存在於番石榴)?是的,這是可能的,你只需撥打inverse
在你的BiMap<K, V>
,你會得到你的bimap BiMap<V, K>
查看。
實施例(從番石榴的測試套件截取):
public void testMapConstructor() {
/* Test with non-empty Map. */
Map<String, String> map = ImmutableMap.of(
"canada", "dollar",
"chile", "peso",
"switzerland", "franc");
HashBiMap<String, String> bimap = HashBiMap.create(map);
assertEquals("dollar", bimap.get("canada"));
assertEquals("canada", bimap.inverse().get("dollar"));
}
廣告。 2.假設你的意思是「我想存儲,鍵 - >多[收藏]值」(Map<K, Collection<V>>
),ListMultimap
可能是你想要什麼,更precisly ArrayListMultimap
(蜜餞值順序)或LinkedListMultimap
(保存鍵和值訂購)。如果你的對象是不可改變的,我強烈建議你使用ImmutableListMultimap
。
您還可以通過使用factory(有點冗長)創建自己的實現的Multimap
,即使用:
private static <K, V> ListMultimap<K, V> makeLinkedArrayListMultimap() {
return Multimaps.newListMultimap(Maps.<K, Collection<V>>newLinkedHashMap(),
new Supplier<List<V>>() {
@Override public List<V> get() {
return Lists.newArrayList();
}
});
}
public static void main(final String[] args) {
final ListMultimap<String, String> multimap = makeLinkedArrayListMultimap();
multimap.putAll("one", ImmutableList.of("zero", "three"));
multimap.putAll("two", ImmutableList.of("three", "four", "three"));
multimap.putAll("three", ImmutableList.<String>of()); // note that this doesn't add key to multimap
multimap.put("four", "forty-two");
System.out.println(multimap);
// prints {one=[one, three], two=[three, four, three], four=[forty-two]}
final List<String> listForOnes = multimap.get("one");
System.out.println(listForOnes.get(0));
// prints zero
}
附:看看Guava's wiki,這是解釋BiMap和Multimaps。
謝謝。其實我想解決這個http://stackoverflow.com/questions/9661374/java-hash-table-issue-with-object-refference問題。這就是爲什麼,我想用單個「foo」替換所有「foo」,然後執行反向查找(用於鍵 - 多值映射),或者從節省空間的位置獲取鍵(用於鍵 - 多值映射)。但是,從你的答案看來,我必須浪費比以前更多的空間。是否有任何番石榴解決方案http://stackoverflow.com/questions/9661374/java-hash-table-issue-with-object-refference。謝謝。 – Arpssss 2012-03-12 20:08:07
正如@Xaerxess在回答您的第2個問題時所說,您可以使用Multimaps.newListMultimap
方法創建自己的ListMultimap
,它使用LinkedHashMap
作爲其支持地圖。
對於第一個問題,如果您將鍵映射到多個值(即Multimap
),則可以使用方法Multimaps.invertFrom
創建原始Multimap
的反轉副本以進行反向查找。此外,您可以創建一個ImmutableListMultimap
原件副本,並使用其inverse()
方法獲得相反的結果,雖然這只是要複製原件,就像Multimaps.invertFrom
一樣(儘管它會緩存它,因此重複調用inverse()
會返回相同的副本。)
如果您不介意額外的內存消耗,想要做多次反向查找,並且不需要反向副本以保持與發生的原始更改保持同步,這可能是值得的在你創建它之後。如果您只想查找映射到一個特定值的鍵,則可以在條目的一次迭代中執行此操作,而無需創建完整副本。
我同意''Multimaps.invertFrom'和'ImmutableXXXMultimap.invert()'是非常有用的。 @OP - 參見我的[用ImmultableListMultimap.invert()'](http://stackoverflow.com/a/8439744/708434)的示例代碼回答另一個問題。 – Xaerxess 2012-03-12 18:19:05
@ColinD,Thanks.Actually我想解決這個http://stackoverflow.com/questions/9661374/java-hash-table-issue-with-object-refference問題。這就是爲什麼我想用單個「foo」替換所有「foo」,然後反向查找(用於鍵值多值映射),或者從可節省空間的位置獲取鍵值(用於鍵 - 多值映射)。但是,從你的答案看來,我必須浪費比以前更多的空間。是否有任何番石榴解決方案http://stackoverflow.com/questions/9661374/java-hash-table-issue-with-object-refference。謝謝。 – Arpssss 2012-03-12 20:05:12
@Arpssss:我擔心在這個問題上,我真的不清楚這個問題,以及那個你真正想做的事情,所以我不確定我能幫助你。在另一個問題中,對我來說,聽起來你擔心的是你可能不應該擔心的事情,但是我再次說不出任何細節。 – ColinD 2012-03-12 20:50:14
其實我想解決這個http://stackoverflow.com/questions/9661374/java-hash-table-issue-with-object-refference問題。這就是爲什麼我想用單個「foo」替換所有「foo」,然後反向查找(用於鍵值多值映射),或者從可節省空間的位置獲取鍵值(用於鍵 - 多值映射)。但是,看來,我必須浪費比以前更多的空間。有沒有這個http://stackoverflow.com/questions/9661374/java-hash-table-issue-with-object-refference的番石榴解決方案。謝謝。 – Arpssss 2012-03-12 20:11:51
這種感覺就像你正試圖做的不成熟的優化。 – 2012-03-13 03:04:08