Hashmap的简单使用

来源:互联网 发布:雷蛇云驱动mac版怎么用 编辑:程序博客网 时间:2024/04/30 23:30

关于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("\n测试entrySet().的用法");    while(it.hasNext())  {           System.out.print(it.next() +"\t");      }                      //2 Set keySet()返回关键字各值的集合         it=hs.keySet().iterator();          System.out.println("\n\nSet keySet()返回关键字各值的集合");     while(it.hasNext())         {           System.out.print(hs.get(it.next())+"\t");        }                    //3 测试  values()的用法    it=hs.values().iterator();         System.out.println("\n\n测试  values()的用法");     while(it.hasNext())     {          System.out.print(it.next()+"\t");         }       }}

这里写图片描述

例二 用tostring查看内容

//功能: 尝试hashmap的使用2package 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 + ", \t薪水=" + salary + "]";    }    private String name;    private double salary;}
0 0
原创粉丝点击