Read Sean

          Read me, read Sean.
          posts - 508, comments - 655, trackbacks - 9, articles - 4

          [Jakarta Commons筆記] Commons Collections - Bag組

          Posted on 2005-08-03 12:36 laogao 閱讀(2616) 評論(2)  編輯  收藏 所屬分類: On Java

           

          首先來看Bag組。

           

          Bag

          HashBag

          BagUtils

           

          Bag是在org.apache.commons.collections包中定義的接口,它extends java.util.Collection,而它的實現(xiàn)類都被放在下面的bag包中。之所以有這樣一組類型,是因為我們有時候需要在Collection中存放多個相同對象的拷貝,并且需要很方便的取得該對象拷貝的個數(shù)。需要注意的一點是它雖然extends Collection,但是如果真把它完全當作java.util.Collection來用會遇到語義上的問題,詳細信息參考Javadoc

           

          HashBagBag接口的一個標準實現(xiàn)。而BagUtils提供一組static的方法讓調(diào)用者獲取經(jīng)過不同裝飾后的Bag實例。

           

          還是舉例子來看:

           

          /** Book.java */

           

          package sean.study.commons.collections;

           

          import org.apache.commons.lang.builder.ToStringBuilder;

          import org.apache.commons.lang.builder.ToStringStyle;

           

          public class Book {

             

              private String name;

              private String isbn;

              private double retailPrice;

             

              public Book() {

              }

             

              public Book(String name, String isbn, double retailPrice) {

                  this.name = name;

                  this.isbn = isbn;

                  this.retailPrice = retailPrice;

              }

             

              public String toString() {

                  return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)

                  .append("name", name)

                  .append("ISBN", isbn)

                  .append("retailPrice", retailPrice)

                  .toString();

              }

           

              public String getIsbn() {

                  return isbn;

              }

           

              public void setIsbn(String isbn) {

                  this.isbn = isbn;

              }

           

              public String getName() {

                  return name;

              }

           

              public void setName(String name) {

                  this.name = name;

              }

           

              public double getRetailPrice() {

                  return retailPrice;

              }

           

              public void setRetailPrice(double retailPrice) {

                  this.retailPrice = retailPrice;

              }

             

          }

           

          /** BagUsage.java */

           

          package sean.study.commons.collections;

           

          import org.apache.commons.collections.Bag;

          import org.apache.commons.collections.BagUtils;

          import org.apache.commons.collections.bag.HashBag;

          import org.apache.commons.lang.StringUtils;

           

          public class BagUsage {

           

              public static void main(String[] args) {

                  demoBagUsage();

              }

           

              public static void demoBagUsage() {

           

                  System.out.println(StringUtils.center(" demoBagUsage ", 40, "="));

           

                  // data setup

                  Book book1 = new Book("Refactoring Workbook", "7-5083-2208-8", 29.8);

                  Book book2 = new Book("J2EE Design Patterns", "7-5083-3099-4", 45);

                  Book book3 = new Book("Agile Software Development", "7-5083-1503-0", 59);

           

                  // create a bag

                  Bag myBag = BagUtils.typedBag(new HashBag(), Book.class);

                  myBag.add(book1, 360);

                  myBag.add(book2, 500);

                  myBag.add(book3, 170);

           

                  // calculations for a bag

                  double price1 = book1.getRetailPrice();

                  double price2 = book2.getRetailPrice();

                  double price3 = book3.getRetailPrice();

                  int book1Count = myBag.getCount(book1);

                  int book2Count = myBag.getCount(book2);

                  int book3Count = myBag.getCount(book3);

                  double totalValue = (price1 * book1Count) + (price2 * book2Count)

                          + (price3 * book3Count);

           

                  // dispaly results

                  System.out.println("There are " + book1Count + " copies of "

                          + book1.getName() + ".");

                  System.out.println("There are " + book2Count + " copies of "

                          + book2.getName() + ".");

                  System.out.println("There are " + book3Count + " copies of "

                          + book3.getName() + ".");

                  System.out.println("The total value of these books is: " + totalValue);

           

                  System.out.println();

           

              }

           

          }

           

          以下是運行結(jié)果:

           

          ============= demoBagUsage =============

          There are 360 copies of Refactoring Workbook.

          There are 500 copies of J2EE Design Patterns.

          There are 170 copies of Agile Software Development.

          The total value of these books is: 43258.0

           

          需要說明的是,以上的代碼僅僅為了演示如何使用Bag,實際應(yīng)用不建議像這樣硬編碼。

           

           

          Feedback

          # re: [Jakarta Commons筆記] Commons Collections - Bag組  回復(fù)  更多評論   

          2005-08-03 19:03 by emu
          想不出什么情形下需要用到。

          # re: [Jakarta Commons筆記] Commons Collections - Bag組  回復(fù)  更多評論   

          2005-08-04 09:13 by 大胃
          其實這個Bag接口對應(yīng)的是一個已經(jīng)有較明確定義的一種數(shù)據(jù)結(jié)構(gòu):
          http://www.nist.gov/dads/HTML/bag.html

          以往我們用Collection接口,或者是預(yù)定義的Map、List等,甚至是我們自己用數(shù)組實現(xiàn)的結(jié)構(gòu)實際上都可以模擬出這個bag數(shù)據(jù)結(jié)構(gòu)的行為。但是這樣做并不方便。Bag為我們提供了便利。

          我能夠想到的是,這個Bag在我們需要處理類似倉庫的系統(tǒng)中還是比較有用的,對于定義好的貨品,可以用枚舉,甚至簡單點用String也是可以,然后就可以成批add和remove,并且直接獲取當前數(shù)量進行盤點或者庫存查詢。如果沒有Bag,我們需要for循環(huán)去add和remove。或者如果你實現(xiàn)的是一個Map,那么你在add和remove時還需要自己去判斷是否已經(jīng)有該貨品存在,remove時甚至需要自己去檢查庫存是否夠用等等。

          Bag只是提供一些便利,如果你需要類似的功能,覺得好用,就用它,不好用的話,自己實現(xiàn)咯。
          主站蜘蛛池模板: 贵德县| 宝应县| 顺平县| 密云县| 绩溪县| 文成县| 萨迦县| 西丰县| 绵竹市| 安阳市| 德安县| 霍林郭勒市| 陈巴尔虎旗| 黄冈市| 南昌市| 庆阳市| 区。| 崇仁县| 邯郸市| 手机| 松溪县| 弥勒县| 锦州市| 陆川县| 阿图什市| 台南市| 凤台县| 淮阳县| 留坝县| 甘孜| 天津市| 合水县| 鹿邑县| 宁海县| 鄂尔多斯市| 广河县| 墨竹工卡县| 师宗县| 聂荣县| 广丰县| 乐东|