A Bag/ultiset stores each object in the collection together with a count of occurrences. Extra methods on the interface allow multiple copies of an object to be added or removed at once. JDK analog is HashMap<T, Integer>, when values is count of copies this key.
Type | Guava | Apache Commons Collections | GS Collections | JDK |
---|---|---|---|---|
Order not defined | HashMultiset | HashBag | HashBag | HashMap |
Sorted | TreeMultiset | TreeBag | TreeBag | TreeMap |
Insertion-order | LinkedHashMultiset | - | - | LinkedHashMap |
Concurrent variant | ConcurrentHashMultiset | SynchronizedBag | SynchronizedBag | Collections.synchronizedMap(HashMap<String, Integer>) |
Concurrent and sorted | - | SynchronizedSortedBag | SynchronizedSortedBag | Collections.synchronizedSortedMap(TreeMap<String,Integer>) |
Immutable collection | ImmutableMultiset | UnmodifiableBag | UnmodifiableBag | Collections.unmodifiableMap(HashMap<String, Integer>) |
Immutable and sorted | ImmutableSortedMultiset | UnmodifiableSortedBag | UnmodifiableSortedBag | Collections.unmodifiableSortedMap(TreeMap<String,Integer>) |
Examples:
1. Using SynchronizedSortedBag from Apache:
// Parse text to separate words String customInputText = "How are you? How is everything?"; // Create Multiset Bag customBag = SynchronizedSortedBag.synchronizedBag(new TreeBag(Arrays.asList(customInputText.split(" ")))); // Print count words System.out.println(customBag); // print [1:How, 1:How, 1:are, 1:everything?, 1:is, 1:you?]- in natural (alphabet) order // Print all unique words System.out.println(customBag.uniqueSet()); // print [How, are, everything?, is, you?]- in natural (alphabet) order // Print count occurrences of words System.out.println("How = " + customBag.getCount("How")); // print 2 System.out.println("are = " + customBag.getCount("are")); // print 1 System.out.println("you? = " + customBag.getCount("you?")); // print 1 System.out.println("is = " + customBag.getCount("is")); // print 1 System.out.println("everything? = " + customBag.getCount("everything?")); // print 1 System.out.println("Empty = " + customBag.getCount("Empty")); // print 0 // Print count all words System.out.println(customBag.size()); //print 6 // Print count unique words System.out.println(customBag.uniqueSet().size()); //print 5
2. Using TreeBag from Eclipse(GC):
// Parse text to separate words String customInputText = "Greetings Universe! Hello Everyone! Hi Universe!"; // Create Multiset MutableSortedBag<String> customBag = TreeBag.newBag(Arrays.asList(customInputText.split(" "))); // Print count words System.out.println(customBag); // print [Everyone!, Greetings, Hello, Hello, Hi, Universe!, Universe!]- in natural order // Print all unique words System.out.println(customBag.toSortedSet()); // print [Everyone!, Greetings, Hello, Hi, Universe!]- in natural order // Print count occurrences of words System.out.println("Hello = " + customBag.occurrencesOf("Hello")); // print 2 System.out.println("Universe = " + customBag.occurrencesOf("Universe!")); // print 2 System.out.println("Greetings = " + customBag.occurrencesOf("Greetings")); // print 1 System.out.println("Hi = " + customBag.occurrencesOf("Hi")); // print 1 System.out.println("Empty = " + customBag.occurrencesOf("Empty")); // print 0 // Print count all words System.out.println(customBag.size()); // print 6 // Print count unique words System.out.println(customBag.toSet().size()); // print 5
3. Using LinkedHashMultiset from Guava:
// Parse text to separate words String customInputText = "How are you? How are all of you doing today?"; // Create Multiset Multiset<String> customMultiset = LinkedHashMultiset.create(Arrays.asList(customInputText.split(" "))); // Print count words System.out.println(customMultiset); // print [How x 2, are x 2, you?, all x 2, of, doing, today?]- in predictable iteration order // Print all unique words System.out.println(customMultiset.elementSet()); // print [How, are, you?, all, of, doing, today?] - in predictable iteration order // Print count occurrences of words System.out.println("How = " + customMultiset.count("How")); // print 2 System.out.println("are = " + customMultiset.count("are")); // print 2 System.out.println("you? = " + customMultiset.count("you?")); // print 1 System.out.println("all = " + customMultiset.count("all")); // print 2 System.out.println("of = " + customMultiset.count("of")); // print 1 System.out.println("doing = " + customMultiset.count("doing")); // print 1 System.out.println("today? = " + customMultiset.count("today?")); // print 1 System.out.println("Empty = " + customMultiset.count("Empty")); // print 0 // Print count all words System.out.println(customMultiset.size()); //print 10 // Print count unique words System.out.println(customMultiset.elementSet().size()); //print 7
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions