Map集合
一、Map集合概述和特点
1,概述
*将键映射到值的对象
*一个映射不能包含重复的键
*每一个键最多只能映射到一个值
2,Map接口和Collection接口的不同
*Map是双列的,Collection是单列的
*Map的键唯一,Collection的子体系Set是唯一的
*Map集合的数据结构值针对键有效,跟值无关;Collection集合的数据结构是针对元素有效
二、Map集合的功能
* a:添加功能
* V put(K key,V value):添加元素。
* 如果键是第一次存储,就直接存储元素,返回null
* 如果键不是第一次存在,就用值把以前的值替换掉,返回以前的值
* b:删除功能
* void clear():移除所有的键值对元素
* V remove(Object key):根据键删除键值对元素,并把值返回
* c:判断功能
* boolean containsKey(Object key):判断集合是否包含指定的键
* boolean containsValue(Object value):判断集合是否包含指定的值
* boolean isEmpty():判断集合是否为空
* d:获取功能
* Set<Map.Entry<K,V>> entrySet():
* V get(Object key):根据键获取值
* Set<K> keySet():获取集合中所有键的集合
* Collection<V> values():获取集合中所有值的集合
* e:长度功能
* int size():返回集合中的键值对的个数
三、Map集合的遍历之键查找值
1,思路:
* 获取所有键的集合
* 遍历键的集合,获取到每一个键
* 根据键查找值
2,例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | HashMap<String, Integer> hm = new HashMap<>(); hm.put("张三", 23); hm.put("李四", 24); hm.put("王五", 25); hm.put("赵六", 26); /*Set<String> keySet = hm.keySet(); //获取集合中所有的键 Iterator<String> it = keySet.iterator(); //获取迭代器 while(it.hasNext()) { //判断单列集合中是否有元素 String key = it.next(); //获取集合中的每一个元素,其实就是双列集合中的键 Integer value = hm.get(key); //根据键获取值 System.out.println(key + "=" + value); //打印键值对 }*/ for(String key : hm.keySet()) { //增强for循环迭代双列集合第一种方式 System.out.println(key + "=" + hm.get(key)); } |
3,图解:
四、Map集合的遍历之键值对对象找键和值
1,思路:
* 获取所有键值对对象集合
* 遍历键值对对象的集合,获取到每一个键值对对象
* 根据键值对对象找键和值
2,例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | HashMap<String, Integer> hm = new HashMap<>(); hm.put("张三", 23); hm.put("李四", 24); hm.put("王五", 25); hm.put("赵六", 26); /*Set<Map.Entry<String, Integer>> entrySet = hm.entrySet(); //获取所有的键值对象的集合 Iterator<Entry<String, Integer>> it = entrySet.iterator();//获取迭代器 while(it.hasNext()) { Entry<String, Integer> en = it.next(); //获取键值对对象 String key = en.getKey(); //根据键值对对象获取键 Integer value = en.getValue(); //根据键值对对象获取值 System.out.println(key + "=" + value); }*/ for(Entry<String, Integer> en : hm.entrySet()) { System.out.println(en.getKey() + "=" + en.getValue()); } |
3,图解:
五、HashMap与LinkedHashMap
1,HashMap演示:
1 2 3 4 5 6 7 8 9 10 11 12 13 | /* HashMap集合键是Student值是String的案例 * 键是学生对象,代表每一个学生 * 值是字符串对象,代表学生归属地 */ public static void main(String[] args) { HashMap<Student, String> hm = new HashMap<>(); hm.put(new Student("张三", 23), "北京"); hm.put(new Student("张三", 23), "上海"); hm.put(new Student("李四", 24), "广州"); hm.put(new Student("王五", 25), "深圳"); System.out.println(hm); } |
2,LinkedHashMap可以保证怎么存就怎么取
3,HashMap和Hashtable的区别
* Hashtable是JDK1.0版本出现的,是线程安全的,效率低,HashMap是JDK1.2版本出现的,是线程不安全的,效率高
* Hashtable不可以存储null键和null值,HashMap可以存储null键和null值
六、TreeMap
可实现对存储的元素进行排序,原理同TreeSet。对自定义类有两种方式实现
例1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | /** * * A:案例演示 * TreeMap集合键是Student值是String的案例 */ public static void main(String[] args) { //demo1(); TreeMap<Student, String> tm = new TreeMap<>(new Comparator<Student>() { @Override public int compare(Student s1, Student s2) { int num = s1.getName().compareTo(s2.getName()); //按照姓名比较 return num == 0 ? s1.getAge() - s2.getAge() : num; } }); tm.put(new Student("张三", 23), "北京"); tm.put(new Student("李四", 13), "上海"); tm.put(new Student("赵六", 43), "深圳"); tm.put(new Student("王五", 33), "广州"); System.out.println(tm); } |
例2:
1,先定义Student类,实现Comparable接口,重写compareTo方法,具体排序规则在compareTo方法中规定
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | public class Student implements Comparable<Student> { private String name; private int age; get方法,set方法,toString方法 @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + age; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Student other = (Student) obj; if (age != other.age) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } @Override public int compareTo(Student o) { int num = this.age - o.age; //以年龄为主要条件 return num == 0 ? this.name.compareTo(o.name) : num; } } |
2,然后new对象存储即可
1 2 3 4 5 6 7 8 9 | public static void demo1() { TreeMap<Student, String> tm = new TreeMap<>(); tm.put(new Student("张三", 23), "北京"); tm.put(new Student("李四", 13), "上海"); tm.put(new Student("王五", 33), "广州"); tm.put(new Student("赵六", 43), "深圳"); System.out.println(tm); } |
七、Collections工具类
* 针对集合操作 的工具类
public static <T> void sort(List<T> list) //将集合排序
public static <T> int binarySearch(List<?> list,T key) //根据默认排序结果获取集合中的最大值
public static <T> T max(Collection<?> coll) //随机置换,可以用来洗牌
public static void reverse(List<?> list) //反转集合
public static void shuffle(List<?> list) //随机置换,可以用来洗牌