Map 学习总结
Collections 工具类
今天我们学习了Collections。
Collections是对集合进行操作的类,里面都是静态的方法。
具体主要的功能有:
1. 对集合进行排序。
public static <T> void sort(List<T> list);
例子:
ArrayList<Integer> list = new ArrayList<>();
list.add(30);
list.add(20);
list.add(50);
list.add(10);
list.add(40);
list.add(10);
//对集合进行排序(升序)
Collections.sort(list);
System.out.println(list);
2.对集合进行查找。(二分查找)
public static <T> int binarySearch(Collection<E> c, T key);
例子:
//集合中的二分查找不需要排序 返回的当前元素所在集合中的索引值
int binarySearch = Collections.binarySearch(list, 50);
System.out.println(binarySearch);
3.找出集合中的最大值。
public static <T> max(Collection<?> coll);
例子:
Integer max = Collections.max(list);
System.out.println(max);
4.找出集合中的最小值。
Integer min = Collections.min(list);
System.out.println(min);
5.对集合进行翻转。
public static void reverse(List<?> list):反转
例子:
//翻转该集合
Collections.reverse(list);
System.out.println(list);
Map集合
Map就是储存一对键值对的集合。通过键映射到值,每个键只能映射一个值,值能相同,但是键是唯一的。
Map特有的功能方法:
1.添加
value put(key,value):返回前一个和key关联的值,如果没有,则返回null。
Map<String, String> map = new HashMap();
map.put("乌鸦", "坐飞机");
System.out.println(map.put("黑虎", "掏心")); //null
map.put("猴子", "偷桃");
map.put("老汉", "推车");
System.out.println(map);
结果是:
null
{乌鸦=坐飞机, 猴子=偷桃, 老汉=推车, 黑虎=掏心}
2.删除
void clear():清空map集合
value remove(key):根据指定的key删除这个键值对
String remove = map.remove("乌鸦");
System.out.println(remove);
map.clear();
System.out.println(map);
3.判断
boolean containsKey(key): 判断集合中是否包含指定键
boolean containsValue(value):判断集合中是否包含指定值
boolean isEmpty():判断集合是否为空
boolean containsKey = map.containsKey("猴子");
System.out.println(containsKey);
boolean containsValue = map.containsValue("老汉");
System.out.println(containsValue);
boolean empty = map.isEmpty();
System.out.println(empty);
4.获取
value get(key):通过键获取值,如果没有该键返回null.
也可以通过返回null,来判断是否包含指定键。
int size():获取键值对的个数。
String string = map.get("猴子");
System.out.println(string);//偷桃
System.out.println(map.size());//4
HashMap
HashMap:是基于哈希表的Map接口的实现。
哈希表的作用是用保证唯一性的。
例子:
HashMap<Integer, String> map = new HashMap<>();
其中Integer是键的类型,String是值的类型。
HashMap集合的遍历
HashMap<Integer, String> map = new HashMap<>();
map.put(1, "黑虎");
map.put(23, "青龙");
map.put(14, "凤凰");
map.put(15, "玄武");
Set<Entry<Integer,String>> entrySet = map.entrySet();
for (Entry<Integer, String> entry : entrySet) {
System.out.println(entry.getKey()+"----"+entry.getValue());
}
System.out.println("-------------------------------");
Set<Integer> set = map.keySet();
for (Integer integer : set) {
System.out.println(integer+"----"+map.get(integer));
}
当引用类型作为键时,键的内容一样,地址不一样,所以会存两个相同内容的键值对。这时需要重写该引用类型中的
int hashCode()方法和boolean equals(Object obj)方法,才能根据内容进行比较。
例子:
HashMap<Worker, Integer> map = new HashMap<>();
map.put(new Worker("张三", 16), 8800);
map.put(new Worker("李四", 26), 1800);
map.put(new Worker("王五", 36), 4800);
map.put(new Worker("张三", 46), 2800);
map.put(new Worker("张三", 16), 1800);
该HashMap集合中的Worker类型,已经在Work类中重写了hashCode()和equals()方法。
LinkedHashMap
LinkedHashMap:该集合是有序唯一的集合。
例子:
LinkedHashMap<Integer, String> map = new LinkedHashMap<>();
map.put(9527, "张三");
map.put(8527, "李四");
map.put(7527, "王五");
map.put(1527, "张龙");
map.put(5527, "赵虎");
map.put(1527, "包青天");
System.out.println(map);
TreeMap
TreeMap:是基于二叉树的Map接口的实现。
例子:
TreeMap<String,String> map = new TreeMap<>();
map.put("张三","老师");
map.put("李四","教导主任");
map.put("王五","体育老师");
map.put("赵四","校长");
map.put("马统","校长小舅");
System.out.println(map);
ArrayList<HashMap<String, String>> arrayList = new ArrayList<>();就是ArrayList集合中储存着HashMap集合。(集合的嵌套)
例子:
ArrayList<HashMap<String, String>> arrayList = new ArrayList<>();
HashMap<String, String> sanguo = new HashMap<>();
sanguo.put("周瑜", "小乔");
sanguo.put("吕布", "貂蝉");
arrayList.add(sanguo);//将HashMap集合添加到arrayList集合中去。
HashMap<String, String> xiyouji = new HashMap<>();
xiyouji.put("牛魔王", "铁扇公主");
xiyouji.put("至尊宝", "紫霞仙子");
arrayList.add(xiyouji);
HashMap<String, String> wuxia = new HashMap<>();
wuxia.put("郭靖", "黄蓉");
wuxia.put("杨过", "大雕");
arrayList.add(wuxia);
System.out.println(arrayList);
for (HashMap<String, String> hashMap : arrayList) { //先遍历最大的集合
Set<Entry<String, String>> entrySet = hashMap.entrySet();
for (Entry<String, String> entry : entrySet) {
System.out.println(entry.getKey()+"--"+entry.getValue());
}
}
HashTabel和HashMap的区别:
Hashtable线程安全,效率低,不许null键和null值
HashMap线程不安全,效率高,允许null键和null值
List,Set,Map等接口是否都继承了子Map接口
List,Set不是自称Map接口,他们是继承于Collection接口
Map接口本身就是一个顶层接口