Guava Multiset Examples

0

A set is a collection that contains no duplicate elements. In contrast, a MultiSet is a collection that supports duplicate elements. A multiset is also sometimes called a bag. See example on Apache HashBag to know more about bag data structure.

It is called MultiSet as it allows adding multiples of elements.

Bag Using Traditional Data Structure

If we want to keep track of bag items count using traditional data structure like Map, we will be doing something like below:

TraditionalMapItemCountExample:

package com.javarticles.guava;

import java.util.HashMap;
import java.util.Map;

public class TraditionalMapItemCountExample {
    private static final String RED = "Red";
    private static final String YELLOW = "Yellow";
    private static final String GREEN = "Green";
    
    public static void main(String[] args) {        
        Map<String, Integer> bag = new HashMap<String, Integer>();
        addItem(bag, RED);
        addItem(bag, RED);
        addItem(bag, YELLOW);
        addItem(bag, GREEN);
        addItem(bag, GREEN);
        addItem(bag, GREEN);

       System.out.println("Total items in Bag: " + bag.size());
       System.out.println("Count of RED: " + bag.get(RED));
       System.out.println("Count of YELLOW: " + bag.get(YELLOW));
       System.out.println("Count of GREEN: " + bag.get(GREEN));
       
       System.out.println("Remove GREEN by 2");
       removeItem(bag, GREEN, 2);
       System.out.println("Count of GREEN: " + bag.get(GREEN));
    }  
    
    private static void addItem(Map<String, Integer> bag, String item) {       
        Integer count = bag.get(item);
        if (count == null) {
          bag.put(item, 1);
        } else {
          bag.put(item, count + 1);
        }
    }
    
    private static void removeItem(Map<String, Integer> bag, String item, Integer removeCount) {       
        if (bag.containsKey(item)) {
            Integer itemCount = bag.get(item);
            Integer updatedCount = itemCount - removeCount;
            bag.put(item, updatedCount);
        }
    }
}

Output:

Total items in Bag: 3
Count of RED: 2
Count of YELLOW: 1
Count of GREEN: 3
Remove GREEN by 2
Count of GREEN: 1

Using MultiSet

Above example can be re-written using MultiSet.

  1. Supports adding multiples of elements.
  2. MultiSet.size() returns the total items in the bag which includes even the duplicate items.
  3. One can either keep calling MultiSet.add(item) or call MultiSet.add(item, occurrence).
  4. We can also reset the count using MultiSet.setCount(item, count)
  5. If we want to remove the item from bag, call MultiSet.remove(item)
  6. To know the count of items in bag, call MultiSet.count(item)

In this example, we will use Multiset implementation backed by a HashMap. We create MultiSet using:

Multiset bag = HashMultiset.create();

MultisetExample:

package com.javarticles.guava;

import com.google.common.collect.HashMultiset;
import com.google.common.collect.Multiset;

public class MultisetExample {
    private static final String RED = "Red";
    private static final String YELLOW = "Yellow";
    private static final String GREEN = "Green";
    
    public static void main(String[] args) {
       ``
       bag.add(RED);
       bag.add(RED);
       bag.add(YELLOW);
       bag.add(GREEN);
       bag.add(GREEN);
       bag.add(GREEN);
       bag.add(YELLOW, 2);
       
       System.out.println("Total items in Bag: " + bag.size());
       System.out.println("Count of RED: " + bag.count(RED));
       System.out.println("Count of YELLOW: " + bag.count(YELLOW));
       System.out.println("Count of GREEN: " + bag.count(GREEN));
       
       System.out.println("Remove GREEN by 2");
       bag.remove(GREEN, 2);
       System.out.println("Count of GREEN: " + bag.count(GREEN));
       
       bag.setCount(GREEN, 0);
       System.out.println("Count of GREEN: " + bag.count(GREEN));
    }   
}

Output:

Total items in Bag: 8
Count of RED: 2
Count of YELLOW: 3
Count of GREEN: 3
Remove GREEN by 2
Count of GREEN: 1
Count of GREEN: 0

Order-independent equality

MultiSet like Set support order independent equality.

OrderIndependentMultiSetExample:

package com.javarticles.guava;

import java.util.HashSet;
import java.util.Set;

import com.google.common.collect.HashMultiset;
import com.google.common.collect.Multiset;

public class OrderIndependentMultisetExample {
    private static final String RED = "Red";
    private static final String YELLOW = "Yellow";
    private static final String GREEN = "Green";
    
    public static void main(String[] args) {
       Set set1 = new HashSet(); 
       set1.add(RED);
       set1.add(YELLOW);
       set1.add(GREEN);
       
       Set set2 = new HashSet(); 
       set2.add(GREEN);
       set2.add(RED);
       set2.add(YELLOW);
       
       System.out.println("Is set1.equals(set2)? " + set1.equals(set2));
       
       
       Multiset bag1 = HashMultiset.create();
       bag1.add(RED);
       bag1.add(RED);
       bag1.add(YELLOW);
       bag1.add(GREEN);
       bag1.add(GREEN);
       bag1.add(GREEN);
       
       Multiset bag2 = HashMultiset.create();
       bag2.add(GREEN, 3);
       bag2.add(YELLOW);
       bag2.add(RED, 2);
       
       
       System.out.println("Is bag1.equals(bag2)? " + bag1.equals(bag2));
    }   
}

Output:

Is set1.equals(set2)? true
Is bag1.equals(bag2)? true

Download the source code

This was an example about Guava Multiset.

You can download the source code here: guavaMultisetExamples.zip
Share.

Comments are closed.