一、知识点
Map<k,v>双泛型;
二、问题
1、HashMap和LinkedHashMap的区别是什么?
HashMap用哈希值存取数据和查询数据;
LinkedHashMap保证记录的顺序,按添加顺序查询数据;
2、Collection集合和Map集合关系和区别是什么?
Collection是单列集合;
Map是双列集合;
3、为什么返回的数据若要定义数据类型要尽量使用包装类例如Integer?
因为返回的null的话,
Integer类型可以接收;
int类型不可以接收;
4、Map集合的put, get, remove, containsKey 方法返回分别是什么?
put返回null或者被替换的value;
get返回的是null或者value;
remove返回的是null或者value;
containKey返回的是true或者false;
只有put()里面要写双参数;
get, remove, containsKey 只需要写单参数key;
5、如何通过键找值的方式遍历Map集合?
使用keySet() 方法获得键值集合;
通过对键值集合进行增强for循环;
public class test {
public static void main(String[] args) {
Map<String,String> map = new HashMap<>();
System.out.println(map.put("黄晓明","杨颖"));
System.out.println(map.put("贾乃亮","李小璐"));
Set<String> mapKey = map.keySet();
for (String s : mapKey) {
System.out.println(s + "*" + map.get(s));
}
}
}
6、Entry对象是什么?
Map集合中没加入一对元素,就增加一个Entry对象‘
Entry对象的数据类型是,该Map.Entry<k,v>;
想要得到Entry集合的方法是map.entrySet();
7、如何通过Entry对象遍历集合?
Entry自带的**getKey()方法和getValue()**方法;
public class test {
public static void main(String[] args) {
Map<String,String> map = new HashMap<>();
map.put("黄晓明","杨颖");
map.put("贾乃亮","李小璐");
Set<Map.Entry<String, String>> set = map.entrySet();
for (Map.Entry<String, String> entry : set) {
System.out.println(entry.getKey());
System.out.println(entry.getValue());
}
}
}
8、自定义数据类型或引用数据类型作为key, value时有何不同?
自定义类型作为键值时需要重写hashCode() 和 equals() 方法,因为要保证键值唯一;
9、为什么重写hashCode()方法和equals()方法就能保证键值唯一?
10、LinkedHashMap集合比HashMap集合多了什么功能?
HashMap 存和查的顺序可能不一致;
LinkedHashMap 存和查的顺序一致;
LinkedHashMap = 哈希表 + 链表(记录存储顺序);
11、HashMap 和 HashTable 的共同点和区别分别是什么?
共同点:都 extends Map;
区别:HashMap(之前学的所有集合都可) 允许 key 和 value 有 Null, HashTable 不允许;
区别: HashMap 多线程,线程不安全,快,非同步;HashTable 单线程,线程安全,慢,同步;
12、HashTable的地位?
Hashtable和Vector集合一样,在JDK1.2之后被HashMap 和 ArrayList 取代;
Hashtable的子类Properties依然活跃;
Properties集合是一个唯一和IO流相结合的集合;