本文整理了Java中org.apache.commons.collections.Bag
类的一些代码示例,展示了Bag
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Bag
类的具体详情如下:
包路径:org.apache.commons.collections.Bag
类名称:Bag
[英]Defines a collection that counts the number of times an object appears in the collection.
Suppose you have a Bag that contains {a, a, b, c}
. Calling #getCount(Object) on a
would return 2, while calling #uniqueSet() would return {a, b, c}
.
NOTE: This interface violates the Collection contract. The behavior specified in many of these methods is not the same as the behavior specified by Collection
. The noncompliant methods are clearly marked with "(Violation)". Exercise caution when using a bag as a Collection
.
This violation resulted from the original specification of this interface. In an ideal world, the interface would be changed to fix the problems, however it has been decided to maintain backwards compatibility instead.
[中]定义一个集合,该集合统计对象在集合中出现的次数。
假设您有一个包含{a, a, b, c}
的包。在a
上调用#getCount(Object)将返回2,而调用#uniqueSet()将返回[$2$]。
注意:此接口违反了收款合同。其中许多方法中指定的行为与Collection
指定的行为不同。不符合的方法清楚地标记为“(违反)”。将袋子用作Collection
时要小心。
此冲突是由此接口的原始规范引起的。在理想的情况下,接口会被更改以解决问题,但已决定改为保持向后兼容性。
代码示例来源: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 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
/**
* 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
/**
* 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
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 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 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 int getCount(Object object) {
synchronized (lock) {
return getBag().getCount(object);
}
}
代码示例来源: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 boolean add(Object object, int count) {
synchronized (lock) {
return getBag().add(object, count);
}
}
代码示例来源: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 Set uniqueSet() {
return getBag().uniqueSet();
}
代码示例来源:origin: commons-collections/commons-collections
public void testIteratorFail() {
Bag bag = makeBag();
bag.add("A");
bag.add("A");
bag.add("B");
Iterator it = bag.iterator();
it.next();
bag.remove("A");
try {
it.next();
fail("Should throw ConcurrentModificationException");
} catch (ConcurrentModificationException e) {
// expected
}
}
代码示例来源: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 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 boolean remove(Object object, int count) {
synchronized (lock) {
return getBag().remove(object, count);
}
}
代码示例来源:origin: commons-collections/commons-collections
public void testContains() {
Bag bag = makeBag();
assertEquals("Bag does not have at least 1 'A'", false, bag.contains("A"));
assertEquals("Bag does not have at least 1 'B'", false, bag.contains("B"));
bag.add("A"); // bag 1A
assertEquals("Bag has at least 1 'A'", true, bag.contains("A"));
assertEquals("Bag does not have at least 1 'B'", false, bag.contains("B"));
bag.add("A"); // bag 2A
assertEquals("Bag has at least 1 'A'", true, bag.contains("A"));
assertEquals("Bag does not have at least 1 'B'", false, bag.contains("B"));
bag.add("B"); // bag 2A,1B
assertEquals("Bag has at least 1 'A'", true, bag.contains("A"));
assertEquals("Bag has at least 1 'B'", true, bag.contains("B"));
}
代码示例来源:origin: hopshadoop/hopsworks
private void removeLocal(MaterialKey key, String materializationDirectory) {
Bag materialBag = materializedCerts.get(key);
if (materialBag != null) {
materialBag.remove(materializationDirectory, 1);
if (materialBag.getCount(materializationDirectory) <= 0) {
scheduleFileRemover(key, materializationDirectory);
}
}
}
代码示例来源:origin: commons-collections/commons-collections
public void testIteratorFailNoMore() {
Bag bag = makeBag();
bag.add("A");
bag.add("A");
bag.add("B");
Iterator it = bag.iterator();
it.next();
it.next();
it.next();
try {
it.next();
fail("Should throw NoSuchElementException");
} catch (NoSuchElementException ex) {
// expected
}
}
内容来源于网络,如有侵权,请联系作者删除!