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
-
来自 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 集合:
II。GS / Eclipse 集合
III。番石榴