一、HashMap集合
Map是映射关系的集合,里面的元素是<key,value>的键值对。
1.HashMap的插入元素流程
2.HashMap集合结构
3.HashMap的特点
- 存储的是key-value类型的数据
- 底层实现是链表数组加红黑树
- key值不允许重复,key重复会被覆盖,value可以重复
- 存储的键值对是无序的
- key可以为null,value也可以为null
4.put(K key, V value) 添加键值对到HashMap集合
HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put("Tom", 40);
map.put("Anni", 80);
System.out.println(map);
输出结果
{Tom=40, Anni=80}
5.get(K key) 返回指定键所映射的值,没有该key对应的值则返回 null
HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put("Anno", 100);
map.put("Anno", 99);
int score = map.get("Anno");
System.out.println(score);
System.out.println(map.get("as"));
输出结果
99
null
所以相同键的key会替换value.
6.size() 返回HashMap集合中键值对的数量。
HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put("Anno", 100);
map.put("Anno", 99);
System.out.println(map.size());
输出结果:1
7.clear() 清空Map集合
public class Test {
public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put("11", 100);
map.put("22", 90);
map.put("55", 91);
map.clear();// 清空map中的key-value
System.out.println(map.size());
}
}
输出结果:0
8.isEmpty () 判断Map集合中是否为空
HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put("Tom", 100);
map.put("Anno", 90);
map.put("Sam", 91);
System.out.println(map.isEmpty());
map.clear();// 清空map中的key-value
System.out.println(map.isEmpty());
输出结果:
false
true
9.remove(K key) 删除HashMap集合中键为key的数据并返回其所对应value值。
HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put("Tom", 100);
map.put("Jim", 90);
map.put("Sam", 91);
System.out.println(map.remove("Tom"));
System.out.println(map);
输出结果:
100
{Sam=91, Jim=90}
10.containsKey(K key) Hashmap判断是否含有目标key
public static void main(String[] args) {
HashMap<String, Integer> map=new HashMap<>();
System.out.println(map.containsKey("aaa"));
map.put("aaa", 1);
System.out.println(map.containsKey("aaa"));
}
输出结果:
false
true
11.containsValue(Kvalue) Hashmap判断是否含有目标value
public static void main(String[] args) {
HashMap<String, Integer> map=new HashMap<>();
System.out.println(map.containsValue(1));
map.put("DEMO", 1);
System.out.println(map.containsValue(1));
}
输出结果
false
true
12.Hashmap添加另一个同一类型的map下的所有数据
HashMap<String, Integer> map=new HashMap<>();
HashMap<String, Integer> map1=new HashMap<>();
map1.put("DEMO1", 1);
map1.put("DEMO3", 3);
map.put("DEMO2", 2);
System.out.println("map集合内容");
System.out.println(map);
System.out.println("map1集合内容");
System.out.println(map1);
map.putAll(map1);
System.out.println("map集合内容");
System.out.println(map);
输出结果:
map集合内容
{DEMO2=2}
map1集合内容
{DEMO1=1, DEMO3=3}
map集合内容
{DEMO1=1, DEMO2=2, DEMO3=3}
13.replace(K key1,V value1)替换key1的value值为value1,并返回被替换的value
HashMap<String, Integer> map=new HashMap<>();
map.put("DEMO1", 1);
map.put("DEMO2", 100);
System.out.println(map);
System.out.println(map.replace("DEMO2", 1));
System.out.println(map);
输出结果:
{DEMO1=1, DEMO2=100}
100
{DEMO1=1, DEMO2=1}
14.分别返回key和value的集合
<1>返回key的集合(keySet())
<2>返回value的集合(values())
public static void main(String[] args) {
String s = "我爱中国";
HashMap<Integer,Character> map = new HashMap<>();
for(int i=0;i<s.length();i++){
map.put(i,s.charAt(i));
}
System.out.println("输出Key:"+map.keySet());
System.out.println("输出Value:"+map.values());
}
输出结果:
输出Key:[0, 1, 2, 3]
暑促Value:[我, 爱, 中, 国]
15.使用getOrDefault(Object key, V defaultValue) 方法记录一个字符串中每个字符串出现的次数
功能:获得一个返回值,如果HashMap中纯在键为key的对象,该方法就返回该键对应的值,若不存在则返回defaultValue;
演示:
通过这段记录字数数量的代码来体会getOrDefault方法
String s = "ababbc";
HashMap<Character, Integer> counter = new HashMap();
for (int i = 0; i < s.length(); i++) {
counter.put(s.charAt(i), counter.getOrDefault(s.charAt(i), 0) + 1);
}
System.out.println(counter);
输出:
{a=2, b=3, c=1}
二、TreeMap集合
TreeMap的特点:
- TreeMap底层是通过红黑树实现的
- treeMap的key是有序的
- TreeMap集合默认会对key键进行排序,所以键必须实现自然排序(字典排序),数字(升序排序)和定制排序(重写了compare方法)
public static void main(String[] args) {
Map<Person, Integer> map = new TreeMap<>(new Comparator<Person>() {
public int compare(Person p1, Person p2) {
return p1.name.compareTo(p2.name);
}
});
map.put(new Person("Tom"), 1);
map.put(new Person("Bob"), 2);
map.put(new Person("Lily"), 3);
for (Person key : map.keySet()) {
System.out.println(key);
}
System.out.println(map.get(new Person("Bob")));
}
class Person {
public String name;
Person(String name) {
this.name = name;
}
public String toString() {
return "{Person: " + name + "}";
}
}
输出结果
{Person: Bob}
{Person: Lily}
{Person: Tom}
2