public void testCreateWithSize() {
   Multiset<String> multiset = HashMultiset.create(50);
   multiset.add("foo", 2);
   multiset.add("bar");
   assertEquals(3, multiset.size());
   assertEquals(2, multiset.count("foo"));
 }
 /**
  * @param scores
  * @param restrictionSet
  * @return intersection of set (Multiset<Integer>) and restrictionSet (if restrictionSet non-null
  *     & non-empty); otherwise return set
  */
 public static Multiset<Integer> intersect(
     final Multiset<Integer> scores, final Set<Integer> restrictionSet) {
   if (restrictionSet != null && !restrictionSet.isEmpty()) {
     int prevSize = scores.size();
     Multiset<Integer> intersection = HashMultiset.create(scores);
     intersection.retainAll(restrictionSet);
     log.debug(
         prevSize != 0
             ? ("Size saving by retainAll = "
                     + (((prevSize - intersection.size()) * 100) / prevSize))
                 + "%"
             : "");
     return intersection;
   }
   return scores;
 }
 public void testCreateWithComparator() {
   Multiset<String> multiset = TreeMultiset.create(Collections.reverseOrder());
   multiset.add("foo", 2);
   multiset.add("bar");
   assertEquals(3, multiset.size());
   assertEquals(2, multiset.count("foo"));
   assertEquals("[foo x 2, bar]", multiset.toString());
 }
 @GwtIncompatible // SerializableTester
 public void testSerializationIndirectSelfReference() {
   Multiset<MultisetHolder> multiset = HashMultiset.create();
   MultisetHolder holder = new MultisetHolder(multiset);
   multiset.add(holder, 2);
   Multiset<MultisetHolder> copy = SerializableTester.reserialize(multiset);
   assertEquals(2, copy.size());
   assertSame(copy, copy.iterator().next().member);
 }
  /** An implementation of {@link Multiset#equals}. */
  static boolean equalsImpl(Multiset<?> multiset, Object object) {
    if (object == multiset) {
      return true;
    }
    if (object instanceof Multiset) {
      Multiset<?> that = (Multiset<?>) object;
      /*
       * We can't simply check whether the entry sets are equal, since that
       * approach fails when a TreeMultiset has a comparator that returns 0
       * when passed unequal elements.
       */

      if (multiset.size() != that.size() || multiset.entrySet().size() != that.entrySet().size()) {
        return false;
      }
      for (Entry<?> entry : that.entrySet()) {
        if (multiset.count(entry.getElement()) != entry.getCount()) {
          return false;
        }
      }
      return true;
    }
    return false;
  }
Exemple #6
0
 @Override
 public boolean equals(@Nullable Object object) {
   if (object == this) {
     return true;
   }
   if (object instanceof Multiset) {
     Multiset<?> that = (Multiset<?>) object;
     if (this.size() != that.size()) {
       return false;
     }
     for (Entry<?> entry : that.entrySet()) {
       if (count(entry.getElement()) != entry.getCount()) {
         return false;
       }
     }
     return true;
   }
   return false;
 }
 public void testCreateFromIterable() {
   Multiset<String> multiset = HashMultiset.create(Arrays.asList("foo", "bar", "foo"));
   assertEquals(3, multiset.size());
   assertEquals(2, multiset.count("foo"));
 }
 public void testCreateFromIterable() {
   Multiset<String> multiset = TreeMultiset.create(Arrays.asList("foo", "bar", "foo"));
   assertEquals(3, multiset.size());
   assertEquals(2, multiset.count("foo"));
   assertEquals("[bar, foo x 2]", multiset.toString());
 }