org.apache.commons.collections.Bag.getCount()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(8.6k)|赞(0)|评价(0)|浏览(132)

本文整理了Java中org.apache.commons.collections.Bag.getCount()方法的一些代码示例,展示了Bag.getCount()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Bag.getCount()方法的具体详情如下:
包路径:org.apache.commons.collections.Bag
类名称:Bag
方法名:getCount

Bag.getCount介绍

[英]Returns the number of occurrences (cardinality) of the given object currently in the bag. If the object does not exist in the bag, return 0.
[中]返回当前包中给定对象的出现次数(基数)。如果行李中不存在该对象,则返回0。

代码示例

代码示例来源:origin: commons-collections/commons-collections

public int getCount(Object object) {
  synchronized (lock) {
    return getBag().getCount(object);
  }
}

代码示例来源:origin: commons-collections/commons-collections

public int getCount(Object object) {
  return getBag().getCount(object);
}

代码示例来源:origin: commons-collections/commons-collections

public int getCount(Object object) {
  return getBag().getCount(object);
}

代码示例来源:origin: commons-collections/commons-collections

public int getCount(Object object) {
  return getBag().getCount(object);
}

代码示例来源:origin: wildfly/wildfly

public int getCount(Object object) {
  synchronized (lock) {
    return getBag().getCount(object);
  }
}

代码示例来源:origin: wildfly/wildfly

public int getCount(Object object) {
  return getBag().getCount(object);
}

代码示例来源:origin: wildfly/wildfly

public int getCount(Object object) {
  return getBag().getCount(object);
}

代码示例来源:origin: wildfly/wildfly

public int getCount(Object object) {
  return getBag().getCount(object);
}

代码示例来源:origin: commons-collections/commons-collections

/**
 * Returns the number of occurrences of <i>obj</i> in <i>coll</i>.
 *
 * @param obj  the object to find the cardinality of
 * @param coll  the collection to search
 * @return the the number of occurrences of obj in coll
 */
public static int cardinality(Object obj, final Collection coll) {
  if (coll instanceof Set) {
    return (coll.contains(obj) ? 1 : 0);
  }
  if (coll instanceof Bag) {
    return ((Bag) coll).getCount(obj);
  }
  int count = 0;
  if (obj == null) {
    for (Iterator it = coll.iterator();it.hasNext();) {
      if (it.next() == null) {
        count++;
      }
    }
  } else {
    for (Iterator it = coll.iterator();it.hasNext();) {
      if (obj.equals(it.next())) {
        count++;
      }
    }
  }
  return count;
}

代码示例来源:origin: commons-collections/commons-collections

/**
 * Returns <code>true</code> if the bag contains all elements in
 * the given collection, respecting cardinality.
 * 
 * @param other  the bag to check against
 * @return <code>true</code> if the Bag contains all the collection
 */
boolean containsAll(Bag other) {
  boolean result = true;
  Iterator it = other.uniqueSet().iterator();
  while (it.hasNext()) {
    Object current = it.next();
    boolean contains = getCount(current) >= other.getCount(current);
    result = result && contains;
  }
  return result;
}

代码示例来源:origin: commons-collections/commons-collections

/**
 * Returns <code>true</code> if the bag contains all elements in
 * the given collection, respecting cardinality.
 * 
 * @param other  the bag to check against
 * @return <code>true</code> if the Bag contains all the collection
 */
public boolean containsAll(Bag other) {
  boolean result = true;
  Iterator i = other.uniqueSet().iterator();
  while (i.hasNext()) {
    Object current = i.next();
    boolean contains = getCount(current) >= other.getCount(current);
    result = result && contains;
  }
  return result;
}

代码示例来源:origin: wildfly/wildfly

/**
 * Returns <code>true</code> if the bag contains all elements in
 * the given collection, respecting cardinality.
 * 
 * @param other  the bag to check against
 * @return <code>true</code> if the Bag contains all the collection
 */
boolean containsAll(Bag other) {
  boolean result = true;
  Iterator it = other.uniqueSet().iterator();
  while (it.hasNext()) {
    Object current = it.next();
    boolean contains = getCount(current) >= other.getCount(current);
    result = result && contains;
  }
  return result;
}

代码示例来源:origin: wildfly/wildfly

/**
 * Returns <code>true</code> if the bag contains all elements in
 * the given collection, respecting cardinality.
 * 
 * @param other  the bag to check against
 * @return <code>true</code> if the Bag contains all the collection
 */
public boolean containsAll(Bag other) {
  boolean result = true;
  Iterator i = other.uniqueSet().iterator();
  while (i.hasNext()) {
    Object current = i.next();
    boolean contains = getCount(current) >= other.getCount(current);
    result = result && contains;
  }
  return result;
}

代码示例来源:origin: commons-collections/commons-collections

/**
 * Compares this Bag to another.
 * This Bag equals another Bag if it contains the same number of occurrences of
 * the same elements.
 * 
 * @param object  the Bag to compare to
 * @return true if equal
 */
