Map<K,V>  HashMap

        Map是一个双列集合,一个元素包含两个值(一个key,一个value)

        Map集合中的元素,key和value的数据类型可以相同,也可以不同

        Map中的元素,key不允许重复,value可以重复

        key一样的,会覆盖

import java.util.Collection;
import java.util.HashMap;
import java.util.Set;

public class HashMap1 {
public static void main(String[] args) {
HashMap<String,String> hm = new HashMap<>();
//hm是否为空
System.out.println(hm.isEmpty());//true // Empty空的
hm.put("s1","java");
hm.put("s23","mysql");
hm.put("s2"," php");
hm.put("s1","1000");
System.out.println(hm);//{s23=mysql, s1=1000, s2= php}

System.out.println(hm.isEmpty());
System.out.println(hm.size());//3
System.out.println("----------------");
System.out.println(hm.containsKey("s3"));// false //判断key中有没有s3
System.out.println(hm.containsValue("java"));//判断value中有没有java
//删除
// hm.clear();
hm.remove("s1");
System.out.println(hm);//{s23=mysql, s2= php} //根据key来删除元素
hm.remove("php");
System.out.println(hm);//{s23=mysql, s2= php}//根据value不能删除
hm.remove("s2","mysql");
System.out.println(hm);//{s23=mysql, s2= php}

String str = hm.replace("s23","javascript");
//修改key的value,将旧的value返回,如果输入的key没有,则返回null,不修改
System.out.println(str);//mysql
System.out.println(hm);//{s23=javascript, s2= php}

System.out.println(hm.get("s23"));//javascript
//获取s7的value,若存在返回对象,若不存在使用默认值html
System.out.println(hm.getOrDefault("s7","html"));
System.out.println(hm.get("s5"));//返回不存在的key,value为null


//方法一遍历value值
Collection<String> col = hm.values();
// System.out.println(col);//遍历value值
//方法二
for(Object o : col){
System.out.println(o);
}
System.out.println("---------------");
//遍历key值
Set<String> keys = hm.keySet();
System.out.println(keys);
for(String k :keys){
System.out.printf("%s = %s %n" , k , hm.get(k));
}

System.out.println("================");
//s23 = javascript s2 = php
hm.forEach((k,v) -> System.out.printf("%s = %s %n" , k , v));


}
}
import java.util.*;

public class HashMap2 {
public static void main(String[] args) {
Map<String, String> map = new HashMap<>();
map.put("k5", "go");
map.put("k1", "ruby");
map.put("k3", "php");
map.put("k4", "c");
map.put("k2", "java");
map.put("k6", "python");
map.put("k7", "javascript");
map.put("k8", "vs");
Collection<String> v = map.values();
System.out.println(v);//[ruby, java, php, c, go, python, javascript, vs]

//将map的key 集合排序 升序
Set<String> keys = map.keySet();
List<String> likey = new ArrayList<>(keys);
Collections.sort(likey);
System.out.println(likey);//[k1, k2, k3, k4, k5, k6, k7, k8]
//降序
Collections.sort(likey, Comparator.reverseOrder());
System.out.println(likey);//[k8, k7, k6, k5, k4, k3, k2, k1]

for (String k : likey) {
System.out.println(k);//k8 k7 k6 k5 k4 k3 k2 k1
// }
}
}
}
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;

public class HashMap3 {
public static void main(String[] args) {
Map<String, String> map = new HashMap<>();
map.put("k523","go");
map.put("k12","ruby");
map.put("k3","php");
map.put("k4244","c");
map.put("k253454","java");
map.put("k623","python");
map.put("k73","javascript");
map.put("k8","vs");
//按字符串key的长度排序
var k = new ArrayList<>(map.keySet());
System.out.println(k);
//升序
k.sort(Comparator.comparingInt(String::length));
System.out.println(k);
//降序
k.sort((a,b)->b.length()-a.length());
System.out.println(k);
}
}