feezh

          我們之所以努力賺錢,是為了讓父母為自己買東西時能像給我們買東西時一樣大方!
          隨筆 - 7, 文章 - 0, 評論 - 10, 引用 - 0

          導航

          <2013年2月>
          272829303112
          3456789
          10111213141516
          17181920212223
          242526272812
          3456789

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          收藏夾

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          Java樹形數據(或者說級聯)分類

          - 這幾天做東西時遇到一個樹形數據(或者說級聯,不會描述了,就這么叫吧)分類的問題,開始有點糾結,后想到了一種方法實現,先分享出來,高手勿噴。

          正文開始
          現有三個類:分別是Chapter.java Section.java KnowledgePoint.java
          Chapter.java
          private Set sections = new HashSet(0);
          public Set getSections() {
                  
          return this.sections;
              }


          public void setSections(Set sections) {
                  
          this.sections = sections;
              }

          Section.java
          private Chapter chapter;
          private Set knowledgePoints = new HashSet(0);
          public Chapter getChapter() {
                 
          return this.chapter;
              }


          public void setChapter(Chapter chapter) {
                 
          this.chapter = chapter;
              }

          public Set getKnowledgePoints() {
                 
          return this.knowledgePoints;
              }


          public void setKnowledgePoints(Set knowledgePoints) {
                 
          this.knowledgePoints = knowledgePoints;
              }


          KnowledgePoint.java
          private Section section;
          public Section getSection() {
                 
          return this.section;
              }


          public void setSection(Section section) {
                 
          this.section = section;
              }


          可以看出 章節 有多個小節,小節有多個知識點。當取得知識點后怎么根據不同的章節和小節分類呢?

          List<KnowledgePoint> kps = this.allService.getKnowledgePointService()
                          .findPoints(kpIds);
          //取得所選擇的知識點
                      /********** 根據小節分類知識點 **********/            Map<Integer, Section> map = new HashMap<Integer, Section>();
                 
          for (int i = 0; i < kps.size(); i++) {
                      Section section
          = kps.get(i).getSection();
                      Integer key
          = section.getId();
                     
          if (!map.containsKey(key)) {
                          section.getKnowledgePoints().clear();
                          map.put(key, section);
                      }

                      map.get(key).getKnowledgePoints().add(kps.get(i));
                  }

                 
          /********** 根據章節分類小節 **********/
                      Map
          <Integer, Chapter> cpMap = new HashMap<Integer, Chapter>();
                  Iterator
          <Section> it = map.values().iterator();
                 
          while (it.hasNext()) {
                      Section section
          = it.next();
                      Chapter chapter
          = section.getChapter();
                      Integer key
          = chapter.getId();
                     
          if (!cpMap.containsKey(key)) {
                          chapter.getSections().clear();
                          cpMap.put(key, chapter);
                      }

                      cpMap.get(key).getSections().add(section);
                  }



          posted on 2012-05-29 21:41 feezh 閱讀(2595) 評論(1)  編輯  收藏 所屬分類: Java相關

          評論

          # website design bangalore  回復  更多評論   

          well written, find your style of writing is very rich. Written in a very good, hope you can continue to try hard, to write better!
          2013-02-05 16:40 | web design bangalore
          主站蜘蛛池模板: 日照市| 鱼台县| 孟州市| 苗栗县| 建昌县| 永德县| 罗源县| 新邵县| 鄯善县| 临武县| 呼和浩特市| 莒南县| 松滋市| 北碚区| 临漳县| 台南市| 杭锦后旗| 梨树县| 栾城县| 威宁| 黎平县| 当阳市| 含山县| 景宁| 通海县| 勐海县| 沭阳县| 手机| 云浮市| 南汇区| 平利县| 镇江市| 开封县| 碌曲县| 肥西县| 南雄市| 温泉县| 七台河市| 宁阳县| 武穴市| 铜鼓县|