Bag可以储存一个对象很多副本,并跟踪这些副本
实例如下:
Bag bag=new HashBag();
bag.add("TEST1",100);
bag.add("TEST2",500);
int test1Count=bag.getCount("TEST1");
int test2Count = bag.getCount( "TEST2" );
System.out.println( "Counts: TEST1: " + test1Count + ", TEST2: " + test2Count );
bag.remove("TEST1",1);
bag.remove( "TEST2", 10 );
test1Count = bag.getCount( "TEST1" );
test2Count = bag.getCount( "TEST2" );
System.out.println( "Counts: TEST1: " + test1Count + ", TEST2: " + test2Count );
结果如下:
Counts: TEST1: 100, TEST2: 500
Counts: TEST1: 99, TEST2: 490
Bag有两种实现分别是HashBag和TreeBag,它们分别用HashMap和TreeMap来存储Bag的内容。性能上来说,一般使用HashBag为佳,而当需要保持顺序时,我们使用TreeBag。
实例如下:使用Bag来跟踪存货清单
package com.discursive.jccook.collections.bag;
import java.text.NumberFormat;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;
import org.apache.commons.collections.Bag;
import org.apache.commons.collections.bag.HashBag;
import org.apache.commons.lang.StringUtils;
public class BagExample {
Bag inventoryBag = new HashBag( );
// Define 4 Albums
Album album1 = new Album( "Radiohead", "OK Computer" );
Album album2 = new Album( "Kraftwerk", "The Man-Machine" );
Album album3 = new Album( "Charlie Parker", "Now's the Time" );
Album album4 = new Album( "ABBA", "ABBA - Gold: Greatest Hits" );
public static void main(String[] args) {
BagExample example = new BagExample( );
example.start( );
}
/**
* The start() method models the behavior the three customers.Each customer creates
* a Bag instance, shoppingBag, which holds the Album object she wishes to purchase.
*/
private void start(){
// Read our inventory into a Bag
populateInventory();
System.out.println("Inventory before Transactions");
printAlbums(inventoryBag);
printSeparator();
// A Customer wants to purchase 500 ABBA, 2 Radiohead, and 150 Parker
Bag shoppingCart1=new HashBag();
shoppingCart1.add(album4,500);
shoppingCart1.add( album3, 150 );
shoppingCart1.add( album1, 2 );
checkout(shoppingCart1,"Customer 1");
// Another Customer wants to purchase 600 copies of ABBA
Bag shoppingCart2 = new HashBag( );
shoppingCart2.add( album4, 600 );
checkout( shoppingCart2, "Customer 2" );
// Another Customer wants to purchase 3 copies of Kraftwerk
Bag shoppingCart3 = new HashBag( );
shoppingCart3.add( album2, 3 );
checkout( shoppingCart3, "Customer 3" );
System.out.println( "Inventory after Transactions" );
printAlbums( inventoryBag );
}
/**
* Albums are stored in the inventoryBag variable, which is populated by a call to
* populateInventory() method
*/
private void populateInventory(){
inventoryBag.add(album1,200);
inventoryBag.add(album2, 100 );
inventoryBag.add(album3, 500 );
inventoryBag.add(album4, 900 );
}
/**
* The printAlbums() method demonstrates how a Bag's iterator will iterator through all of the
* distinct objects stored in a Bag, printing out the count for each album by calling getCount()
* on the inventoryBag.
* @param albumBag
*/
private void printAlbums(Bag albumBag){
Set albums=albumBag.uniqueSet();
Iterator albumIterator=albums.iterator();
while(albumIterator.hasNext()){
Album album=(Album)albumIterator.next();
NumberFormat format=NumberFormat.getInstance();
format.setMinimumIntegerDigits(3);
format.setMaximumFractionDigits(0);
System.out.println("\t"+format.format(albumBag.getCount(album))+
"-"+album.getBand());
}
}
/**
* When a customer checks out of the store, the containsAll() method is called to make
* sure that the inventoryBag contains adequate inventory to fulfill a customer's order.
* If a customer attempts to buy 40 copies of an album, we create a Bag with 40 instances
* of the Album object, and containsAll( ) will only return true if the inventoryBag contains
* at least 40 matching albums. Certain that the order can be fulfilled, removeAll( ) reduces
* the number of albums in the inventoryBag by 40 and the customer's transaction is considered
* complete.Each customer transaction is modeled by a Bag that is subtracted from the inventoryBag
* using the removeAll( ) method
* @param shoppingCart
* @param customer
*/
private void checkout(Bag shoppingCart, String customer){
if(inventoryBag.containsAll((Collection)shoppingCart)){
inventoryBag.removeAll((Collection)shoppingCart);
System.out.println(customer+" purchased the following items:");
printAlbums(shoppingCart);
}else{
System.out.println(customer+", I'm sorry but we are unable to fill your order.");
}
printSeparator();
}
private void printSeparator(){
System.out.println(StringUtils.repeat("*", 65));
}
结果如下:
Inventory before Transactions
900-ABBA
100-Kraftwerk
500-Charlie Parker
200-Radiohead
*****************************************************************
Customer 1 purchased the following items:
500-ABBA
150-Charlie Parker
002-Radiohead
*****************************************************************
Customer 2, I'm sorry but we are unable to fill your order.
*****************************************************************
Customer 3 purchased the following items:
003-Kraftwerk
*****************************************************************
Inventory after Transactions
400-ABBA
097-Kraftwerk
350-Charlie Parker
198-Radiohead