java学习笔记(17)

版权声明:版权所有,如需引用,请注明出处! https://blog.csdn.net/DHRMM999/article/details/83789943

1:Map(掌握)
(1)将键映射到值的对象。一个映射不能包含重复的键;每个键最多只能映射到一个值。
(2)Map和Collection的区别?
A:Map 存储的是键值对形式的元素,键唯一,值可以重复。夫妻对
B:Collection 存储的是单独出现的元素,子接口Set元素唯一,子接口List元素可重复。光棍
(3)Map接口功能概述(自己补齐)
A:添加功能
B:删除功能
C:判断功能
D:获取功能
E:长度功能
(4)Map集合的遍历
A:键找值
a:获取所有键的集合
b:遍历键的集合,得到每一个键
c:根据键到集合中去找值
B:键值对对象找键和值
a:获取所有的键值对对象的集合
b:遍历键值对对象的集合,获取每一个键值对对象
c:根据键值对对象去获取键和值

代码体现:
Map<String,String> hm = new HashMap<String,String>();

hm.put(“it002”,“hello”);
hm.put(“it003”,“world”);
hm.put(“it001”,“java”);

//方式1 键找值
Set set = hm.keySet();
for(String key : set) {
String value = hm.get(key);
System.out.println(key+"—"+value);
}

//方式2 键值对对象找键和值
Set<Map.Entry<String,String>> set2 = hm.entrySet();
for(Map.Entry<String,String> me : set2) {
String key = me.getKey();
String value = me.getValue();
System.out.println(key+"—"+value);
}
(5)HashMap
A:HashMap<String,String>
B:HashMap<Integer,String>
C:HashMap<String,Student>
D:HashMap<Student,String>
(6)TreeMap
A:TreeMap<String,String>
B:TreeMap<Student,String>

2:Collections(理解)
(1)是针对集合进行操作的工具类
(2)面试题:Collection和Collections的区别
A:Collection 是单列集合的顶层接口,有两个子接口List和Set
B:Collections 是针对集合进行操作的工具类,可以对集合进行排序和查找等
(3)常见的几个小方法:
A:public static void sort(List list)
B:public static int binarySearch(List<?> list,T key)
C:public static T max(Collection<?> coll)
D:public static void reverse(List<?> list)
E:public static void shuffle(List<?> list)

猜你喜欢

转载自blog.csdn.net/DHRMM999/article/details/83789943