Java基础教程——Map
Map
返回类型 | 方法 | 描述 |
---|---|---|
V | get(Object key) | 根据key取得value |
V | put(Obejct k,Object v) | 向Map中加入(替换)元素,返回之前的Value;之前没有元素就返回null |
V | remove(Object key) | 根据key删除元素,返回Value对应的value |
void | clear() | 清空 |
int | size() | 获得集合的长度 |
boolean | isEmpty() | 判断是否为空 |
boolean | containsKey(Object object) | 判断指定的key是否存在 |
boolean | containsValue(Object value) | 判断指定的value是否存在 |
Set | keySet() | 所有key的集合 |
Collection | values() | 所有value |
HashMap
存储K-V,使用key来区分。
import java.util.*;
public class TestHashMap {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>();
// 新k返回null,旧k返回旧v
String put = map.put(1, "A");
System.out.println("之前没有此K的V:" + put);
put = map.put(1, "B");
System.out.println("之前有同K的V:" + put);
System.out.println(map);
// get:有k返回v,无k返回null
String v = map.get(2);
System.out.println(v);
v = map.get(1);
System.out.println(v);
// containsKey/containsValue
boolean containsKey = map.containsKey(2);
boolean containsValue = map.containsValue("B");
// remove:删k返回v;删无可删,返回null
String remove = map.remove(2);
System.out.println(remove);
remove = map.remove(1);
System.out.println(remove);
}
}
遍历:
package ah;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class TestMap1Hash {
public static void main(String[] args) {
Map<String, String> _map = new HashMap<String, String>();
_map.put("1", "悟空");
_map.put(null, "白龙");
_map.put("2", "悟能");
_map.put("3", "悟净");
System.out.println("------foreach语法遍历map(输出K-V)------");
for (String _key : _map.keySet()) {
System.out.print("key = " + _key);
System.out.println(" value = " + _map.get(_key));
}
System.out.println("------Java 8.forEach:Lambda------");
_map.forEach((k, v) -> System.out.println(k + ":" + v));
System.out.println("------使用迭代器迭代map(输出V)------");
// 1.获取值的Collection
Collection<String> _values = _map.values();
// 2.通过Collection获得迭代器
Iterator<String> it = _values.iterator();
// 3.输出值
while (it.hasNext()) {
String next = it.next();
System.out.println(next);
}
System.out.println("-----Map.Entry<K,V>-----");
// Map.Entry<K,V>是Map的内部接口,称为映射项(键-值对)
Set<Entry<String, String>> entrySet = _map.entrySet();
System.out.println("=====Map.Entry<K,V>:for循环=====");
for (Entry<String, String> entry : entrySet) {
System.out.println(entry.getKey() + ":" + entry.getValue());
}
System.out.println("=====Map.Entry<K,V>:迭代器=====");
Iterator<Entry<String, String>> iterator = entrySet.iterator();
while (iterator.hasNext()) {
Entry<String, String> next = iterator.next();
System.out.println(next.getKey() + ":" + next.getValue());
}
}
}
LinkedHashMap
自带链表(记录元素顺序),具有可预知的迭代顺序。
Hashtable (不接受null)
Hashtable是一个比较老的类,甚至没有遵循Java命名规范。尽量少用。
但是Hashtable有一个重要的子类——java.util.Properties。
public class Properties extends Hashtable<Object,Object> {...}
父以子贵,尽管Hashtable连命名都不规范,还是不会被淘汰。Properties类会在IO章节中介绍。
Hashtable是同步的,速度较慢。
两个map使用上是基本一致,但是Hashtable限制性更强一些,K和V都不接受null——
- 不接受
- 不提示
- 运行时出错
// X m.put(null, "NNN");
// X m.put("NNN", null);
HashMap对比Hashtable:
null键 | null值 | 重复键 | |
---|---|---|---|
HashMap | ● | ● | × |
Hashtable | × | × | × |
JAVA 9:of方法
List接口、Set接口、Map接口里增加静态方法of,用于初始化集合。
集合元素个数不可变,添加新元素会抛出UnsupportedOperationException
of方法只适用于接口,不用于实现类。
Set、Map调用of方法时不能有重复的元素,如果重复会抛出IllegalArgumentException(不合法参数异常)
import java.util.*;
public class TestJ9of {
public static void main(String[] args) {
List<String> lst = List.of("a", "b");
// lst.add("c");//UnsupportedOperationException
System.out.println(lst);
// lang.IllegalArgumentException: duplicate element: a
Set<String> set = Set.of("a", "b");
// Set<String> set = Set.of("a", "b","a");
System.out.println(set);
Map<Integer, String> map = Map.of(1, "A", 2, "B");
System.out.println(map);
}
}