HashMap和TreeMap详解
来源:互联网 发布:社交网络观后感 编辑:程序博客网 时间:2024/05/08 05:00
概述:
如果程序中存储了几百万个学生,而且经常需要使用学号来搜索某个学生,那么这个需求有效的数据结构就是Map。Map是一种依照键(key)存储元素的容器,键(key)很像下标,在List中下标是整数。在Map中键(key)可以使任意类型的对象。Map中不能有重复的键(Key),每个键(key)都有一个对应的值(value)。一个键(key)和它对应的值构成map集合中的一个元素。Map中的元素是两个对象,一个对象作为键,一个对象作为值。键不可以重复,但是值可以重复。看顶层共性方法找子类特有对象.Map与Collection在集合框架中属并列存在Map存储的是键值对Map存储元素使用put方法,Collection使用add方法Map集合没有直接取出元素的方法,而是先转成Set集合,在通过迭代获取元素Map集合中键要保证唯一性也就是Collection是单列集合, Map 是双列集合。 总结: Map一次存一对元素, Collection 一次存一个。Map 的键不能重复,保证唯一。 Map 一次存入一对元素,是以键值对的形式存在.键与值存在映射关系.一定要保证键的唯一性.
K - 此映射所维护的键的类型V - 映射值的类型概念将键映射到值的对象。一个映射不能包含重复的键;每个键最多只能映射到一个值。特点Key和Value是1对1的关系,如:门牌号 :家 老公:老婆
双列集合
Map学习体系:
—| Map 接口 将键映射到值的对象。一个映射不能包含重复的键;每个键最多只能映射到一个值。
————| HashMap 采用哈希表实现,所以无序
————| TreeMap 可以对健进行排序
---|Hashtable: 底层是哈希表数据结构,线程是同步的,不可以存入null键,null值。 效率较低,被HashMap 替代。---|HashMap: 底层是哈希表数据结构,线程是不同步的,可以存入null键,null值。 要保证键的唯一性,需要覆盖hashCode方法,和equals方法。 ---| LinkedHashMap: 该子类基于哈希表又融入了链表。可以Map集合进行增删提高效率。---|TreeMap: 底层是二叉树数据结构。可以对map集合中的键进行排序。需要使用Comparable或者Comparator 进行比较排序。return 0,来判断键的唯一性。
常见方法
1、添加: 1、V put(K key, V value) (可以相同的key值,但是添加的value值会覆盖前面的,返回值是前一个,如果没有就返回null) 2、putAll(Map<? extends K,? extends V> m) 从指定映射中将所有映射关系复制到此映射中(可选操作)。2、删除 1、remove() 删除关联对象,指定key对象 2、clear() 清空集合对象3、获取 1:value get(key); 可以用于判断键是否存在的情况。当指定的键不存在的时候,返回的是null。3、判断: 1、boolean isEmpty() 长度为0返回true否则false 2、boolean containsKey(Object key) 判断集合中是否包含指定的key3、boolean containsValue(Object value) 判断集合中是否包含指定的value4、长度:Int size()
方法示例
添加:
该案例使用了HashMap,建立了学生姓名和年龄之间的映射关系。并试图添加重复的键。import java.util.HashMap;import java.util.Map;public class Demo1 { public static void main(String[] args) { // 定义一个Map的容器对象 Map<String, Integer > map1 = new HashMap<String, Integer >(); map1.put("jack", 20); map1.put("rose", 18); map1.put("lucy", 17); map1.put("java", 25); System.out.println(map1); // 添加重复的键值(值不同),会返回集合中原有(重复键)的值, System.out.println(map1.put("jack", 30)); //20 Map<String, Integer> map2 = new HashMap<String, Integer>(); map2.put("张三丰", 100); map2.put("虚竹", 20); System.out.println("map2:" + map2);// 从指定映射中将所有映射关系复制到此映射中。 map1.putAll(map2); System.out.println("map1:" + map1); // }}
删除:
// 删除: // remove() 删除关联对象,指定key对象 // clear() 清空集合对象 Map<String, Integer> map1 = new HashMap<String, Integer>(); map1.put("jack", 20); map1.put("rose", 18); map1.put("lucy", 17); map1.put("java", 25); System.out.println(map1); // 指定key,返回删除的键值对映射的值。 System.out.println("value:" + map1.remove("java")); map1.clear(); System.out.println("map1:" + map1);
获取:
// 获取: // V get(Object key) 通过指定的key对象获取value对象 // int size() 获取容器的大小 Map<String, Integer> map1 = new HashMap<String, Integer>(); map1.put("jack", 20); map1.put("rose", 18); map1.put("lucy", 17); map1.put("java", 25); System.out.println(map1); // V get(Object key) 通过指定的key对象获取value对象 // int size() 获取容器的大小 System.out.println("value:" + map1.get("jack")); System.out.println("map.size:" + map1.size());
判断:
// 判断: // boolean isEmpty() 长度为0返回true否则false // boolean containsKey(Object key) 判断集合中是否包含指定的key // boolean containsValue(Object value) Map<String, Integer> map1 = new HashMap<String, Integer>(); map1.put("jack", 20); map1.put("rose", 18); map1.put("lucy", 17); map1.put("java", 25); System.out.println(map1); System.out.println("isEmpty:" + map1.isEmpty()); System.out.println("containskey:" + map1.containsKey("jack")); System.out.println("containsvalues:" + map1.containsValue(100));
4.1. HashMap
底层是哈希表数据结构,线程是不同步的,可以存入null键,null值。要保证键的唯一性,需要覆盖hashCode方法,和equals方法。
案例:自定义对象作为Map的键。
package cn.itcast.gz.map;import java.util.HashMap;import java.util.Iterator;import java.util.Map.Entry;import java.util.Set;public class Demo3 { public static void main(String[] args) { HashMap<Person, String> hm = new HashMap<Person, String>(); hm.put(new Person("jack", 20), "1001"); hm.put(new Person("rose", 18), "1002"); hm.put(new Person("lucy", 19), "1003"); hm.put(new Person("hmm", 17), "1004"); hm.put(new Person("ll", 25), "1005"); System.out.println(hm); System.out.println(hm.put(new Person("rose", 18), "1006")); Set<Entry<Person, String>> entrySet = hm.entrySet(); Iterator<Entry<Person, String>> it = entrySet.iterator(); while (it.hasNext()) { Entry<Person, String> next = it.next(); Person key = next.getKey(); String value = next.getValue(); System.out.println(key + " = " + value); } }}class Person { private String name; private int age; Person() { } public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public int hashCode() { return this.name.hashCode() + age * 37; } @Override public boolean equals(Object obj) { if (obj instanceof Person) { Person p = (Person) obj; return this.name.equals(p.name) && this.age == p.age; } else { return false; } } @Override public String toString() { return "Person@name:" + this.name + " age:" + this.age; }}}
TreeMap
TreeMap的排序,TreeMap可以对集合中的键进行排序。如何实现键的排序?方式一:元素自身具备比较性和TreeSet一样原理,需要让存储在键位置的对象实现Comparable接口,重写compareTo方法,也就是让元素自身具备比较性,这种方式叫做元素的自然排序也叫做默认排序。方式二:容器具备比较性当元素自身不具备比较性,或者自身具备的比较性不是所需要的。那么此时可以让容器自身具备。需要定义一个类实现接口Comparator,重写compare方法,并将该接口的子类实例对象作为参数传递给TreeMap集合的构造方法。注意:当Comparable比较方式和Comparator比较方式同时存在时,以Comparator的比较方式为主;注意:在重写compareTo或者compare方法时,必须要明确比较的主要条件相等时要比较次要条件。(假设姓名和年龄一直的人为相同的人,如果想要对人按照年龄的大小来排序,如果年龄相同的人,需要如何处理?不能直接return 0,以为可能姓名不同(年龄相同姓名不同的人是不同的人)。此时就需要进行次要条件判断(需要判断姓名),只有姓名和年龄同时相等的才可以返回0.)通过return 0来判断唯一性。
import java.util.TreeMap;public class Demo4 { public static void main(String[] args) { TreeMap<String, Integer> tree = new TreeMap<String, Integer>(); tree.put("张三", 19); tree.put("李四", 20); tree.put("王五", 21); tree.put("赵六", 22); tree.put("周七", 23); tree.put("张三", 24); System.out.println(tree); System.out.println("张三".compareTo("李四"));//-2094 }}
自定义元素排序
package cn.itcast.gz.map;import java.util.Comparator;import java.util.Iterator;import java.util.Map.Entry;import java.util.Set;import java.util.TreeMap;public class Demo3 { public static void main(String[] args) { TreeMap<Person, String> hm = new TreeMap<Person, String>( new MyComparator()); hm.put(new Person("jack", 20), "1001"); hm.put(new Person("rose", 18), "1002"); hm.put(new Person("lucy", 19), "1003"); hm.put(new Person("hmm", 17), "1004"); hm.put(new Person("ll", 25), "1005"); System.out.println(hm); System.out.println(hm.put(new Person("rose", 18), "1006")); Set<Entry<Person, String>> entrySet = hm.entrySet(); Iterator<Entry<Person, String>> it = entrySet.iterator(); while (it.hasNext()) { Entry<Person, String> next = it.next(); Person key = next.getKey(); String value = next.getValue(); System.out.println(key + " = " + value); } }}class MyComparator implements Comparator<Person> { @Override public int compare(Person p1, Person p2) { if (p1.getAge() > p2.getAge()) { return -1; } else if (p1.getAge() < p2.getAge()) { return 1; } return p1.getName().compareTo(p2.getName()); }}class Person implements Comparable<Person> { private String name; private int age; Person() { } public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public int hashCode() { return this.name.hashCode() + age * 37; } @Override public boolean equals(Object obj) { if (obj instanceof Person) { Person p = (Person) obj; return this.name.equals(p.name) && this.age == p.age; } else { return false; } } @Override public String toString() { return "Person@name:" + this.name + " age:" + this.age; } @Override public int compareTo(Person p) { if (this.age > p.age) { return 1; } else if (this.age < p.age) { return -1; } return this.name.compareTo(p.name); }}
注意:Set的元素不可重复,Map的键不可重复,如果存入重复元素如何处理
Set元素重复元素不能存入add方法返回false
Map的重复健将覆盖旧键,将旧值返回。
2 0
- HashMap和TreeMap详解
- HashMap和TreeMap
- HashMap和TreeMap
- HashTable,HashMap和TreeMap
- HashMap和TreeMap
- hashmap和treemap
- HashMap和TreeMap
- HashMap和TreeMap区别
- HashMap和TreeMap区别
- hashmap和treemap比较
- TreeMap 和 HashMap 用法
- HashMap和TreeMap
- HashMap和TreeMap
- HashMap和TreeMap排序
- HashMap和TreeMap
- HashMap和TreeMap区别详解以及底层实现
- HashMap 和TreeMap的比较?
- HashMap Hashtable LinkedHashMap 和TreeMap
- 【Unity&3D射线检测】
- 调错笔记1
- jquery.cookie() 方法的使用(读取、写入、删除)
- 微软100题第18题(求出在这个圆圈中剩下的最后一个数字)
- Android内存分析工具
- HashMap和TreeMap详解
- oracle PROCEDURE
- AndroidStudio 配置SVN以及设置必要的忽略文件
- 底部导航Bottom navigation的简单使用
- java多线程
- 循环商品属性
- Oracle建立表空间和用户
- 【API设计风格—RESTful】:什么是RESTful(一)
- mac键盘符号含义