牛骨文教育服务平台(让学习变的简单)
博文笔记

Hashmap的简单使用

创建时间:2016-01-19 投稿人: 浏览次数:4380

关于hashmap的一些使用
例一 用iterator迭代器查看内容

//功能: 尝试hashmap的使用
package com_1;

import java.util.HashMap;
import java.util.Iterator;

public class hashmap_test {

    /**
     * @param args
     */
    public static void main(String[] args)
    {    
     HashMap hs=new HashMap();       
     hs.put("name", "张三");       
     hs.put("sex", "男");       
     hs.put("age", "30");       
     hs.put("home", "河北");    

     //测试是否包含关键字"name" 
    System.out.println(hs.containsKey("name"));//返回true 
    System.out.println(hs.get("name"));//返回  张三 
    System.out.println(hs.entrySet());//返回  [home=河北, sex=男, age=30, name=张三]  
    System.out.println(hs.hashCode()); //返回7960688 
    System.out.println(hs.keySet()); //返回 [home, sex, age, name] 

    //1 测试entrySet().的用法
    Iterator it=hs.entrySet().iterator();//迭代程序 
    System.out.println("
测试entrySet().的用法");
    while(it.hasNext())  {      
     System.out.print(it.next() +"	");  
    }                  

    //2 Set keySet()返回关键字各值的集合    
     it=hs.keySet().iterator();     
     System.out.println("

Set keySet()返回关键字各值的集合");
     while(it.hasNext())     
    {      
     System.out.print(hs.get(it.next())+"	");    
    }                

    //3 测试  values()的用法
    it=hs.values().iterator();     
    System.out.println("

测试  values()的用法");
     while(it.hasNext())     {      
    System.out.print(it.next()+"	");     
    }   

    }
}

这里写图片描述

例二 用tostring查看内容

//功能: 尝试hashmap的使用2

package com_1;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class hashmap_test {
    public static void main(String[] args) {
        Map<String, Employee> staff = new HashMap<String, Employee>();
        staff.put("144-25-5464", new Employee("Amy",15000));
        staff.put("567-24-2546", new Employee("Harry",23000));
        staff.put("157-62-7935", new Employee("Gray",36000));
        staff.put("456-62-5527", new Employee("France",67000));
        System.out.println(staff);

        staff.remove("567-24-2546");//删除
        System.out.println(staff);

        staff.put("456-62-5527", new Employee("Bob",76000));//替换
        System.out.println(staff);

        System.out.println(staff.get("157-62-7935"));//查询

        //取得map中所有的key和value
        for(Map.Entry<String, Employee> entry : staff.entrySet()) {
            String key = entry.getKey();
            Employee value = entry.getValue();
            System.out.println("key=" + key + ", value=" + value + "");
        }

        //取得map中所有的key
        Set<String> keys = staff.keySet();
        for(String key : keys) {
            System.out.println(key);
        }

        //取得map中所有的value
        Collection<Employee> values = staff.values();
        for(Employee value : values) {
            System.out.println(value);
        }
    }
}

class Employee {
    public Employee(String name,double salary) {
        this.name = name;
        this.salary =salary;
    }

    //这句话很重要,println staff 的时候需要
    public String toString() {
        return "[名字=" + name + ", 	薪水=" + salary + "]";
    }

    private String name;
    private double salary;
}
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。