1.HashMap
1.按照值传递
package com.jq.LeetCode;
import java.util.HashMap;
public class Code2_HashMap {
public static void main(String[] args) {
//(K V)表
HashMap<String, String> map = new HashMap<>();
map.put("小米","颗粒大小为20cm"); //map里面添加数据 即是查询操作也是更新操作
System.out.println(map.containsKey("小米")); //是否包含 key="小米"
System.out.println(map.get("小米")); //获取 key="小米" 对应的value值
map.remove("小米"); //删除操作
System.out.println(map.containsKey("小米")); //是否包含 key="小米"
System.out.println(map.get("小米")); //获取 key="小米" 对应的value值
}
}
1.2按照引用传递
非原生,自定义的结构
2.TreeMap
package com.jq.LeetCode;
import java.util.TreeMap;
public class Code3_TreeMap {
public static void main(String[] args) {
TreeMap<Integer,String> treeMap1 =new TreeMap<>();
//添加数据
treeMap1.put(3,"我是数字3");
treeMap1.put(5,"我是数字5");
treeMap1.put(6,"我是数字6");
treeMap1.put(0,"我是数字0");
treeMap1.put(1,"我是数字1");
//根据键值判断是否存在
System.out.println(treeMap1.containsKey(3));
System.out.println(treeMap1.containsKey(5));
System.out.println(treeMap1.containsKey(2));
//查询更新操作
treeMap1.put(3,"他是3");
//根据键 获取对应的value值
System.out.println(treeMap1.get(3));
//删除操作
treeMap1.remove(3);
System.out.println(treeMap1.get(3));
// 获取 HashMap中 最小的key
System.out.println(treeMap1.firstKey());
// 获取 HashMap中 最大的key
System.out.println(treeMap1.lastKey());
// <=5 离5最近的key告诉我
System.out.println(treeMap1.floorKey(5));
// >=5 离5最近的key告诉我
System.out.println(treeMap1.ceilingKey(5));
}
}