Java Language Multimap dans les collections Goyave, Apache et Eclipse


Exemple

Ce multimap permet des paires clé-valeur en double. Les analogues JDK sont HashMap <K, List>, HashMap <K, Set>, etc.

Commande de Key Commande de valeur Dupliquer Clé analogique Valeur analogique Goyave Apache Collections Eclipse (GS) JDK
non défini Ordre d'insertion Oui HashMap ArrayList ArrayListMultimap MultiValueMap FastListMultimap HashMap<K, ArrayList<V>>
non défini non défini non HashMap HashSet HashMultimap MultiValueMap. multiValueMap( new HashMap<K, Set>(), HashSet.class); UnifiedSetMultimap HashMap<K, HashSet<V>>
non défini trié non HashMap TreeSet Multimaps. newMultimap( HashMap, Supplier <TreeSet>) MultiValueMap.multiValueMap( new HashMap<K, Set>(), TreeSet.class) TreeSortedSet- Multimap HashMap<K, TreeSet<V>>
Ordre d'insertion Ordre d'insertion Oui LinkedHashMap ArrayList LinkedListMultimap MultiValueMap. multiValueMap (new LinkedHashMap <K, List> (), ArrayList.class); LinkedHashMap <K, ArrayList>
Ordre d'insertion Ordre d'insertion non LinkedHashMap LinkedHashSet LinkedHashMultimap MultiValueMap. multiValueMap(new LinkedHashMap<K, Set>(), LinkedHashSet.class) LinkedHashMap<K, LinkedHashSet<V>>
trié trié non TreeMap TreeSet TreeMultimap MultiValueMap. multiValueMap( new TreeMap<K, Set>(),TreeSet.class) TreeMap<K, TreeSet<V>>
Exemples utilisant Multimap

Tâche : Analyser "Bonjour tout le monde! Bonjour à tous! Bonjour à tous!" chaîne pour séparer les mots et imprimer tous les index de chaque mot en utilisant MultiMap (par exemple, Hello = [0, 2], World! = [1, 5], etc.)

1. MultiValueMap d'Apache

    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. HashBiMap de la collection GS / Eclipse

    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. HashMultiMap de goyave

     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 exemples:

I. Collection Apache:

  1. MultiValueMap
  2. MultiValueMapLinked
  3. MultiValueMapTree

II. Collection GS / Eclipse

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

III. Goyave

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