1 public class Emp { 2 private String e_id; 3 private String e_name; 4 5 public Emp(String e_id, String e_name) { 6 this.e_id = e_id; 7 this.e_name = e_name; 8 } 9 10 public String getE_id() { 11 return e_id; 12 } 13 14 public void setE_id(String e_id) { 15 this.e_id = e_id; 16 } 17 18 public String getE_name() { 19 return e_name; 20 } 21 22 public void setE_name(String e_name) { 23 this.e_name = e_name; 24 } 25 26 }
1 import java.util.*; 2 3 public class MapText { 4 5 public static void main(String[] args) { 6 Map<String,String> map = new HashMap<>(); 7 Emp emp = new Emp("001", "张三"); 8 Emp emp1 = new Emp("002", "李四"); 9 Emp emp2 = new Emp("005", "王五"); 10 Emp emp3 = new Emp("006", "马六"); 11 map.put(emp.getE_id(), emp.getE_name()); 12 map.put(emp1.getE_id(), emp1.getE_name()); 13 map.put(emp2.getE_id(), emp2.getE_name()); 14 map.put(emp3.getE_id(), emp3.getE_name()); 15 16 System.out.println("HashMap类实现的map集合,无序:"); 17 for (String str : map.keySet()) { 18 System.out.println(str + ":" + map.get(str)); 19 } 20 System.out.println("HashMap类实现的map集合,有序:"); 21 TreeMap<String,String> tmap = new TreeMap<>(); 22 tmap.putAll(map); 23 for (String str : tmap.keySet()) { 24 System.out.println(str + ":" + tmap.get(str)); 25 } 26 27 map.remove("005"); 28 System.out.println(map); 29 tmap.remove("005"); 30 System.out.println(tmap); 31 } 32 33 }
运行: