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

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

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

Bag.size介绍

[英]Returns the total number of items in the bag across all types.
[中]返回包中所有类型的项目总数。

代码示例

代码示例来源: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

/**
 * Returns true if the given object is not null, has the precise type 
 * of this bag, and contains the same number of occurrences of all the
 * same elements.
 *
 * @param object  the object to test for equality
 * @return true if that object equals this bag
 */
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: wildfly/wildfly

/**
 * 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: wildfly/wildfly

/**
 * Returns true if the given object is not null, has the precise type 
 * of this bag, and contains the same number of occurrences of all the
 * same elements.
 *
 * @param object  the object to test for equality
 * @return true if that object equals this bag
 */
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

public void testIteratorFailDoubleRemove() {
  Bag bag = makeBag();
  bag.add("A");
  bag.add("A");
  bag.add("B");
  Iterator it = bag.iterator();
  it.next();
  it.next();
  assertEquals(3, bag.size());
  it.remove();
  assertEquals(2, bag.size());
  try {
    it.remove();
    fail("Should throw IllegalStateException");
  } catch (IllegalStateException ex) {
    // expected
  }
  assertEquals(2, bag.size());
  it.next();
  it.remove();
  assertEquals(1, bag.size());
}

代码示例来源: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());
}

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

public void testEmptyBagSerialization() throws IOException, ClassNotFoundException {
  Bag bag = makeBag();
  if (!(bag instanceof Serializable && isTestSerialization())) return;
  
  byte[] objekt = writeExternalFormToBytes((Serializable) bag);
  Bag bag2 = (Bag) readExternalFormFromBytes(objekt);
  assertEquals("Bag should be empty",0, bag.size());
  assertEquals("Bag should be empty",0, bag2.size());
}

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

public void testTransformedBag() {
  Bag bag = TransformedSortedBag.decorate(new TreeBag(), TestTransformedCollection.STRING_TO_INTEGER_TRANSFORMER);
  assertEquals(0, bag.size());
  Object[] els = new Object[] {"1", "3", "5", "7", "2", "4", "6"};
  for (int i = 0; i < els.length; i++) {
    bag.add(els[i]);
    assertEquals(i + 1, bag.size());
    assertEquals(true, bag.contains(new Integer((String) els[i])));
  }
  
  assertEquals(true, bag.remove(new Integer((String) els[0])));
  
}

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

public void testIteratorRemoveProtectsInvariants() {
  Bag bag = makeBag();
  bag.add("A");
  bag.add("A");
  assertEquals(2, bag.size());
  Iterator it = bag.iterator();
  assertEquals("A", it.next());
  assertEquals(true, it.hasNext());
  it.remove();
  assertEquals(1, bag.size());
  assertEquals(true, it.hasNext());
  assertEquals("A", it.next());
  assertEquals(false, it.hasNext());
  it.remove();
  assertEquals(0, bag.size());
  assertEquals(false, it.hasNext());
  
  Iterator it2 = bag.iterator();
  assertEquals(false, it2.hasNext());
}

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

public void testFullBagSerialization() throws IOException, ClassNotFoundException {
  Bag bag = makeBag();
  bag.add("A");
  bag.add("A");
  bag.add("B");
  bag.add("B");
  bag.add("C");
  int size = bag.size();
  if (!(bag instanceof Serializable && isTestSerialization())) return;
  
  byte[] objekt = writeExternalFormToBytes((Serializable) bag);
  Bag bag2 = (Bag) readExternalFormFromBytes(objekt);
  assertEquals("Bag should be same size", size, bag.size());
  assertEquals("Bag should be same size", size, bag2.size());
}

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

public void testRetainAll() {
  Bag bag = makeBag();
  bag.add("A");
  bag.add("A");
  bag.add("A");
  bag.add("B");
  bag.add("B");
  bag.add("C");
  List retains = new ArrayList();
  retains.add("B");
  retains.add("C");
  bag.retainAll(retains);
  assertEquals("Should have 2 total items", 2, bag.size());
}

