帮忙看看集合的问题

dsjflwe 2016-12-17 07:03:24
下面是关于新闻发布系统运用到List+Map+oop技术的程序,2. 实现新闻发布系统中的 n个类别对应 该类别新闻的数据结构并输出 所有类别以及该类别下的所有新闻, 实现添加,修改,删除,查询的功能。程序如下:
第一个类:
package News;

public class Kind {
private String newsKind;


public Kind(String newsKind){
this.newsKind=newsKind;
}
public String getNewsKind() {
return newsKind;
}

public void setNewsKind(String newsKind) {
this.newsKind = newsKind;
}

}

第二个类:
package News;

import java.util.Date;

public class News {
private String theme;
private Date date;
private String place;
private int viewNum;

public News(String theme,Date date,String place,int viewNum) {
this.theme=theme;
this.date=date;
this.place=place;
this.viewNum=viewNum;
}

public String getTheme() {
return theme;
}

public void setTheme(String theme) {
this.theme = theme;
}

public Date getDate() {
return date;
}

public void setDate(Date date) {
this.date = date;
}

public String getPlace() {
return place;
}

public void setPlace(String place) {
this.place = place;
}

public int getViewNum() {
return viewNum;
}

public void setViewNum(int viewNum) {
this.viewNum = viewNum;
}

}

第三个类:
package News;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

public class NewsDemo1 {
public static void main(String[] args) throws Exception {
//创建新闻对象
Date date =new Date();
News new1=new News("广东一老人买排骨没付款被超市扣留罚700",date, "广东", 3427);
News new2=new News("特朗普首场新闻发布会推迟",date, "美国", 111223);
News new3=new News("中关村二小最新回应",date, "美国", 2235878);
News new4=new News("2017版的三星A7/A5/A3重点规格参数曝光",date, "韩国", 34567);
News new5=new News("朝韩虚拟战争已打响",date, "韩国", 239254);
News new6=new News("厉以宁:中国正发生新人口红利 红利来自农村",date, "中国", 22343);
News new7=new News("揭秘韩国网络代购黑幕:假货只卖给中国人",date, "韩国", 36055);
//创建新闻类型
Kind politic=new Kind("时事政治"); //
Kind dailyNews=new Kind("民生");
Kind army=new Kind("军事");
Kind internet=new Kind("互联网");
//用hashMap组装新闻对象与新闻类型
HashMap<Kind, News> hashMap1=new HashMap<Kind, News>();
HashMap<Kind, News> hashMap2=new HashMap<Kind, News>();
HashMap<Kind, News> hashMap3=new HashMap<Kind, News>();
HashMap<Kind, News> hashMap4=new HashMap<Kind, News>();
HashMap<Kind, News> hashMap5=new HashMap<Kind, News>();
HashMap<Kind, News> hashMap6=new HashMap<Kind, News>();
HashMap<Kind, News> hashMap7=new HashMap<Kind, News>();

hashMap1.put(dailyNews, new1);
hashMap2.put(politic, new2);
hashMap3.put(dailyNews, new3);
hashMap4.put(internet, new4);
hashMap5.put(army, new5);
hashMap6.put(dailyNews, new6);
hashMap7.put(internet, new7);
//用arraylist装填hashMap
ArrayList<HashMap<Kind, News>> list=new ArrayList<HashMap<Kind, News>>();
list.add(hashMap1);
list.add(hashMap2);
list.add(hashMap3);
list.add(hashMap4);
list.add(hashMap5);
list.add(hashMap6);
list.add(hashMap7);

//用Iterator遍历所有对象
Iterator iterator= list.iterator();
while(iterator.hasNext()){ //使用hasNext()检查序列中是否还有元素
HashMap<Kind, News> hashMap= (HashMap<Kind, News>) iterator.next();
Iterator iterator2= hashMap.entrySet().iterator();
while (iterator2.hasNext()) {
Map.Entry entry=(Entry)iterator2.next();
Kind key=(Kind) entry.getKey();
News value=(News) entry.getValue();
System.out.println("今天的新闻有以下内容:"+key.getNewsKind()+" 新闻的主题为"+value.getTheme()+" 事件发生的地点为"+value.getPlace()+" 发生日期为"+value.getDate()+" 评论量为"+value.getViewNum()+" 条");
}
}
}
}

请问下面每一句代表什么意思?
while(iterator.hasNext()){ //使用hasNext()检查序列中是否还有元素
HashMap<Kind, News> hashMap= (HashMap<Kind, News>) iterator.next();
Iterator iterator2= hashMap.entrySet().iterator();
while (iterator2.hasNext()) {
Map.Entry entry=(Entry)iterator2.next();
Kind key=(Kind) entry.getKey();
News value=(News) entry.getValue();

为什么要循环2次?已经创建了iterator为什么还要创建iterator2,特别是第2、5、6句看不明白,请高手指点,谢谢
...全文
249 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Be_nurturing 2016-12-19
  • 打赏
  • 举报
回复
1.这两层循环,相当于,你有N个货物,第一步先把货物放在了N个不同的盒子里,第二部把N个盒子有放在了一个大货车里。 2.我感觉用map 包含list 更好一些,一类新闻放在一个list中,然后map把每个新闻的类别对应一个list, map.put("军事",armList),map.put("民生",dailyList)其他的一样,定义一个map就可以了,不用定义那么多map浪费。
dsjflwe 2016-12-17
  • 打赏
  • 举报
回复
我再问具体一点吧: while(iterator.hasNext()){ //使用hasNext()检查序列中是否还有元素 HashMap<Kind, News> hashMap= (HashMap<Kind, News>) iterator.next(); 这一句(HashMap<Kind, News>)的作用是什么,为什么外面要加括号()? Iterator iterator2= hashMap.entrySet().iterator(); 这一句hashMap.entrySet()的作用是什么? while (iterator2.hasNext()) { Map.Entry entry=(Entry)iterator2.next(); 这一句Map.Entry和(Entry)的作用是什么,(Entry)为什么要加括号? News value=(News) entry.getValue(); 这一句(News)的作用是什么?为什么要加括号?
qq_31404055 2016-12-17
  • 打赏
  • 举报
回复
因为list包着map啊 而你要的数据在map里面所以有两个iterator,第二句就是把list里的map取出来,后面是把map中的数据取出来

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