一、Map接口中键和值一一映射,可以通过键来获取值
1.给定一个键和一个值,你可以将该值存储在一个Map对象.之后,你可以通过键来访问对应的值
2.当访问的值不存在的时候,方法就会抛出一个NoSuchElementException异常
3.当对象的类型和Map里元素类型不兼容的时候,就会抛出一个ClassCastException异常
4.当这种不允许使用Null对象的Map中使用Null对象,会抛出一个NullPointerException异常
5.当尝试修改一个只读的Map时,会抛出一个UnsupportedOperationException异常
二、Map接口代码例子:
Map接口 key-value 根据一个对象查找对象. HashMap、HashTable、TreeMap是它的实现类
public class HashMapAndHashTable {
public static void main(String[] args) {
//如果是基本数据类型,声明的map的时候使用包装类
Map<Integer, String> map = new HashMap<>();
//HashMap和Hashtable用法基本一致,只是内部的特性不完全一样,外部差不多//Map<Integer, String> map = new Hashtable<>();//HashMap和Hashtable本质上是没有什么区别的,用法基本一致,Hashtable用的比较少,HashMap常见
//添加数据 put当key不存在时,添加key-value
map.put(0, "str0");
map.put(1, "str1");
map.put(2, "str2");
map.put(3, "str3");
map.put(4, "str4");
map.put(5, "str5");
System.out.println(map);
// put 当key存在时,修改key对应的value
map.put(5, "111111");
System.out.println(map);
//
map.put(6, null);
map.put(null, "11111");
System.out.println(map);
// 移除 remove(key)
map.remove(null);
System.out.println(map);
//
// 判断是否存在key
System.out.println("是否存在key:5===》" + map.containsKey(5));
// 判断是否存在value
System.out.println("是否存在Value:str4====>" + map.containsValue("str4"));
//
// 清空map
// map.clear();
//
System.out.println("map是否为空:" + map.isEmpty());
// 输出
// System.out.println(map);
// // 遍历
Set<Integer> keysSet = map.keySet();
Iterator<Integer> iterator = keysSet.iterator();
while (iterator.hasNext()) {
Integer intKey = iterator.next();
System.out.println("key:" + intKey + "---->Value:"
+ map.get(intKey));
}
// System.out.println("--------------------");
for (Iterator<Integer> iterator2 = keysSet.iterator(); iterator2.hasNext();) {
int intKey = iterator2.next();
System.out.println("key:" + intKey + "---->Value:"
+ map.get(intKey));
}
// System.out.println("--------------------");
for (int intKey : keysSet) {
System.out.println("key:" + intKey + "---->Value:"
+ map.get(intKey));
}
}
}
输出结果:
三、TreeMap类
TreeMap类代码例子如下:
public class TreeMapDemo {
public static void main(String[] args) {
TreeMap<Integer, String> map=new TreeMap<>();
//
map.put(1,"Str1");
map.put(2,"Str2");
map.put(3,"Str3");
map.put(4,"Str4");
map.put(5,"Str5");
map.put(6,"Str6");
System.out.println(map);//默认的打印方式(即输出)
//返回最小的值
System.out.println(map.firstKey());//第一个返回他的key值 //返回最小的1
System.out.println(map.firstEntry());//第二个返回他的键值对,自带等号
//返回最大值
System.out.println(map.lastKey());//返回最大的6
System.out.println(map.lastEntry());
//要比我给的值大
System.out.println(map.higherKey(3));//在map中成对出现 //要比我给的值大
System.out.println(map.higherEntry(3));//要大于我给的值,这里我给定的值为3,它要大于3,否则为空null,值为6则为空
//要比我给的值小
System.out.println(map.lowerKey(3));//要比我给的值小,否则返回为空null,,,, key和Entry是成对出现的
System.out.println(map.lowerEntry(3));
//这里我给3,它返回的值要比3小,不然为空
//截断
System.out.println(map.headMap(4));//小于4的
System.out.println(map.tailMap(2));//大于等于2的
System.out.println(map.subMap(2, 5));//2-5之间的//[左闭 右开)
}
}
以下为上面所有输出的结果:
{1=Str1, 2=Str2, 3=Str3, 4=Str4, 5=Str5, 6=Str6}
1
1=Str1
6
6=Str6
4
4=Str4
2
2=Str2
{1=Str1, 2=Str2, 3=Str3}
{2=Str2, 3=Str3, 4=Str4, 5=Str5, 6=Str6}
{2=Str2, 3=Str3, 4=Str4}