A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© danerchen 初级黑马   /  2014-5-6 00:17  /  903 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

我一般要把Map迭代的话都是用这个代码:
Set entrySet = map.entrySet();
Iterator it = entrySet.iterator();       
while(iter1.hasNext()){                       
Map.Entry me = (Map.Entry)iter1.next();                       
System.out.println("key=" + me.getKey() + " value=" + me.getValue());
但是当我想用增强for循环的时候就只能写:
for(Entry<K,V>entry:map.entryset()){}
为什么不能写for(Set<Map.Entry<K,V>> entry:map.entryset())?
查看API文档里的Map接口的entrySet()方法,它返回的的确是一个Set集合啊,为什么这里编译不能通过?
还有Entry<K,V>是什么类啊?为什么API文档里找不到啊?
求高手解答

2 个回复

倒序浏览
  1. public class Demo5
  2. {


  3.         public static void main(String[] args)
  4.         {
  5.                
  6.                 Map<Integer, String> m = new HashMap();
  7.                
  8.                 m.put(1, "111");
  9.                 m.put(2, "222");
  10.                 m.put(3, "333");
  11.                 m.put(4, "444");
  12.                 m.put(5, "555");
  13.                
  14.                 Set entrySet = m.entrySet();
  15.                 Iterator it = entrySet.iterator();        
  16.                 while(it.hasNext())
  17.                 {                        
  18.                 Map.Entry me = (Map.Entry)it.next();                        
  19.                 System.out.println("key=" + me.getKey() + " value=" + me.getValue());
  20.                 }
  21.                 System.out.println("-------");

  22.                 for(Entry<Integer, String> o : m.entrySet() )
  23.                 {
  24.                         System.out.println("key=" + o.getKey() + " value=" + o.getValue());
  25.                 }

  26.         }
复制代码


set底层调用的还是map集合。而且你使用高级for循环的话先指定返回数据的类型。
Entry<K,V>不是什么类,Map.Entry<K,V>这是一个接口。

评分

参与人数 1技术分 +1 收起 理由
zzkang0206 + 1

查看全部评分

回复 使用道具 举报
  1. import java.util.*;
  2. class  MapTest6
  3. {
  4.         public static void main(String[] args)
  5.         {
  6.                 List<List<Student>> shiyan = new ArrayList<List<Student>>();

  7.                 List<Student> java01 = new ArrayList<Student>();
  8.                 List<Student> java02 = new ArrayList<Student>();

  9.                 shiyan.add("java01"+java01);
  10.                 shiyan.add(java02);

  11.                 java01.add(new Student("01","lisi1"));
  12.                 java01.add(new Student("02","lisi1"));
  13.                 java02.add(new Student("001","lisi001"));
  14.                 java02.add(new Student("002","lisi002"));
  15.                 //MapShow(java01);
  16.                 Iterator<List<Student>> it =shiyan.iterator();
  17.                 while (it.hasNext())
  18.                 {
  19.                         List<Student> li =it.next();
  20.                         System.out.println(li);
  21.                         MapShow(li);
  22.                 }


  23.         }
  24.         public static void MapShow(List<Student> room)
  25.         {
  26.                 Iterator<Student> it = room.iterator();
  27.                 while (it.hasNext())
  28.                 {
  29.                         Student id =it.next();
  30.                         System.out.println(id);
  31.                 }
  32.         }
  33. }
  34. class Student
  35. {
  36.         private String name;
  37.         private String age;
  38.         Student(String name,String age)
  39.         {
  40.                 this.name=name;
  41.                 this.age=age;
  42.         }
  43.         public String getName()
  44.         {
  45.                 return name;
  46.         }
  47.         public String getAge()
  48.         {
  49.                 return age;
  50.                
  51.         }
  52.         public String toString()
  53.         {
  54.                 return name+age;
  55.         }
  56. }
复制代码


评分

参与人数 1技术分 +1 收起 理由
zzkang0206 + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马