Map
Map和Collection存储数据的形式不同
Collection中的集合,元素时孤立存在的(理解为单身),想集合中存储元素采用一个个元素的方式存储。
Map中的集合,元素是成对存在的。每个元素由键和值两部分组成,通过键可以找所对应的值。
Collection中的集合称为单列集合,Map中的集合称为双列集合。
需要注意的是,Map中的集合不能包含重复的键,值可以重复;每个键只能对应一个值。
HashMap<K, V>:存储数据采用的哈希表结构,元素的存取顺序不能保证一致。
由于要保证键的唯一,不重复,需要重写键的hashCode()和equals()方法。
LinkedHashMap<K, V>:HashMap下有个子类LinkedHashMap,存储数据采用的哈希表和链表结构。
通过链表结构可以保证元素的存取顺序一致。
由于要保证键的唯一,不重复,需要重写键的hashCode()和equals()方法。
public class MapDemo{
psvm{
}
/*
移除集合中的键值对,返回被移除之前的值
V remove(K)
*/
public static void func3(){
psvm{
Map<Integer, String> map = new HashMap<Integer, String>();
map.put(1,"a");
map.put(2,"d");
map.put(3,"c");
String value = map.remove(3);
sout(value);
String value = map.remove(4);
sout(value); //返回null,并且没有移除成功
}
}
/*
通过键对象,获取值对象
V get(K)
*/
public static void func2(){
psvm{
Map<Integer, String> map = new HashMap<Integer, String>();
map.put(1,"a");
map.put(2,"d");
map.put(3,"c");
String value = map.get(1);
sout(value);
String value = map.get(4);
sout(value); //结果为null
}
}
/*
将键值对存储到集合中
V put(K, V)
*/
public static void func(){
//创建集合对象,hashmap
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("a",1);
map.put("b",2);
map.put("c",3);
map.put("c",5); //重复的键会把原来的值覆盖掉,不允许出现重复键
Integer i= map.put("d",6);
sout(i); //返回值一般情况下返回null,存储重复键的时候,返回被覆盖之前的值
sout(map);
}
}
Map集合的遍历
利用键获取值
Map接口中定义方法keySet
所有的键,存储到Set集合
public class MapDemo1(){
psvm{
//1、利用Map集合的方法keySet,所有的键存储到Set集合中
//2、遍历Set集合,获取出Set集合中的所有元素(Map中的键)
//3、调用map集合方法get,通过键获取到值
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("a",1);
map.put("b",2);
map.put("c",3);
Set<String> set = map.keySet();
//此Set是HashMap中的内部类KeySet
//1
Iterator<String> it = set.iterator();
while(it.hasNext()){
sout(map.get(it.next()));
}
//2
for(String key : set){
sout(map.get(key));
}
//3
for(String key : map.keySet()){
sout(map.get(key));
}
}
}
Map中的Entry对象
/*
Map集合获取方式
entrySet方法,键值对映射关系(结婚证)获取
实现步骤:
1、调用map集合方法entrySet()将集合中的映射关系对象,存储到Set集合
Set<Entry<K, V>>
2、迭代Set集合
3、获取出的Set结合的元素,是映射关系对象
4、通过映射关系对象方法getKey,getValue获取键值对
*/
public class MapDemo2(){
psvm{
Map<Integer, String> map = new HashMap<Integer, String>();
map.put(1, "a");
map.put(2, "b");
map.put(3, "c");
Set<Map.Entry<Integer, String>> set = map.entrySet();
Iterator<Map.Entry<Integer, String>> it = set.iterator();
while(it.hasNext()){
Map.Entry<Integer, String> entry = it.next();
sout(entry.getKey()+"...."+entry.getValue());
}
for(Map.Entry<Integer, String> entry : map.entrySet()){
sout(entry.getKey()+"...."+entry.getValue());
}
}
注意:Iterable 接口下没有map集合,所以map不能用增强for循环
HashMap
/*
利用HashMap集合,存储自定义的对象
自定义对象,作为键出现,作为值出现
*/
public class HashMapDemo{
psvm{
}
/*
HashMap存储自定义的对象Person,作为值出现
*/
public static void func(){
HashMap<String, Person> map = new HashMap<String, Person>();
map.put("a",new Person("a",11));
map.put("b",new Person("b",12));
map.put("c",new Person("c",13));
for(String key : map.keySet()){
Person p = map.get(key);
sout(key+"...."+p);
}
}
}
public class Person{
private int age;
private String name;
public Person(){
}
public Person(String name, int age){
this.name = name;
this.age = age;
}
getter
setter
@Override
public String toString(){
return "Person"+"..."+name+"..."+age;
}
//当Person作为map的键时,为了保证键的唯一性,必须重写hashCode()和equals()方法。
}
LinkedHashMap
/*
LinkedHashMap继承HashMap
保证迭代的顺序
*/
public class LinkedHashMapDemo{
psvm{
LinkedHashMap<String, String> link = LinkedHashMap<String, String>();
link.put("a","1");
link.put("b","2");
link.put("c","3");
link.put("d","4");
sout(link); //怎么存进去,就会怎么取出来
}
}
HashTable
/*
Map接口实现类 HashTable
底层数据结构哈希表,特点和hashMap时一样的
HashTable线程安全,运行速度慢
HashMap线程不安全,运行速度快
HashTable命运和Vector一样
HashMap 允许存储null值,null键
HashTable 不允许存储null值,null键
HashTable的子类,Properties,依然活跃在开发舞台
*/