码迷,mamicode.com
首页 > 其他好文 > 详细

iterator的使用

时间:2016-12-04 23:10:39      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:entryset   迭代器   todo   iterator   ext   ash   int   system   str   

package ListPackage;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class Demo4 {
//**************使用迭代器遍历map和list**************
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Map<String,Integer> map = new HashMap<>();//图
        List<Integer> list = new LinkedList<Integer>();//链表
        
        map.put("Mike", 15);
        map.put("John", 4);
        map.put("Amy", 45);
        map.put("Michael", 8);
        map.put("Marry", 67);
        map.put("Zack", 39);
        
        list.add(34);
        list.add(56);
        list.add(54);
        list.add(89);
        list.add(308);
        
//        使用迭代器遍历map
        Iterator<Map.Entry<String,Integer>> it = map.entrySet().iterator();
        while(it.hasNext()){
            Map.Entry<String, Integer> entries = it.next();
            System.out.println(entries.getKey()+" "+entries.getValue());
        }
        System.out.println();
        
//        使用迭代器遍历list
        Iterator<Integer> it1 = list.iterator();
        while(it1.hasNext()){
            System.out.println(it1.next());
        }

    }

}

iterator的使用

标签:entryset   迭代器   todo   iterator   ext   ash   int   system   str   

原文地址:http://www.cnblogs.com/ihins/p/6132203.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!