本文整理汇总了Java中org.apache.commons.collections15.Bag类的典型用法代码示例。如果您正苦于以下问题:Java Bag类的具体用法?Java Bag怎么用?Java Bag使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Bag类属于org.apache.commons.collections15包,在下文中一共展示了Bag类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Java代码示例。
示例1: retainAll
import org.apache.commons.collections15.Bag; //导入依赖的package包/类
/**
* Remove any members of the bag that are not in the given
* bag, respecting cardinality.
*
* @param other the bag to retain
* @return <code>true</code> if this call changed the collection
* @see #retainAll(Collection)
*/
boolean retainAll(Bag<E> other) {
boolean result = false;
Bag excess = new HashBag();
Iterator<E> i = uniqueSet().iterator();
while (i.hasNext()) {
E 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;
}
开发者ID:jgaltidor,项目名称:VarJ,代码行数:28,代码来源:AbstractMapBag.java
示例2: equals
import org.apache.commons.collections15.Bag; //导入依赖的package包/类
/**
* 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<E> it = map.keySet().iterator(); it.hasNext();) {
E element = it.next();
if (other.getCount(element) != getCount(element)) {
return false;
}
}
return true;
}
开发者ID:jgaltidor,项目名称:VarJ,代码行数:28,代码来源:AbstractMapBag.java
示例3: testRemove
import org.apache.commons.collections15.Bag; //导入依赖的package包/类
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"));
}
开发者ID:jgaltidor,项目名称:VarJ,代码行数:19,代码来源:AbstractTestBag.java
示例4: testRemoveAll
import org.apache.commons.collections15.Bag; //导入依赖的package包/类
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());
}
开发者ID:jgaltidor,项目名称:VarJ,代码行数:17,代码来源:AbstractTestBag.java
示例5: testContains
import org.apache.commons.collections15.Bag; //导入依赖的package包/类
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"));
}
开发者ID:jgaltidor,项目名称:VarJ,代码行数:19,代码来源:AbstractTestBag.java
示例6: testSize
import org.apache.commons.collections15.Bag; //导入依赖的package包/类
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());
}
开发者ID:jgaltidor,项目名称:VarJ,代码行数:20,代码来源:AbstractTestBag.java
示例7: testIterator
import org.apache.commons.collections15.Bag; //导入依赖的package包/类
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"));
}
开发者ID:jgaltidor,项目名称:VarJ,代码行数:26,代码来源:AbstractTestBag.java
示例8: testIteratorFailNoMore
import org.apache.commons.collections15.Bag; //导入依赖的package包/类
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
}
}
开发者ID:jgaltidor,项目名称:VarJ,代码行数:17,代码来源:AbstractTestBag.java
示例9: testIteratorFailDoubleRemove
import org.apache.commons.collections15.Bag; //导入依赖的package包/类
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());
}
开发者ID:jgaltidor,项目名称:VarJ,代码行数:23,代码来源:AbstractTestBag.java
示例10: testToArray
import org.apache.commons.collections15.Bag; //导入依赖的package包/类
public void testToArray() {
Bag bag = makeBag();
bag.add("A");
bag.add("A");
bag.add("B");
bag.add("B");
bag.add("C");
Object[] array = bag.toArray();
int a = 0, b = 0, c = 0;
for (int i = 0; i < array.length; i++) {
a += (array[i].equals("A") ? 1 : 0);
b += (array[i].equals("B") ? 1 : 0);
c += (array[i].equals("C") ? 1 : 0);
}
assertEquals(2, a);
assertEquals(2, b);
assertEquals(1, c);
}
开发者ID:jgaltidor,项目名称:VarJ,代码行数:19,代码来源:AbstractTestBag.java
示例11: testToArrayPopulate
import org.apache.commons.collections15.Bag; //导入依赖的package包/类
public void testToArrayPopulate() {
Bag bag = makeBag();
bag.add("A");
bag.add("A");
bag.add("B");
bag.add("B");
bag.add("C");
String[] array = (String[]) bag.toArray(new String[0]);
int a = 0, b = 0, c = 0;
for (int i = 0; i < array.length; i++) {
a += (array[i].equals("A") ? 1 : 0);
b += (array[i].equals("B") ? 1 : 0);
c += (array[i].equals("C") ? 1 : 0);
}
assertEquals(2, a);
assertEquals(2, b);
assertEquals(1, c);
}
开发者ID:jgaltidor,项目名称:VarJ,代码行数:19,代码来源:AbstractTestBag.java
示例12: testEquals
import org.apache.commons.collections15.Bag; //导入依赖的package包/类
public void testEquals() {
Bag bag = makeBag();
Bag bag2 = makeBag();
assertEquals(true, bag.equals(bag2));
bag.add("A");
assertEquals(false, bag.equals(bag2));
bag2.add("A");
assertEquals(true, bag.equals(bag2));
bag.add("A");
bag.add("B");
bag.add("B");
bag.add("C");
bag2.add("A");
bag2.add("B");
bag2.add("B");
bag2.add("C");
assertEquals(true, bag.equals(bag2));
}
开发者ID:jgaltidor,项目名称:VarJ,代码行数:19,代码来源:AbstractTestBag.java
示例13: testEqualsHashBag
import org.apache.commons.collections15.Bag; //导入依赖的package包/类
public void testEqualsHashBag() {
Bag bag = makeBag();
Bag bag2 = new HashBag();
assertEquals(true, bag.equals(bag2));
bag.add("A");
assertEquals(false, bag.equals(bag2));
bag2.add("A");
assertEquals(true, bag.equals(bag2));
bag.add("A");
bag.add("B");
bag.add("B");
bag.add("C");
bag2.add("A");
bag2.add("B");
bag2.add("B");
bag2.add("C");
assertEquals(true, bag.equals(bag2));
}
开发者ID:jgaltidor,项目名称:VarJ,代码行数:19,代码来源:AbstractTestBag.java
示例14: testHashCode
import org.apache.commons.collections15.Bag; //导入依赖的package包/类
public void testHashCode() {
Bag bag = makeBag();
Bag bag2 = makeBag();
assertEquals(0, bag.hashCode());
assertEquals(0, bag2.hashCode());
assertEquals(bag.hashCode(), bag2.hashCode());
bag.add("A");
bag.add("A");
bag.add("B");
bag.add("B");
bag.add("C");
bag2.add("A");
bag2.add("A");
bag2.add("B");
bag2.add("B");
bag2.add("C");
assertEquals(bag.hashCode(), bag2.hashCode());
int total = 0;
total += ("A".hashCode() ^ 2);
total += ("B".hashCode() ^ 2);
total += ("C".hashCode() ^ 1);
assertEquals(total, bag.hashCode());
assertEquals(total, bag2.hashCode());
}
开发者ID:jgaltidor,项目名称:VarJ,代码行数:26,代码来源:AbstractTestBag.java
示例15: testFullBagSerialization
import org.apache.commons.collections15.Bag; //导入依赖的package包/类
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());
}
开发者ID:jgaltidor,项目名称:VarJ,代码行数:17,代码来源:AbstractTestBag.java
示例16: setCount
import org.apache.commons.collections15.Bag; //导入依赖的package包/类
/**
* Set the counter of the given entry in the given set.
*
* @param bag
* The bag to modify
* @param entry
* The entry whose counter is set
* @param count
* The new non-negative counter value
*/
public static <T> void setCount(Bag<T> bag, T entry, int count) {
if (bag == null)
throw new IllegalArgumentException();
else if (count < 0)
throw new IllegalArgumentException();
int oldCount = bag.getCount(entry);
if (count > oldCount)
bag.add(entry, count - oldCount);
else if (count < oldCount)
bag.remove(entry, oldCount - count);
}
开发者ID:KeepTheBeats,项目名称:alevin-svn2,代码行数:24,代码来源:BagUtil.java
示例17: report
import org.apache.commons.collections15.Bag; //导入依赖的package包/类
public void report(PrintStream ps) {
ps.println("Collected " + plots.size() + " plots");
Bag<String> plotTypeCounts = new HashBag<>();
for (Plot plot : plots) {
plotTypeCounts.add(plot.getPlotType());
}
ps.println("Found PlotTypes:");
for (String plotType : plotTypeCounts.uniqueSet()) {
ps.println("\t" + plotType + ": " + plotTypeCounts.getCount(plotType));
}
ps.println("Created " + traitByName.size() + " Traits");
for (Trait trait : traitByName.values()) {
ps.println("\t" + trait + " [id=" + trait.getTraitId() + "]");
}
ps.println("Created " + plotAttributeByName.size() + " Plot Attributes");
for (PlotAttribute pa : plotAttributeByName.values()) {
ps.println("\t" + pa);
}
ps.println("Created " + nPlotAttributeValues + " Plot Attribute Values");
ps.println("Created " + nSamples + " Samples");
if (! lineNumbersByMessage.isEmpty()) {
ps.println("Warnings:");
for (String msg : lineNumbersByMessage.keySet()) {
Set<Integer> range = lineNumbersByMessage.get(msg);
ps.println(range.size()+" times: " + msg);
}
}
}
开发者ID:kddart,项目名称:kdxplore,代码行数:37,代码来源:BmsExcelImportCallable.java
示例18: containsAll
import org.apache.commons.collections15.Bag; //导入依赖的package包/类
/**
* Determines if the bag contains the given elements.
*
* @param coll the collection to check against
* @return <code>true</code> if the Bag contains all the collection
*/
public boolean containsAll(Collection<?> coll) {
if (coll instanceof Bag) {
return containsAll((Bag<E>) coll);
}
return containsAll(new HashBag(coll));
}
开发者ID:jgaltidor,项目名称:VarJ,代码行数:13,代码来源:AbstractMapBag.java
示例19: testTransformedBag
import org.apache.commons.collections15.Bag; //导入依赖的package包/类
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])));
}
开发者ID:jgaltidor,项目名称:VarJ,代码行数:16,代码来源:TestTransformedBag.java
示例20: testOrdering
import org.apache.commons.collections15.Bag; //导入依赖的package包/类
public void testOrdering() {
Bag bag = setupBag();
assertEquals("Should get elements in correct order", "A", bag.toArray()[0]);
assertEquals("Should get elements in correct order", "B", bag.toArray()[1]);
assertEquals("Should get elements in correct order", "C", bag.toArray()[2]);
assertEquals("Should get first key", "A", ((SortedBag) bag).first());
assertEquals("Should get last key", "D", ((SortedBag) bag).last());
}
开发者ID:jgaltidor,项目名称:VarJ,代码行数:9,代码来源:TestTreeBag.java
注:本文中的org.apache.commons.collections15.Bag类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论