public static void main(String[] args) {
/*
* Map集合。 1,Map集合中存储的是一对儿元素。键和值。之间存在着对应关系。 2,必须要保证键的唯一性。 3,如果存储键相同,值会覆盖。
*/
/*
* 需求:将学号和对应的学生姓名进行存储。
*/
Map<Integer, String> map = new HashMap<Integer, String>();
// 存储键值对。
System.out.println(map.put(2, "小花"));
System.out.println(map.put(2, "麻子"));
map.put(6, "赵六");
map.put(1, "王五");
map.put(1, "王五1");
System.out.println(map);
System.out.println(map.get(1));
}
null
小花
{1=王五1, 2=麻子, 6=赵六}
王五1
遍历Map
public static void main(String[] args) {
/*
* 获取Map中的所有键值对。
* Map取出所有元素的原理:将map先转成set,在使用迭代器。
*
*/
Map<String,String> map = new HashMap<String,String>();
map.put("zhangsan","北京");
map.put("lisi","上海");
map.put("wangwu","成都");
map.put("zhaoliu","广州");
// System.out.println(map);
//第一种方式:
//获取map集合中的键的集合。keySet();
Set<String> keySet = map.keySet();
//通过set集合获取迭代器。
for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
String key = (String) iterator.next();
String value=map.get(key);
System.out.println(key+"="+value );
}
System.err.println("-----------------------------");
/*//第二种方式:
//获取map集合中所有的键值关系集合。entrySet,键值关系的类型是 Map.Entry.
Set<Map.Entry<String, String>> entrySet = map.entrySet();
//迭代器取出所有的关系对象。
Iterator<Map.Entry<String, String>> it = entrySet.iterator();
while(it.hasNext()){
Map.Entry<String, String> me = it.next();
String key = me.getKey();
String value = me.getValue();
System.out.println(key+"::::"+value);
}*/
Set<Map.Entry<String, String>> entrySet = map.entrySet();
for (Entry<String, String> entry : entrySet) {
System.out.println(entry.getKey()+"="+entry.getValue());
}
//第三种方式:获取值的集合。values();
/*Collection<String> values = map.values();
Iterator<String> it = values.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
*/
}
总结 获取键集合 keySet 获取值集合 values 获取关系集合 entrySet
内部接口
//MyMap.MyEntry
interface MyMap{
public static interface MyEntry{//内部接口
}
}
class Demo5 implements MyMap.MyEntry{
}
Map集合:特点:
1,存储的都是键值对。
2,要保证键的唯一性。
存的方式:put(key,value);
取的方式:keySet(),entrySet(),values();原理:就是将map集合转成set集合或者Collection集合,再通过Iterator取出。
Map
|--Hashtable:哈希表结构,同步的。null不可以作为键和值。
|--HashMap:哈希表结构,不同步的。替代了Hashtable。允许null作为键和值。
|--TreeMap:二叉树结构,不同步的,可以对map集合中的键进行排序。
public class HashMapDemo {
public static void main(String[] args) {
/*
* 将学生对象存储和学生的归属地存储到HashMap集合中。
* 对于学生对象,同姓名同年龄视为同一个人。
*
*/
//1,创建一个HashMap集合对象。
HashMap<Student,String> hm = new HashMap<Student,String>();
//2,将学生对象和归属地存储到集合中。
hm.put(new Student("xiaoming",20),"北京");
hm.put(new Student("abc",20),"成都");
hm.put(new Student("wangcai",20),"广州");
hm.put(new Student("caige",20),"上海");
hm.put(new Student("wangcai",20),"铁岭");
//3,取出map集合中所有的元素。通过keySet方式。
// Set<Student> keySet = hm.keySet();
// Iterator<Student> it = keySet.iterator();
Iterator<Student> it = hm.keySet().iterator();
while(it.hasNext()){
Student key = it.next();
String value = hm.get(key);
System.out.println(key.getName()+":"+key.getAge()+":"+value);
}
}
}
为了保证HashMap保存键值的唯一性 键值对象需覆盖hashCode和equals方法
/**
* 覆盖hashCode方法
* @return
*/
@Override
public int hashCode() {
final int NUMBER = 31;//对31这个数字进行命名定义。增强了阅读性。
return name.hashCode()+age*NUMBER;
}
/**
* 覆盖equals方法
* @return
*/
@Override
public boolean equals(Object obj) {
if(!(obj instanceof Student))
throw new NotTypeException("错误的类型");
Student stu = (Student)obj;
return this.name.equals(stu.name) && this.age == stu.age;
}
public class TreeMapDemo {
public static void main(String[] args) {
/*
* 演示TreeMap集合。
*/
//1,创建TreeMap集合。
TreeMap<Student,String> tm = new TreeMap<Student,String>(new ComparatorByName());
tm.put(new Student("xiaoming",26),"北京");
tm.put(new Student("abc",24),"成都");
tm.put(new Student("wangcai",21),"广州");
tm.put(new Student("caige",27),"上海");
tm.put(new Student("wangcai",21),"铁岭");
/*
* 取出集合中的元素。entrySet()
*/
// Set<Map.Entry<Student,String>> entrySet = tm.entrySet();
// Iterator<Map.Entry<Student,String>> it = entrySet.iterator();
Iterator<Map.Entry<Student,String>> it = tm.entrySet().iterator();
while(it.hasNext()){
Map.Entry<Student, String> me = it.next();
Student key = me.getKey();
String value = me.getValue();
System.out.println(key.getName()+"::"+key.getAge()+"::"+value);
}
}
}
为了保证TreeMap排序 需实现Comparable接口或声明构造器Comparator