糊言亂語(yǔ)

          志未半斤, 才無(wú)八兩. 有苦有樂(lè), 糊涂過(guò)活。
          posts - 25, comments - 7, trackbacks - 0, articles - 42
            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          首先來(lái)看Bag組。

           

          Bag

          HashBag

          BagUtils

           

          Bag是在org.apache.commons.collections包中定義的接口,它extends java.util.Collection,而它的實(shí)現(xiàn)類(lèi)都被放在下面的bag包中。之所以有這樣一組類(lèi)型,是因?yàn)槲覀冇袝r(shí)候需要在Collection中存放多個(gè)相同對(duì)象的拷貝,并且需要很方便的取得該對(duì)象拷貝的個(gè)數(shù)。需要注意的一點(diǎn)是它雖然extends Collection,但是如果真把它完全當(dāng)作java.util.Collection來(lái)用會(huì)遇到語(yǔ)義上的問(wèn)題,詳細(xì)信息參考Javadoc。

           

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

           

          還是舉例子來(lái)看:

           

          /** 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();

           

              }

           

          }

           

          以下是運(yùn)行結(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

           

          需要說(shuō)明的是,以上的代碼僅僅為了演示如何使用Bag,實(shí)際應(yīng)用不建議像這樣硬編碼。
          主站蜘蛛池模板: 沂源县| 巧家县| 噶尔县| 永兴县| 晋州市| 故城县| 图们市| 威海市| 沅陵县| 宜丰县| 深泽县| 长阳| 桐梓县| 建宁县| 锦屏县| 临泉县| 麦盖提县| 高邑县| 获嘉县| 雅江县| 车致| 枝江市| 荣昌县| 库伦旗| 绩溪县| 上高县| 深圳市| 黎平县| 尼勒克县| 门源| 大洼县| 丰镇市| 宁城县| 乌海市| 昌黎县| 祥云县| 阳信县| 和顺县| 盐山县| 石屏县| 漳浦县|