加载中...

HashMap的使用

import java.util.HashMap;
import java.util.Map;
/**
测试HashMap的使用
*/
public class TestMap {
public static void main(String[] args) {
//Test01();

Employee e1 = new Employee(1001, "熊大", 10000);
Employee e2 = new Employee(1002, "熊二", 20000);

Map<Integer,Employee> map = new HashMap<>();

map.put(1001, e1); //使用map存储
map.put(1002, e2);

Employee emp = map.get(1001);
System.out.println(emp.getEname());

}

public static void Test01() {
Map<Integer,String> m1 = new HashMap<>();

m1.put(1, "one");
m1.put(2, "two");
m1.put(3, "three");

System.out.println(m1.get(1));

System.out.println(m1.size()); //3
System.out.println(m1.isEmpty()); //false
System.out.println(m1.containsKey(2)); //true,是否包含2这个键
System.out.println(m1.containsValue("four"));//false,是否包含four

Map<Integer,String> m2 = new HashMap();
m2.put(4,"si");
m2.put(5,"wu");

m1.putAll(m2);

System.out.println(m1); //{1=one, 2=two, 3=three, 4=si, 5=wu}

}
}

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 String getEname() {
return this.ename;
}

}

posted @ 2021-08-14 11:05  nongeason  阅读(49)  评论(0)    收藏  举报