Java 容器(四) Map
来源:互联网 发布:应力分析软件 编辑:程序博客网 时间:2024/05/16 06:51
纸上得来终觉浅
1.Map的使用估计是最常见的了:
1)HashMap:存储的值可以重复,键不能;不能使用Comparator和Comparable接口排序;HashMap最多只允许一条记录的键为null,不允许多条记录的值为null
2)TreeMap:存储的值可以重复,键不能;自动排序或手动排序;
3)LinkedHashMap:存储的值可以重复,键不能;保持输入的顺序
其中它们的用法和Set对比,几乎是一一对应:
package roadArchitectWeb.Test;import java.util.Comparator;import java.util.HashMap;import java.util.LinkedHashMap;import java.util.Map;import java.util.TreeMap;class Student{private Integer id;private String name;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Student(Integer id, String name) {super();this.id = id;this.name = name;}@Overridepublic String toString() {return "Student [id=" + id + ", name=" + name + "]";}@Overridepublic int hashCode() {return this.id;}@Overridepublic boolean equals(Object obj) {if(obj instanceof Student){Student stu = (Student)obj;if(stu.name == this.name && stu.id == this.id)return true;}return false;}}public class Test5 {protected static Map<String,Student> hashMap = new HashMap<String,Student>();/*Comparable的排序会失效,因为map只能按键排列*/protected static Map<String,Student> treeMapComparable = new TreeMap<String,Student>();protected static Map<String,Student> treeMapComparator = new TreeMap<String,Student>(new Comparator<String>() {@Overridepublic int compare(String o1, String o2) {return o2.compareTo(o1);}});protected static Map<String,Student> linkedHashMap = new LinkedHashMap<String,Student>();public static void main(String[] args) {hashMap.put("1",new Student(3,"sun"));hashMap.put("2",new Student(1,"zhao"));hashMap.put("3",new Student(2,"qian"));System.out.println("hashMap:"+hashMap);/* *这里使用Comparable */treeMapComparable.put("3",new Student(3,"sun"));treeMapComparable.put("2",new Student(1,"zhao"));treeMapComparable.put("1",new Student(2,"qian"));System.out.println("treeMapComparable:"+treeMapComparable);/*这里使用Comparator */treeMapComparator.put("3",new Student(3,"sun"));treeMapComparator.put("1",new Student(1,"zhao"));treeMapComparator.put("2",new Student(2,"qian"));System.out.println("treeMapComparator:"+treeMapComparator);linkedHashMap.put("3",new Student(3,"sun"));linkedHashMap.put("1",new Student(1,"zhao"));linkedHashMap.put("2",new Student(2,"qian"));System.out.println("linkedHashMap:"+linkedHashMap);}}结果如下:
hashMap:{1=Student [id=3, name=sun], 2=Student [id=1, name=zhao], 3=Student [id=2, name=qian]}treeMapComparable:{1=Student [id=2, name=qian], 2=Student [id=1, name=zhao], 3=Student [id=3, name=sun]}treeMapComparator:{3=Student [id=3, name=sun], 2=Student [id=2, name=qian], 1=Student [id=1, name=zhao]}linkedHashMap:{3=Student [id=3, name=sun], 1=Student [id=1, name=zhao], 2=Student [id=2, name=qian]}
2.注意
1)同Set,hashMap和LinkedHashMap不支持排序;
2)hashTable:基本上与hashMap相同,它不允许记录的键或者值为空;线程同步;
3)HashSet是通过HashMap实现的,TreeSet是通过TreeMap实现的,只不过Set用的只是Map的key;
4)另外注意到,treeMap当key是基础类,如String时候,似乎只能使用Comparator接口,基础类的Comparable接口是内置的(TreeMap排序使用Comparable没有实现过)
3.Map中迭代器使用:
/*遍历key*/Iterator it1 = hashMap.keySet().iterator();while(it1.hasNext()){System.out.println("key:"+it1.next());}/*遍历entry,从而可以获得key或者value的值*/Iterator it2 = hashMap.entrySet().iterator();while(it2.hasNext()){Map.Entry<String, Student> entry = null;entry = (Entry<String, Student>) it2.next();System.out.println("value:"+entry.getValue());}结果如下:
key:1key:2key:3value:Student [id=3, name=sun]value:Student [id=1, name=zhao]value:Student [id=2, name=qian]
0 0
- Java 容器(四) Map
- 系统研究Java--容器(四)--Map和Set
- Java容器分析--Map
- Java容器分析--Map
- Java容器分析--Map
- Java容器分析--Map
- Java容器之Map
- java容器---Map扩展
- java容器之Map
- java容器之Map
- Java容器(Map)
- Java容器四:Hashtable
- C++容器(四):map类型
- JAVA,List,Map,Set,容器
- JAVA,List,Map,Set,容器
- JAVA,List,Map,Set,容器
- JAVA,List,Map,Set,容器
- Java容器map的用法
- NTFS文件系统主要特点是什么?
- 数据分析能力的8个等级
- iOS 图片捏合放大缩小 点击放大缩小
- Inno Setup入门(十二)——Pascal脚本(1)
- RHEL 6.7 使用multipathd reload命 令有导致DB实例Crash风险,特发Alert提醒,请关注!
- Java 容器(四) Map
- 设计模式--策略模式
- Android自定义View解析之视图状态及视图重绘(二)
- 定时器实现定时功能
- 海思3518e mpp2/sample/venc makefile简析
- opencv函数操作对象的一致性问题
- Android Volley完全解析(一),初识Volley的基本用法
- mybatis 之 generator插件使用 集成到了eclipse 自动生成实体,example,mapper
- iOS获取当前年、月、日等信息