老做不出来的题。求改一下, 知道哪错了不知道怎么改

java131313 2011-10-18 07:27:38
/*
* 在一个Map中,记录了某学校每门课程的授课老师(key为课程名,value为老师名),请分析原有Map,创建一个新的Map,用来表示每个老师
* 负责讲授哪些课程 新的Map的key应为老师的名字,value为一个Set,Set中的元素为课程的名字
*/
public class Test06 {
public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>();
map.put("数据结构", "王老师");
map.put("线性代数", "王老师");
map.put("C语言程序设计", "高老师");
map.put("微机原理", "高老师");
map.put("Java语言程序设计", "李老师");
// Map<String, String> map1 = new HashMap<String, String>();
Set<String> set = new HashSet<String>();

Map<String, Set<String>> resMap = new HashMap<String,Set<String> >();

Set<Map.Entry<String , String>> set1 =map.entrySet();//从原里的map中取出Map.entry
Set<String> set2= new HashSet<String>();
for(Map.Entry<String , String> s:set1){
//System.out.println(s.getKey()+"---"+s.getValue());
set.add(s.getValue());//取出value即老师来,放到set中,确保老量的唯一性
}


for(String cn:set){//取出老师来
for(Map.Entry<String , String> s:set1){//来原来的老师比较
// System.out.println(cn);
if(cn.equals(s.getValue())){
//System.out.println(s.getValue());
set2.add(s.getKey());//把学科加入到set2中确保唯一性
// System.out.println(s.getValue()+":"+s.getKey());
}

//System.out.println(s.getKey()+"---"+s.getValue());
//set.add(s.getValue());
}
resMap.put(cn, set2);
}

Set<Entry<String, Set<String>>> set3 =resMap.entrySet();
for(Map.Entry<String, Set<String>> s:set3){
//System.out.println(s.getKey()+"---"+s.getValue());
System.out.println(s.getKey());
Set<String> st = s.getValue();
for(String s1:st){
//System.out.println(s.getKey()+"---"+s.getValue());
System.out.println(s1);
}

}

// Iterator it =map
}


}
...全文
98 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
magong 2011-10-18
  • 打赏
  • 举报
回复

public class Test06 {
public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>();
map.put("数据结构", "王老师");
map.put("线性代数", "王老师");
map.put("C语言程序设计", "高老师");
map.put("微机原理", "高老师");
map.put("Java语言程序设计", "李老师");
// Map<String, String> map1 = new HashMap<String, String>();
Set<String> set = new HashSet<String>();

Map<String, Set<String>> resMap = new HashMap<String,Set<String> >();

Set<Map.Entry<String , String>> set1 =map.entrySet();//从原里的map中取出Map.entry
for(Map.Entry<String , String> s:set1){
if(!resMap.containsKey(s.getValue()))
resMap.put(s.getValue(), new HashSet<String>());
resMap.get(s.getValue()).add(s.getKey());
}

Set<Entry<String, Set<String>>> set3 =resMap.entrySet();
for(Map.Entry<String, Set<String>> s:set3){
//System.out.println(s.getKey()+"---"+s.getValue());
System.out.println(s.getKey());
Set<String> st = s.getValue();
for(String s1:st){
//System.out.println(s.getKey()+"---"+s.getValue());
System.out.println(s1);
}

}

// Iterator it =map
}


}
chinalian 2011-10-18
  • 打赏
  • 举报
回复
最后一个for循环之前的Set<Entry<String, Set<String>>> set3 =resMap.entrySet();
更改为:
Set<Map.Entry<String, Set<String>>> set3 =resMap.entrySet();
licip 2011-10-18
  • 打赏
  • 举报
回复
包是没有导入进来吧。包应该不少吧。
孟祥月 2011-10-18
  • 打赏
  • 举报
回复
代码少东西吧
波杰克男 2011-10-18
  • 打赏
  • 举报
回复
我复制到eclipse中 怎么那么多错? 是不是缺少包啊

62,628

社区成员

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

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