zoukankan      html  css  js  c++  java
  • 集合HashMap和HashSet中迭代器的使用

    package setTest;

    import java.util.HashMap;
    import java.util.HashSet;
    import java.util.Iterator;
    import java.util.Set;

    public class TestMap {

    public static void main(String[] args) {
    HashMap hm = new HashMap();

    hm.put("aa", "test1");
    hm.put("bb", "test2");
    hm.put("cc", "test3");
    hm.put("dd", "test4");
    hm.put("ee", "test5");

    //iterator
    Iterator it = hm.keySet().iterator();

    while(it.hasNext()) {
    //key
    String str = (String)it.next();
    System.out.print(str+" ");
    //value
    String value = (String)hm.get(str);
    System.out.println(value);
    }

    for (Object obj : hm.keySet()) {
    String key = (String)obj;
    System.out.print(key+" ");
    String value = (String)hm.get(key);
    System.out.println(value);
    }


    Set set = new HashSet();
    set.add("aa");
    set.add("bb");
    set.add("cc");
    set.add("dd");

    for(Iterator it1 = set.iterator();it1.hasNext();) {
    String str = (String)it1.next();
    System.out.println(str);
    }

    for(Object obj:set) {
    System.out.println(obj);
    }

    }

    }

  • 相关阅读:
    poj2752Seek the Name, Seek the Fame【kmp next数组应用】
    poj1961Period【kmp next数组】
    poj2406(kmp next数组)
    KMP原理
    0529
    0428
    2045年4月25日
    0421
    黄金连分数【大数】
    学习linux内核时常碰到的汇编指令(1)
  • 原文地址:https://www.cnblogs.com/dongrilaoxiao/p/6673343.html
Copyright © 2011-2022 走看看