栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java 核心技术(第八版)卷1:基础知识:例13-5P582MapTest

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java 核心技术(第八版)卷1:基础知识:例13-5P582MapTest

import java.util.*;
//本程序演示 map 的使用,它的key 是 String,值是Employee类型
public class MapTset {
    public static void main(String[]args)
    {
        Map staff=new HashMap();
        staff.put("144-25-5464",new Employee("Amy Lee"));
        staff.put("567-24-2546",new Employee("Harry Hacker"));
        staff.put("157-62-7935",new Employee("Gary Cooper"));
        staff.put("456-62-5527",new Employee("Francersca Cruz"));

        // print all entries
        System.out.println(staff);
        //remove an entry
        staff.remove("567-24-2546");
        System.out.println("After remove Key:567-24-2546 :");
        System.out.println(staff);
        //replace an entry
        staff.put("456-62-5527",new Employee("Francesca Miller"));
        System.out.println("After replace Key:456-62-5527 :");
        System.out.println(staff);
        //iterate through all entries
        System.out.println("Let have a iteration:");
        for(Map.Entry entry:staff.entrySet())
        {
            String key=entry.getKey();
            Employee value=entry.getValue();
            System.out.println("key="+key+",value="+value);
        }




    }
}
//mini employee class for test purpose
class Employee
{
    public Employee(String n)
    {
        name=n;
        salary=0;
    }
    public String toString()
    {
        return "[name="+name+",salary="+salary+"]";
    }

    private String name;
    private double salary;
}

运行结果:

{157-62-7935=[name=Gary Cooper,salary=0.0], 144-25-5464=[name=Amy Lee,salary=0.0], 456-62-5527=[name=Francersca Cruz,salary=0.0], 567-24-2546=[name=Harry Hacker,salary=0.0]}
After remove Key:567-24-2546 :
{157-62-7935=[name=Gary Cooper,salary=0.0], 144-25-5464=[name=Amy Lee,salary=0.0], 456-62-5527=[name=Francersca Cruz,salary=0.0]}
After replace Key:456-62-5527 :
{157-62-7935=[name=Gary Cooper,salary=0.0], 144-25-5464=[name=Amy Lee,salary=0.0], 456-62-5527=[name=Francesca Miller,salary=0.0]}
Let have a iteration:
key=157-62-7935,value=[name=Gary Cooper,salary=0.0]
key=144-25-5464,value=[name=Amy Lee,salary=0.0]
key=456-62-5527,value=[name=Francesca Miller,salary=0.0]

进程已结束,退出代码0

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/885335.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号