Map 接口是实现了类的键值对形式存储数据(key-value)
Map接口有HashMap和TreeMap子类
HashMap 是数据结构的哈希表方式的结构。
TreeMap 是数据结构的二叉树结构.什么是二叉树呢?主要就是一个元素下面只有两个元素。
Map类中存储的是键值对的标识,所以键不能重复。
Map中键值对的比较是通过equals和hashCode方法,重写equals方法必须要重写hashCode方法。
package com.study;
import java.util.*;
public class MapDemo01 {
public static void main(String []args) {
Map hash = new HashMap();
Map tree = new TreeMap();
/**
hash.put("zds1", new Integer(100));
hash.put("zds2", new Double(100.00));
hash.put("zds3", new Integer(200));
hash.put("zds4", 300);
tree.put("1", 100);
tree.put("2", new Boolean(true));
System.out.println(hash.get("zds4"));
System.out.println(hash.size());
System.out.println(hash.containsKey("zds1"));
System.out.println(hash.containsKey("zds11"));
System.out.println(hash.containsValue(new Integer(200)));
System.out.println(hash.containsValue(new Integer(2002)));
System.out.println(hash.put("zds3", new Integer(222)));//把新数据放入value中,在提取老数据的值返回
Map test = new HashMap(tree);
test.putAll(hash);
System.out.println(test);
*/
hash.put("map1", new MapDemo("zhudanshneg","100"));
hash.put("map1", new MapDemo("zhudanshneg","300")); //重复则以前的键的数据将替换掉
hash.put("map3", new MapDemo("zhudanshneg","400"));
System.out.println(hash);
System.out.println(tree.isEmpty());
}
}
class MapDemo {
private String name;
private String width;
public MapDemo(String name ,String width) {
this.name=name;
this.width=width;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getWidth() {
return width;
}
public void setWidth(String width) {
this.width = width;
}
public String toString() {
return this.name +" "+this.width;
}
}