2

グアバでそれは可能ですか、

  1. BiMapキーと複数の値の逆引きを実行するには? 正確には、キーと対応する複数の値があり、値からキーを取得したい。

  2. 複数の値をLinkedHashMap? 正確には、キー - 複数の値をある順序で保存したいので、リスト内のキーの位置を取得できます。

4

3 に答える 3

8

広告。1.はい、 で逆ルックアップを行うことができます。BiMap<K, V>を呼び出すだけで、 の逆ビューが得られます。inverseBiMapBiMap<V, K> BiMap

例 (Guava のテスト スイートから取得):

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おそらくより正確に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
}

PSと の両方を説明しているGuava の wikiを見てください。BiMapMultimap

于 2012-03-12T10:01:48.560 に答える
3

@Xaerxessが2番目の質問に対する回答で述べているように、このメソッドを使用して、バッキングマップとしてListMultimapを使用する独自の質問を作成できます。LinkedHashMapMultimaps.newListMultimap

キーが複数の値(つまり、Multimap)にマップされている最初の質問では、このメソッドを使用してMultimaps.invertFrom、元の逆コピーを作成し、Multimap逆ルックアップを実行できます。また、ImmutableListMultimapオリジナルのコピーを作成し、そのinverse()メソッドを使用して逆を取得することもできますが、これはオリジナルと同じようにコピーします(ただし、同じコピーMultimaps.invertFromを返すために繰り返し呼び出すとキャッシュされます)。inverse()

余分なメモリ消費を気にせず、複数の逆ルックアップを実行したい場合、および作成後に発生する元の変更を最新の状態に保つために逆コピーを必要としない場合、これはおそらく価値があります。1つの特定の値にマップするキーを検索するだけの場合は、完全なコピーを作成せずに、エントリの1回の反復でそれを実行できます。

于 2012-03-12T17:59:13.860 に答える
3

The closest in Guava is Multiset to map multiple values to key, but I doubt it satisfies your requirement.

  1. I doubt it is good idea to look up key using values (when you have multiple values mapped to single key), in order to do this your value should be unique and considering your data structure (which is like Map<Key, Collection<Value>) it cannot be guaranteed to have unique values.
  2. The another option with guava is BiMap which requires unique values and can provide a reverse mappings (value -> key) but since you need to map multiple values to same key, this also not a good fit.
于 2012-03-12T09:53:38.560 に答える