iterator的使用

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());
        }

}

}

时间: 2024-12-24 21:24:13

iterator的使用的相关文章

Iterator接口。集合输出

在集合中支持以下几种方式. iterator ListIterator foreach输出 emumeration输出. 集合输出的标准操作: 集合输出的时候必须形成以下的思路:只要碰到了集合输出的操作,就一定使用iterator接口,这是最重要的标准. iterator接口的操作原理: iterator是专门的迭代输出接口,所谓的迭代输出就是将元素一个个进行判断,判断其是否有内容,如果有内容,则把内容输出. 对于iterator而言,其本身是一个借口,所以要想实例化,需要必须依靠collect

Java源码分析:深入探讨Iterator模式

作者:兄弟连 java.util包中包含了一系列重要的集合类.本文将从分析源码入手,深入研究一个集合类的内部结构,以及遍历集合的迭代模式的源码实现内幕. 下面我们先简单讨论一个根接口Collection,然后分析一个抽象类AbstractList和它的对应Iterator接口,并仔细研究迭代子模式的实现原理. 本文讨论的源代码版本是JDK 1.4.2,因为JDK 1.5在java.util中使用了很多泛型代码,为了简化问题,所以我们还是讨论1.4版本的代码. 集合类的根接口Collection

LeetCode OJ:Peeking Iterator(peeking 迭代器)

Given an Iterator class interface with methods: next() and hasNext(), design and implement a PeekingIterator that support the peek() operation -- it essentially peek() at the element that will be returned by the next call to next(). Here is an exampl

LeetCode——Peeking Iterator

Description: Given an Iterator class interface with methods: next() and hasNext(), design and implement a PeekingIterator that support the peek() operation -- it essentially peek() at the element that will be returned by the next call to next(). Here

Java Iterator和增强for循环 for each详解

Iterator是Java中的一个迭代器接口(Interface),用来提供标准的Java迭代器 Iterator支持泛型因为集合(Collection)类可以装入的类型是不确定的,从集合中取出的都是Object类型,加入泛型,就是告诉编译器确定要装入的对象类型,取值时就无需强制转换了. for each 是 Java 5 中新增的一个循环结构,本质上是一个Iterator,特点是可以用来遍历集合元素而不用考虑集合下标. 综合实例: package net.csdn.shf4715; impor

一大波Java来袭(六)——Java集合之Collection和Iterator接口

本文主要介绍Collection和Iterator接口. 一.Collection和Iterator接口 Collection接口是List.Set..Queue的父接口. Collection  c = new ArrayList(); 父类指向子类对象!优点:多态.动态链接.向上转型. 面向接口编程,被调用者对于调用者是完全透明的,可以随意替换子类,屏蔽了子类特有的东西. Demo:详细请参加:Java API文档 二.如何依次遍历集合中的元素? 普通情况下,当我们把一个对象"丢进"

Java容器(List、Map、Set、Iterator)

容器是一个Java 所编写的程序,原先必须自行编写程序以管理对象关系,现在容器都会自动帮您做好. List特点:元素有放入顺序,元素可重复 Set特点:元素无放入顺序,元素不可重复(注意:元素虽然无放入顺序,但是元素在set中的位置是有该元素的HashCode决定的,其位置其实是固定的) Map特点:元素按键值对存储,无放入顺序 . 一.List接口 ArrayList:线程不安全,效率高. 底层实现是数组,查询块,修改删除慢. LinkedList: 线程不安全,效率高. 底层实现是链表,查询

Binary Search Tree Iterator

QUESTION Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST. Calling next() will return the next smallest number in the BST. Note: next() and hasNext() should run in average O(1) time

LevelDB源码分析--Iterator

我们先来参考来至使用Iterator简化代码2-TwoLevelIterator的例子,略微修改希望能帮助更加容易立即,如果有不理解请各位看客阅读原文. 下面我们再来看一个例子,我们为一个书店写程序,书店里有许多书Book,每个书架(BookShelf)上有多本书. 类结构如下所示 class Book { private: string book_name_; }; class Shelf { private: vector<Book> books_; }; 如何遍历书架上所有的书呢?一种实

畅销书对Java中Iterator的理解误区

声明:本博客为原创博客,未经允许,不得转载!原文链接为http://blog.csdn.net/bettarwang/article/details/28110615 最近放假,闲来无事,便翻看以前看过的一些书,竟然发现有些书本(甚至是一些畅销书)对Java中Iterator有很大的误解,比如某畅销书在Collection那一章有这么一句话:"当使用Iterator对集合元素进行迭代时,Iterator并不是把集合元素本身传给了迭代变量,而是把集合元素的值传给了迭代变量,所以修改迭代变量的值对集