Map集合
一、Map集合
1、Map集合介绍
1.1、Map集合的特点
Map集合:将键映射到值的对象。一个映射不能包含重复的键,每个键最多只能映射到一个值。
Map集合就是用来存放具有对应关系的数据的。即就是key对应value这样的关系,并且要求key不能重复。
public class MapDemo {
public static void main(String[] args) {
//创建Map对象
Map<String, String> map = new HashMap<String,String>();
//给map中添加元素
map.put("星期一", "Monday");
map.put("星期日", "Sunday");
//当给Map中添加元素,会返回key对应的原来的value值,若key没有对象的值,返回null
System.out.println(map.put("星期一", "Mon"));
//根据指定的key获取对应的value
String en = map.get("星期日");
System.out.println(en);
//根据key删除元素,会返回key对应的value值
String value = map.remove("星期日");
System.out.println(value);
}
}
1.2、Map集合的基本方法
(1)添加 : put();
(2)获取: get();
(3)移除:remove();
public class MapDemo01 {
public static void main(String[] args) {
// 创建一个Map集合,键是Integer,值是String
Map<Integer,String> map = new HashMap<>();
// 添加 键值对
map.put(1,"西安");
map.put(2,"上海");
map.put(3,"海岛");
map.put(4,"北京");
System.out.println(map);
// 键保证Map唯一性
map.put(1,"绥德");
System.out.println(map);
// 值没有唯一性
map.put(5,"北京");
System.out.println(map);
// 获取: 根据键去取值
String str = map.get(2);
System.out.println(str);
// 移除: 根据键去移除元素 返回值类型是String类型
String remove1 = map.remove(3);
System.out.println(remove1);
// 移除: 根据键值对去移除元素,键值都对的上就删除并返回true,否则返回false
boolean remove2 = map.remove(4,"北京");
System.out.println(remove2);
}
}
1.3、keySet 方法演示
keySet()
:获取到Map中的所有key。
public class MapDemo {
public static void main(String[] args) {
//创建Map对象
Map<String, String> map = new HashMap<String,String>();
//给map中添加元素
map.put("星期一", "Monday");
map.put("星期日", "Sunday");
//获取Map中的所有key
Set<String> keySet = map.keySet();
//遍历存放所有key的Set集合
Iterator<String> it =keySet.iterator();
while(it.hasNext()){
//得到每一个key
String key = it.next();
//通过key获取对应的value
String value = map.get(key);
System.out.println(key+"="+value);
}
}
}
1.4、entrySet 方法演示
entrySet
:描述是得到所有key和value的映射关系。
public class MapDemo {
public static void main(String[] args) {
//创建Map对象
Map<String, String> map = new HashMap<String,String>();
//给map中添加元素
map.put("星期一", "Monday");
map.put("星期日", "Sunday");
//获取Map中的所有key与value的对应关系
Set<Entry<String,String>> entrySet = map.entrySet();
//遍历Set集合
Iterator<Entry<String,String>> it =entrySet.iterator();
while(it.hasNext()){
//得到每一对对应关系
Entry<String,String> entry = it.next();
//通过每一对对应关系获取对应的key
String key = entry.getKey();
//通过每一对对应关系获取对应的value
String value = entry.getValue();
System.out.println(key+"="+value);
}
}
}
注意:Map集合不能直接使用迭代器或者foreach进行遍历。但是转成Set之后就可以使用了。
2、Map集合常用子类介绍
2.1、Map常见子类的特点
HashTable:数据结构-哈希表。是同步的,不允许null作为键和值。被hashMap替代。
Properties:属性集,键和值都是字符串,而且可以结合流进行键值的操作。
HashMap:数据结构-哈希表。不是同步的,允许null作为键和值。HashMap下有个子类LinkedHashMap基于链表+哈希表。可以保证map集合有序(存入和取出的顺序一致)。
TreeMap:数据结构-二叉树。不是同步的。可以对map集合中的键进行排序。
2.2、HashMap存储自定义键值
public class HashMapTest {
public static void main(String[] args) {
//1,创建hashmap集合对象。
Map<Student,String> map = new HashMap<Student,String>();
//2,添加元素。
map.put(new Student("lisi",28), "上海");
map.put(new Student("wangwu",22), "北京");
map.put(new Student("zhaoliu",24), "成都");
map.put(new Student("zhouqi",25), "广州");
map.put(new Student("wangwu",22), "南京");
//3,取出元素。keySet entrySet
//Set<Student> keySet = map.keySet();
//for(Student key : keySet){}
for(Student key : map.keySet()){
String value = map.get(key);
System.out.println(key.toString()+"....."+value);
}
}
}
注意:当给HashMap中存放自定义对象时,如果自定义对象作为key存在,这时要保证对象唯一,必须复写对象的hashCode和equals方法(如果忘记,请回顾HashSet存放自定义对象)。
如果要保证map中存放的key和取出的顺序一致,可以使用LinkedHashMap集合来存放。
2.3、TreeMap存储自定义键值
public class TreeMapTest {
public static void main(String[] args) {
// 1,创建TreeMap集合对象。
Map<Student, String> map = new TreeMap<Student, String>();
// 2,添加元素。
map.put(new Student("lisi", 28), "上海");
map.put(new Student("wangwu", 22), "北京");
map.put(new Student("zhaoliu", 24), "成都");
map.put(new Student("zhouqi", 25), "广州");
map.put(new Student("wangwu", 22), "南京");
//3,取出所有元素,entrySet()
for(Map.Entry<Student, String> me : map.entrySet()){
Student key = me.getKey();
String value = me.getValue();
System.out.println(key+"::"+value);
}
}
}
注意:给TreeMap存放自定义对象,自定义对象作为key进行排序时,自定义对象必须具备比较功能,即实现Comparable接口。如果需要特定方式进行比较,我们也可以给TreeMap集合传递自定义的比较器进行比较。
2.4、Map练习-字母出现次数
练习:
“werertrtyuifgkiryuiop”,获取字符串中每一个字母出现的次数。要求返回结果个格式是 a(1)b(2)d(4)…;
public class MapTest {
public static void main(String[] args) {
String str = "awaa+acr=ebarct,btydui[efgkiryuiop";
str = getCharCount(str);
System.out.println(str);
}
/*
* 获取字符串中的字母出现次数。
*/
public static String getCharCount(String str) {
//1,将字符串转成字符数组。
char[] chs = str.toCharArray();
//2,定义表。treemap.
TreeMap<Character, Integer> map = new TreeMap<Character, Integer>();
//3,遍历字符数组。
for (int i = 0; i < chs.length; i++) {
//判断必须是字母。
if(!(chs[i]>='a' && chs[i]<='z' || chs[i]>='A' && chs[i]<='Z')){
continue;
}
//4,将遍历到的字母作为键去查map这个表。获取对应的次数。
Integer value = map.get(chs[i]);
//5,有可能要查询的字母在表中不存在对应的次数,需要判断。
//如果返回是null,说明字母没有对应的次数。就将这个字母和1存储到表中。
if(value == null){
//将字母和1存储。
map.put(chs[i],1);
}else{
//否则,说明有对应的次数对次数自增。将字母和新的次数存储到表中。
value++;
map.put(chs[i],value);
}
}
return mapToString(map);
}
/*
* 将map集合中的键值转成 格式是 a(1)b(2)d(4)......
* map中有很多数据,无论是多少个,什么类型,最终都变成字符串。
* StringBuffer 这个容器就符合这个需求。如果是单线程,建议使用StringBuilder。
*
*/
private static String mapToString(Map<Character, Integer> map) {
//1,明确容器。
StringBuilder sb = new StringBuilder();
//2,遍历map集合。
for(Character key : map.keySet()){
Integer value = map.get(key);
sb.append(key+"("+value+")");
}
return sb.toString();
}
}