• HashMap遍历


    package cn.yu.hashmap;

    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Map;
    import java.util.Map.Entry;

    public class hashmap {
        
    public static void main(String[] args) {
            HashMap
    <String, String> map = new HashMap<String, String>();
            map.put(
    "1""aaa");
            map.put(
    "2""bbb");
            map.put(
    "3""ccc");
            map.put(
    "4""ddd");
            map.put(
    "5""eee");
            map.put(
    "6""fff");
            map.put(
    "7""ggg");
            map.put(
    "8""hhh");
            map.put(
    "9""iii");

            
    /*
             * 遍历hashmap
             
    */
            hashmap1(map);
            line();
            
    /*
             * 用keySet遍历
             
    */
            hashmap2(map);
            line();
            
    /*
             * 用entrySet遍历 速度快
             
    */
            hashmap3(map);
            line();
        }

        
    public static void hashmap1(HashMap<String, String> map) {
            
    for (Map.Entry<String, String> entry : map.entrySet()) {
                System.out.println(
    "Key:" + entry.getKey() + "   value:" + entry.getValue().toString());
            }

        }

        
    public static void hashmap2(HashMap<String, String> map) {
            Iterator
    <String> it=map.keySet().iterator();//这是取得键对象   
            while(it.hasNext())   {   
               System.out.println(
    "value: "+map.get(it.next()));   //获得键所对应的值。   
            } 
        }

        
    public static void hashmap3(HashMap<String, String> map) {
            Iterator
    <Entry<String, String>> it = map.entrySet().iterator();  
            
    while(it.hasNext()){  
                Entry
    <String, String>  entry=(Entry<String, String>)it.next();  
                System.out.println(
    "key:"+entry.getKey()+"   value:"+entry.getValue());
                
            }  
        }
        
    public static void line(){
            System.out.println(
    "========================华丽的分割线===============================");
        }
    }



      Hashmap实际上是一个数组和链表的结合体,利用数组来模拟一个个桶(类似于Bucket Sort)以快速存取不同hashCode的key,对于相同hashCode的不同key,再调用其equals方法从List中提取出和key所相对应的value

  • 相关阅读:
    WPF Timer替代者
    <转>Change the Background of a selected ListBox Item
    WPF Button样式模板
    WPF中自定义只能输入数字的TextBox
    ansible playbook模式及语法
    数据挖掘Kaggle
    电影网站
    数据挖掘面临的科学和工程的新问题
    KDD Cup 2012(今年数据挖掘在中国)
    能力是在执行中实现的,要高节奏不要详细的设计
  • 原文地址:https://www.cnblogs.com/harrygogo/p/3526642.html
Copyright © 2020-2023  润新知