Map概述(java.util.map):
Map是按<键,值>对形式存储数据的
HashMap (HashMap <K,V>-数列表)底层是哈希表
Hashtable (java.util.Hashtable <K,V>)底层是哈希表 它是线程安全的-Properties子类 键与值都是String类型,常用于设置/读取属性
ConcurrentHashMap, ConcurrentSkipListMap,
SortedMap<K,V> 根据键自然排序要求键都是可比较的 Comparator/Comparable
TreeMap<K,V> 可以根据键自然排序,排序的原理是:二叉树
------
Map的基本操作(集合)
void dear()清除所有的<键,值>对
boolean containsKey(Object key) 判断是否包含指定的键
boolean containsValue(Object value) 判断是否包含指定的值
Set<Map.Entry<K,V>> entrySet() 返回entry的集合,一个<键,值>对就是一个Entry
V get(Object key) 返回key对应的值
boolean isEmpty()判断是否为空
Set keySet()返回键的集合
V put(K key,V value) 向map中添加<key,value>对,如果键 key已存在,使用value值替换原来的值,Map中的键是不能重复的
V remove(Object key) 只要key匹配就删除对应的<键,值>对
default boolean remove(Object key,Object Value)要求<key,value>都匹配才删除
default V replace(K key,V value)替换
default boolean repalce(K key,V oldValue, V newValue)
int size() 返回<键,值>对的数量
Collection values()返回值的集合
public class MapDemo {
public static void main(String[] args) {
//1)创建map集合,存储<员工 :姓名 工资>
Map<String,Integer> map=new HashMap<>();
//2)添加数据 put(k,v)
map.put("小王",9000);
map.put("nana",13000);
map.put("小李",20000);
map.put("小磊",6000);
System.out.println(map);//{小李=30000, nana=13000, 小王=9000}
//3)键已存在 可替换值
map.put("小李",10000);
System.out.println(map);//{小李=35000, nana=13000, 小王=9000}
System.out.println(map.size());//<键,值>对的数量
//nana的工资
System.out.println(map.get("nana"));
System.out.println(map.get("nana01"));
//4)修改 替换
map.replace("nana",35000);
System.out.println(map.get("nana"));
//5)删除
map.remove("小王");
System.out.println(map);
map.remove("nana",100);//需要匹配键 值
System.out.println(map);
//6)判断
System.out.println(map.containsKey("nana")); //true
System.out.println(map.containsValue(35000));
System.out.println(map.containsValue(1));//false
//7)返回所有键的集合
Set<String> keySet=map.keySet();
System.out.println(map.keySet());
//8)返回所有值的集合
// System.out.println(map.values());
Collection<Integer> values=map.values();//定义一个集合 values
Iterator<Integer> iterator=values.iterator();
while (iterator.hasNext()){
Integer integer=iterator.next();
System.out.println(integer);
}
System.out.println();
//9)返回所有entry的集合
System.out.println(map.entrySet());
Set<Map.Entry<String,Integer>> entrySet=map.entrySet();
for (Map.Entry<String,Integer> entry:entrySet){ //entry就是一个键值对
System.out.println(entry.getKey()+":"+entry.getValue());
}
}
}
统计字符串出现的次数
String text="sdddddhjhjhjhjhkllluuuishdshdshdh";
//1)定义一个map保存<字符,次数>
//3)打印结果
Map<Character,Integer> map=new HashMap<>();
//2)遍历字符串的每个字符
for(int i=0;i<text.length();i++){
//2.1 如果字符是第一次出现,把<字符,1>添加到map中
char cc=text.charAt(i);
if(!map.containsKey(cc)){
map.put(cc,1);
}else{
//获取次数
// 2.2如果字符不是第一次出现,把map中的该字符的次数取出来+1保存到map中
int count=map.get(cc);
map.replace(cc,count+1);
}
}
System.out.println(map);
总结:
TreeMap:
二叉树有一个根结点,左子树和右子树;左子树和右子树也分别是一个二叉树
二叉树的每个结点最多有2颗子树
1)TreeMap实现了SortedMap ,可以根据键自然排序,排序原理是二叉树原理
2)TreeMap的键必须是可比较的
3)TreeMap的键可以是自定义类型,这种情况很少见,一般键都是String类型
hashmap 与 treemap如何选择:
如果不需要根据键排序 选择HashMap
如果需要根据键排序 选择TreeMap
注意:
HashMap中的键需要重写equals()和HashCode()方法
TreeMap中的键是根据Comparable /comparator 的比较结果为0来判断是否相等