/*
* 在一个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
}
}
* 在一个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
}
}
更改为:
Set<Map.Entry<String, Set<String>>> set3 =resMap.entrySet();
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
}
}