public boolean equals(Object object) {
  if (object == this) {
    return true;
  }
  if (object instanceof Bag == false) {
    return false;
  }
  Bag other = (Bag) object;
  if (other.size() != size()) {
    return false;
  }
  for (Iterator it = map.keySet().iterator(); it.hasNext();) {
    Object element = it.next();
    if (other.getCount(element) != getCount(element)) {
      return false;
    }
  }
  return true;
}

代码示例来源:origin: commons-collections/commons-collections

/**
 * Remove any members of the bag that are not in the given
 * bag, respecting cardinality.
 * @see #retainAll(Collection)
 * 
 * @param other  the bag to retain
 * @return <code>true</code> if this call changed the collection
 */
public boolean retainAll(Bag other) {
  boolean result = false;
  Bag excess = new HashBag();
  Iterator i = uniqueSet().iterator();
  while (i.hasNext()) {
    Object current = i.next();
    int myCount = getCount(current);
    int otherCount = other.getCount(current);
    if (1 <= otherCount && otherCount <= myCount) {
      excess.add(current, myCount - otherCount);
    } else {
      excess.add(current, myCount);
    }
  }
  if (!excess.isEmpty()) {
    result = removeAll(excess);
  }
  return result;
}

代码示例来源:origin: commons-collections/commons-collections

public void testRemoveAll() {
  Bag bag = makeBag();
  bag.add("A", 2);
  assertEquals("Should have count of 2", 2, bag.getCount("A"));
  bag.add("B");
  bag.add("C");
  assertEquals("Should have count of 4", 4, bag.size());
  List delete = new ArrayList();
  delete.add("A");
  delete.add("B");
  bag.removeAll(delete);
  assertEquals("Should have count of 1", 1, bag.getCount("A"));
  assertEquals("Should have count of 0", 0, bag.getCount("B"));
  assertEquals("Should have count of 1", 1, bag.getCount("C"));
  assertEquals("Should have count of 2", 2, bag.size());
}

代码示例来源:origin: commons-collections/commons-collections

public void testRemove() {
  Bag bag = makeBag();
  bag.add("A");
  assertEquals("Should have count of 1", 1, bag.getCount("A"));
  bag.remove("A");
  assertEquals("Should have count of 0", 0, bag.getCount("A"));
  bag.add("A");
  bag.add("A");
  bag.add("A");
  bag.add("A");
  assertEquals("Should have count of 4", 4, bag.getCount("A"));
  bag.remove("A", 0);
  assertEquals("Should have count of 4", 4, bag.getCount("A"));
  bag.remove("A", 2);
  assertEquals("Should have count of 2", 2, bag.getCount("A"));
  bag.remove("A");
  assertEquals("Should have count of 0", 0, bag.getCount("A"));
}

代码示例来源:origin: commons-collections/commons-collections

public void testIterator() {
  Bag bag = makeBag();
  bag.add("A");
  bag.add("A");
  bag.add("B");
  assertEquals("Bag should have 3 items", 3, bag.size());
  Iterator i = bag.iterator();

  boolean foundA = false;
  while (i.hasNext()) {
    String element = (String) i.next();
    // ignore the first A, remove the second via Iterator.remove()
    if (element.equals("A")) {
      if (foundA == false) {
        foundA = true;
      } else {
        i.remove();
      }
    }
  }

  assertTrue("Bag should still contain 'A'", bag.contains("A"));
  assertEquals("Bag should have 2 items", 2, bag.size());
  assertEquals("Bag should have 1 'A'", 1, bag.getCount("A"));
}

代码示例来源:origin: commons-collections/commons-collections

public void testBagAdd() {
  Bag bag = makeBag();
  bag.add("A");
  assertTrue("Should contain 'A'", bag.contains("A"));
  assertEquals("Should have count of 1", 1, bag.getCount("A"));
  bag.add("A");
  assertTrue("Should contain 'A'", bag.contains("A"));
  assertEquals("Should have count of 2", 2, bag.getCount("A"));
  bag.add("B");
  assertTrue(bag.contains("A"));
  assertTrue(bag.contains("B"));
}

代码示例来源:origin: commons-collections/commons-collections

public void testSize() {
  Bag bag = makeBag();
  assertEquals("Should have 0 total items", 0, bag.size());
  bag.add("A");
  assertEquals("Should have 1 total items", 1, bag.size());
  bag.add("A");
  assertEquals("Should have 2 total items", 2, bag.size());
  bag.add("A");
  assertEquals("Should have 3 total items", 3, bag.size());
  bag.add("B");
  assertEquals("Should have 4 total items", 4, bag.size());
  bag.add("B");
  assertEquals("Should have 5 total items", 5, bag.size());
  bag.remove("A", 2);
  assertEquals("Should have 1 'A'", 1, bag.getCount("A"));
  assertEquals("Should have 3 total items", 3, bag.size());
  bag.remove("B");
  assertEquals("Should have 1 total item", 1, bag.size());
}

相关文章