feezh

          我們之所以努力賺錢,是為了讓父母為自己買東西時(shí)能像給我們買東西時(shí)一樣大方!
          隨筆 - 7, 文章 - 0, 評(píng)論 - 10, 引用 - 0
          數(shù)據(jù)加載中……

          Java樹形數(shù)據(jù)(或者說級(jí)聯(lián))分類

          - 這幾天做東西時(shí)遇到一個(gè)樹形數(shù)據(jù)(或者說級(jí)聯(lián),不會(huì)描述了,就這么叫吧)分類的問題,開始有點(diǎn)糾結(jié),后想到了一種方法實(shí)現(xiàn),先分享出來,高手勿噴。

          正文開始
          現(xiàn)有三個(gè)類:分別是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;
              }


          可以看出 章節(jié) 有多個(gè)小節(jié),小節(jié)有多個(gè)知識(shí)點(diǎn)。當(dāng)取得知識(shí)點(diǎn)后怎么根據(jù)不同的章節(jié)和小節(jié)分類呢?

          List<KnowledgePoint> kps = this.allService.getKnowledgePointService()
                          .findPoints(kpIds);
          //取得所選擇的知識(shí)點(diǎn)
                      /********** 根據(jù)小節(jié)分類知識(shí)點(diǎn) **********/            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));
                  }

                 
          /********** 根據(jù)章節(jié)分類小節(jié) **********/
                      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 閱讀(2594) 評(píng)論(1)  編輯  收藏 所屬分類: Java相關(guān)

          評(píng)論

          # website design bangalore  回復(fù)  更多評(píng)論   

          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
          主站蜘蛛池模板: 准格尔旗| 阿拉善左旗| 万荣县| 陕西省| 仁布县| 阜新市| 汽车| 和平县| 通渭县| 辽阳县| 朝阳市| 顺平县| 乐亭县| 五峰| 八宿县| 惠东县| 高淳县| 华池县| 大姚县| 蒲城县| 平利县| 吉水县| 固原市| 嘉鱼县| 尼勒克县| 呼图壁县| 灵璧县| 内江市| 宁波市| 墨竹工卡县| 诏安县| 平昌县| 卓尼县| 慈溪市| 武鸣县| 沙雅县| 无极县| 睢宁县| 淮南市| 临颍县| 吉水县|