• 56 容器(十)——Iterator迭代器遍历容器


    迭代器的获取

    LIst与Set容器统一使用他们的对象.Iterator()方法获得迭代器对象,然后使用while循环配合迭代器的方法hasNext()及next()来遍历容器。

    List<String> myList = new ArrayList<>();
    Iterator<String> it = myList.iterator();

     while(it.hasNext()) {
     System.out.println(it.next());
     }

      

    对于Map容器,Map容器因为底层数据结构比较特殊

    第一种方法:我们需要使用Map对象调用entrySet().iterator()方法来获取迭代器。

    Map<Integer,String> map = new HashMap<>();	
    System.out.println("-----HashMap方法一:获得内部数组对象-------");
    for(Iterator<Entry<Integer,String>> it = map.entrySet().iterator();it.hasNext();) {
    	System.out.println(it.next());
    }
    

      

    第二种方法:获取HashMap的keySet,通过keySet()方法,然后通过得到集合获取遍历器,然后使用此遍历器遍历出key,通过Map的get方法得到value。

    System.out.println("-----HashMap方法二:获得keySet-------");
    for(Iterator<Integer> it = map.keySet().iterator();it.hasNext();) {
    	int key;
    	System.out.println("key="+(key = it.next())+" value="+map.get(key));
    }
    
    

      

    各容器遍历测试

    package _20191213;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.HashSet;
    import java.util.Iterator;
    import java.util.LinkedList;
    import java.util.List;
    import java.util.Map;
    import java.util.Map.Entry;
    import java.util.Set;
    
    /**
     * 迭代器的使用
     * @author TEDU
     *
     */
    public class IteratorTest {
    	public static void main(String[] args) {
    		//调用遍历ArrayList方法
    		testArrayList();
    		//调用LinkedList
    		testLinkedList();
    		//调用testHashMap()
    		testHashMap();
    		//调用HashSet
    		testHashSet();
    	}
    	
    	/**
    	 * iterator遍历ArrayList 
    	 */
    	public static void testArrayList() {
    		List<String> myList = new ArrayList<>();
    		myList.add("谁");
    		myList.add("在");
    		myList.add("敲");
    		myList.add("打");
    		myList.add("我");
    		myList.add("窗");
    		System.out.println("-----ArrayList-------");
    		Iterator<String> it = myList.iterator();
    		while(it.hasNext()) {
    			System.out.println(it.next());
    		}
    	}
    	
    	/**
    	 * LinkedList的遍历
    	 */
    	public static void testLinkedList() {
    		LinkedList<String> ml = new LinkedList<>();
    		ml.add("suck");
    		ml.add("my");
    		ml.add("ass");
    		System.out.println("-----LinkedList-------");
    		Iterator<String> it = ml.iterator();
    		while(it.hasNext()) {
    			System.out.println(it.next());
    		}
    	}
    	/**
    	 * HashMap
    	 */
    	public static void testHashMap() {
    		Map<Integer,String> map = new HashMap<>();
    		map.put(1001,"张三");
    		map.put(1002,"李四");
    		map.put(1003,"王五");
    		System.out.println("-----HashMap-------");
    		for(Iterator<Entry<Integer,String>> it = map.entrySet().iterator();it.hasNext();) {
    			System.out.println(it.next());
    		}
    	}
    	/**
    	 * HashSet
    	 */
    	public static void testHashSet() {
    		Set<String> set = new HashSet<>(); 
    		set.add("you");
    		set.add("geek");
    		System.out.println("-----HashSet-------");
    		for(Iterator<String> it = set.iterator();it.hasNext();) {
    			System.out.println(it.next());
    		}
    	}
    }
    

      

  • 相关阅读:
    postman简单使用
    Jenkins新建任务
    Property 'mapperLocations' was not specified or no matching resources found org.apache.ibatis.builder.BuilderException: Could not find value method on SQL annotation
    spring boot 处理异常
    Spring MVC @ExceptionHandler Example【转】
    微信小程序bindtap与catchtap的区别【转】
    Android imageview点了二,三次才有效果处理
    android 发email
    startActivity报错exposed beyond app through Intent.getData()
    ENUM 值.ordinal() 获取index
  • 原文地址:https://www.cnblogs.com/Scorpicat/p/12033709.html
Copyright © 2020-2023  润新知