第三篇 集合与容器(三)

package com.zzp.demo;

import java.util.HashMap;
import java.util.Map;

/**
 * 
 * 测试HashMap
 * @author java
 *
 */
public class TestMap {
    public static void main(String[] args) {
        Map<Integer,String> m1 = new HashMap<Integer,String>();
        m1.put(1, "一");
        m1.put(2, "二");
        m1.put(3, "三");
        System.out.println(m1.get(1));
        System.out.println(m1.isEmpty());
        System.out.println(m1.containsKey(1));
        System.out.println(m1.containsValue("四"));
        System.out.println(m1.size());
        System.out.println(m1.toString());
        
        Map<Integer,String> m2 = new HashMap<>();
        m2.put(4, "三");
        m2.put(5, "五");
        m1.putAll(m2);
        System.out.println(m1);
        m2.clear();
        System.out.println(m2);
    }
}

 第一版

package com.zzp.demo;

import java.util.HashMap;
import java.util.Map;

public class TestMap02 {

    public static void main(String[] args) {        
        Employee e1 = new Employee(1001, "张三", 10000);
        Employee e2 = new Employee(1002, "李四", 12000);
        Employee e3 = new Employee(1003, "王五", 13000);
        
        Map<Integer,Employee> m = new HashMap<>();
        m.put(1001, e1);
        m.put(1002, e2);
        m.put(1003, e3);
        System.out.println(m.get(1001).getEname());
    }

}

class Employee{
    
    private int id;
    private String ename;
    private double salary;
    
    public Employee(int id, String ename, double salary) {
        super();
        this.id = id;
        this.ename = ename;
        this.salary = salary;
    }

    public Employee() {
        super();
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getEname() {
        return ename;
    }

    public void setEname(String ename) {
        this.ename = ename;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }
}

 

posted on 2018-08-22 05:52  奋斗的小刀001  阅读(102)  评论(0编辑  收藏  举报

导航