代码示例来源: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 testTransformedBag() {
  Bag bag = TransformedBag.decorate(new HashBag(), TestTransformedCollection.STRING_TO_INTEGER_TRANSFORMER);
  assertEquals(0, bag.size());
  Object[] els = new Object[] {"1", "3", "5", "7", "2", "4", "6"};
  for (int i = 0; i < els.length; i++) {
    bag.add(els[i]);
    assertEquals(i + 1, bag.size());
    assertEquals(true, bag.contains(new Integer((String) els[i])));
    assertEquals(false, bag.contains(els[i]));
  }
  
  assertEquals(false, bag.remove(els[0]));
  assertEquals(true, bag.remove(new Integer((String) els[0])));
  
}

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

public void testlegalAddRemove() {
  Bag bag = makeTestBag();
  assertEquals(0, bag.size());
  Object[] els = new Object[] {"1", "3", "5", "7", "2", "4", "1"};
  for (int i = 0; i < els.length; i++) {
    bag.add(els[i]);
    assertEquals(i + 1, bag.size());
    assertEquals(true, bag.contains(els[i]));
  }
  Set set = ((PredicatedBag) bag).uniqueSet();
  assertTrue("Unique set contains the first element",set.contains(els[0]));
  assertEquals(true, bag.remove(els[0])); 
  set = ((PredicatedBag) bag).uniqueSet();
  assertTrue("Unique set now does not contain the first element",
    !set.contains(els[0])); 
}

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

public void testlegalAddRemove() {
  Bag bag = makeTestBag();
  assertEquals(0, bag.size());
  Object[] els = new Object[] {"1", "3", "5", "7", "2", "4", "1"};
  for (int i = 0; i < els.length; i++) {
    bag.add(els[i]);
    assertEquals(i + 1, bag.size());
    assertEquals(true, bag.contains(els[i]));
  }
  Set set = ((PredicatedBag) bag).uniqueSet();
  assertTrue("Unique set contains the first element",set.contains(els[0]));
  assertEquals(true, bag.remove(els[0])); 
  set = ((PredicatedBag) bag).uniqueSet();
  assertTrue("Unique set now does not contain the first element",
    !set.contains(els[0])); 
}

代码示例来源: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

/**
 * Compare the current serialized form of the Bag
 * against the canonical version in CVS.
 */
public void testEmptyBagCompatibility() throws IOException, ClassNotFoundException {
  // test to make sure the canonical form has been preserved
  Bag bag = makeBag();
  if(bag instanceof Serializable && !skipSerializedCanonicalTests() && isTestSerialization()) {
    Bag bag2 = (Bag) readExternalFormFromDisk(getCanonicalEmptyCollectionName(bag));
    assertTrue("Bag is empty",bag2.size()  == 0);
    assertEquals(bag, bag2);
  }
}

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

/**
   * Compare the current serialized form of the Bag
   * against the canonical version in CVS.
   */
  public void testFullBagCompatibility() throws IOException, ClassNotFoundException {
    // test to make sure the canonical form has been preserved
    Bag bag = makeBag();
    bag.add("A");
    bag.add("A");
    bag.add("B");
    bag.add("B");
    bag.add("C");
    if(bag instanceof Serializable && !skipSerializedCanonicalTests() && isTestSerialization()) {
      Bag bag2 = (Bag) readExternalFormFromDisk(getCanonicalFullCollectionName(bag));
      assertEquals("Bag is the right size",bag.size(), bag2.size());
      assertEquals(bag, bag2);
    }
  }
}

代码示例来源:origin: uk.gov.dstl.baleen/baleen-entity-linking

/** @return the total size of the bag (not the number of unique words) */
public int size() {
 return words.size();
}

代码示例来源:origin: dstl/baleen

/** @return the total size of the bag (not the number of unique words) */
public int size() {
 return words.size();
}

相关文章