Guava Apache 和 Eclipse 集合中的 Multimap

此多圖允許重複的鍵值對。JDK 模擬是 HashMap <K,List>,HashMap <K,Set>等等。

鍵的順序 值的順序 重複 模擬鍵 模擬值 番石榴 阿帕奇 Eclipse(GS) 集合 JDK
沒有定義的 插入順序 HashMap 中 陣列列表 ArrayListMultimap MultiValueMap FastListMultimap HashMap<K, ArrayList<V>>
沒有定義的 沒有定義的 沒有 HashMap 中 HashSet 的 HashMultimap MultiValueMap. multiValueMap( new HashMap<K, Set>(), HashSet.class); UnifiedSetMultimap HashMap<K, HashSet<V>>
沒有定義的 分類 沒有 HashMap 中 TreeSet 中 Multimaps. newMultimap( HashMap, Supplier <TreeSet>) MultiValueMap.multiValueMap( new HashMap<K, Set>(), TreeSet.class) TreeSortedSet- Multimap HashMap<K, TreeSet<V>>
插入順序 插入順序 LinkedHashMap 的 陣列列表 LinkedListMultimap MultiValueMap。multiValueMap(new LinkedHashMap <K,List>(),ArrayList.class); LinkedHashMap <K,ArrayList>
插入順序 插入順序 沒有 LinkedHashMap 的 LinkedHashSet LinkedHashMultimap MultiValueMap. multiValueMap(new LinkedHashMap<K, Set>(), LinkedHashSet.class) LinkedHashMap<K, LinkedHashSet<V>>
分類 分類 沒有 TreeMap 的 TreeSet 中 TreeMultimap MultiValueMap. multiValueMap( new TreeMap<K, Set>(),TreeSet.class) TreeMap<K, TreeSet<V>>

使用 Multimap 的示例

任務 :解析“Hello World!大家好!嗨世界!” 字串分隔單詞並使用 MultiMap 列印每個單詞的所有索引(例如,Hello = [0,2],World != [1,5]等)

1.來自 Apache 的 MultiValueMap

    String INPUT_TEXT = "Hello World! Hello All! Hi World!";
    // Parse text to words and index
    List<String> words = Arrays.asList(INPUT_TEXT.split(" "));
    // Create Multimap
    MultiMap<String, Integer> multiMap = new MultiValueMap<String, Integer>();

    // Fill Multimap
    int i = 0;
    for(String word: words) {
        multiMap.put(word, i);
        i++;
    }

    // Print all words
    System.out.println(multiMap); // print {Hi=[4], Hello=[0, 2], World!=[1, 5], All!=[3]} - in random orders
    // Print all unique words
    System.out.println(multiMap.keySet());    // print [Hi, Hello, World!, All!] - in random orders

    // Print all indexes
    System.out.println("Hello = " + multiMap.get("Hello"));    // print [0, 2]
    System.out.println("World = " + multiMap.get("World!"));    // print [1, 5]
    System.out.println("All = " + multiMap.get("All!"));    // print [3]
    System.out.println("Hi = " + multiMap.get("Hi"));    // print [4]
    System.out.println("Empty = " + multiMap.get("Empty"));    // print null

    // Print count unique words
    System.out.println(multiMap.keySet().size());    //print 4

2.來自 GS / Eclipse Collection 的 HashBiMap

    String[] englishWords = {"one", "two", "three","ball","snow"};
    String[] russianWords = {"jeden", "dwa", "trzy", "kula", "snieg"};

    // Create Multiset
    MutableBiMap<String, String> biMap = new HashBiMap(englishWords.length);
    // Create English-Polish dictionary
    int i = 0;
    for(String englishWord: englishWords) {
        biMap.put(englishWord, russianWords[i]);
        i++;
    }

    // Print count words
    System.out.println(biMap); // print {two=dwa, ball=kula, one=jeden, snow=snieg, three=trzy} - in random orders
    // Print all unique words
    System.out.println(biMap.keySet());    // print [snow, two, one, three, ball] - in random orders
    System.out.println(biMap.values());    // print [dwa, kula, jeden, snieg, trzy] - in random orders

    // Print translate by words
    System.out.println("one = " + biMap.get("one"));    // print one = jeden
    System.out.println("two = " + biMap.get("two"));    // print two = dwa
    System.out.println("kula = " + biMap.inverse().get("kula"));    // print kula = ball
    System.out.println("snieg = " + biMap.inverse().get("snieg"));    // print snieg = snow
    System.out.println("empty = " + biMap.get("empty"));    // print empty = null

    // Print count word's pair
    System.out.println(biMap.size());    //print 5
  1. 來自 Guava 的 HashMultiMap

     String INPUT_TEXT = "Hello World! Hello All! Hi World!";
     // Parse text to words and index
     List<String> words = Arrays.asList(INPUT_TEXT.split(" "));
     // Create Multimap
     Multimap<String, Integer> multiMap = HashMultimap.create();
    
     // Fill Multimap
     int i = 0;
     for(String word: words) {
         multiMap.put(word, i);
         i++;
     }
    
     // Print all words
     System.out.println(multiMap); // print {Hi=[4], Hello=[0, 2], World!=[1, 5], All!=[3]} - keys and values in random orders
     // Print all unique words
     System.out.println(multiMap.keySet());    // print [Hi, Hello, World!, All!] - in random orders
    
     // Print all indexes
     System.out.println("Hello = " + multiMap.get("Hello"));    // print [0, 2]
     System.out.println("World = " + multiMap.get("World!"));    // print [1, 5]
     System.out.println("All = " + multiMap.get("All!"));    // print [3]
     System.out.println("Hi = " + multiMap.get("Hi"));    // print [4]
     System.out.println("Empty = " + multiMap.get("Empty"));    // print []
    
     // Print count all words
     System.out.println(multiMap.size());    //print 6
    
     // Print count unique words
     System.out.println(multiMap.keySet().size());    //print 4
    

Nore 示例:

I. Apache 集合:

  1. MultiValueMap
  2. MultiValueMapLinked
  3. MultiValueMapTree

II。GS / Eclipse 集合

  1. FastListMultimap
  2. HashBagMultimap
  3. TreeSortedSetMultimap
  4. UnifiedSetMultimap

III。番石榴

  1. HashMultiMap
  2. LinkedHashMultimap
  3. LinkedListMultimap
  4. TreeMultimap
  5. ArrayListMultimap