①. Map是双列集合与Collection的单列集合并列存在
②. Map
这里的K 、V 都应该是引用数据类型
③. HashSet底层依赖HashMap,单列底层依赖双列集合
①. 添加功能:如果键是第一次储存,就直接储存元素,返回null;如果键不是第一次存在,就用值把以前的值替换掉,返回以前的值
V put(K key ,V value):添加元素,返回的是以前的值
"prettyprint hljs go" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">Map map=new HashMap<>();
- Integer i1=map.put("张三",23);
- Integer i2=map.put("李四",23);
- Integer i3=map.put("王五",24);
- Integer i4=map.put("赵六",25);
- Integer i5=map.put("张三",26);//相同的键不储存,把原来的值覆盖 把被覆盖的值返回
-
- System.out.println(map);//{李四=23, 张三=26, 王五=24, 赵六=25}
-
- //这里的i1--i4 都是null是因为开始的时候[张三,23][李四,23]....
- // 在集合中没有,这样把原来的值覆盖了,返回的是原来的值
- System.out.println(i1);//null
- System.out.println(i2);//null
- System.out.println(i3);//null
- System.out.println(i4);//null
- System.out.println(i5);//23
"prettyprint hljs go" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">Map map=new HashMap<>();
- map.put("张三",23);
- map.put("李四",23);
- map.put("王五",24);
- map.put("赵六",25);
- //根据键删除元素,返回键对应的值
- Integer value=map.remove("张三");
- System.out.println(value);//23
"prettyprint hljs go" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">//判断是否包含传入的键
- System.out.println(map.containsKey("张三"));//true
- //判断是否包含传入的值
- System.out.println(map.containsValue(23));//true
④. 长度功能
int size():返回集合中的键值对的个数
"prettyprint hljs go" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">Map map=new HashMap<>();
- map.put("张三",26);
- map.put("李四",23);
- map.put("王五",24);
- map.put("赵六",25);
-
- Collection
coll=map.values(); - System.out.println(coll);//[23, 23, 24, 25]
- System.out.println(map.size());//4
V get(Object key):根据键获取值
Set
keySet():获取集合中所有的键集合
Collection
values:获取集合中所有值集合
Set
K getKey():得到entrySet中的键
V getValue():得到entrySet中的值
class="prettyprint hljs go" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">Map<String, Integer> map = new HashMap<>();
- map.put("张三", 23);
- map.put("李四", 24);
- map.put("王五", 25);
- map.put("赵六", 23);
class="prettyprint hljs swift" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">//V get(Object key):根据键获取值
- Integer i = map.get("张三");//26
- Integer i2=map.get("小智");//没有的话返回null
-
- //1.获取所有的键
- Set
keySet = map.keySet(); - //iterator遍历
- Iterator
iterator = keySet.iterator(); - while (iterator.hasNext()) {
- String key = iterator.next();//获取每一个键
- Integer value = map.get(key); //根据键获取值
- System.out.println(key + "=" + value);
- }
- System.out.println("--------------");
- //使用增强for循环
- for (String key : map.keySet()) {
- Intger value=map.get(key);
- System.out.println(key + "=" +value );
- }
class="prettyprint hljs java" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">interface Inter{
- interface Inter2{
- public void show();
- }
- }
- //这里的Inter.Inter2 和Map.Entry一样的
- class Demo implements Inter.Inter2{
-
- @Override
- public void show() {
-
- }
- }
class="prettyprint hljs dart" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">//2.根据键值对对象,获取键和值
- //Map.Entry说明Entry是Map的内部接口,将键和值封装成了Entry对象,并储存在Set集合中
- Set<Map.Entry<String,Integer>>entrySet=map.entrySet();
- //获取每一个对象
- Iterator<Map.Entry<String,Integer>>it=entrySet.iterator();
- while(it.hasNext()){
- //获取每一个Entry对象
- //static class Entry
implements Map.Entry{} - Map.Entry<String,Integer>en=it.next();//父类引用指向子类对象
- //Entry
en=it.next(); 子类对象 - //根据键值对对象获取键
- String key=en.getKey();
- //根据键值对对象获取值
- Integer value=en.getValue();
- System.out.println(key+"="+value);
- }
-
- //增强for循环
- for(Map.Entry<String,Integer> en:map.entrySet()){
- System.out.println(en.getKey()+"="+en.getValue());
- }
- for(Entry<String,Integer> en:map.entrySet()){
- System.out.println(en.getKey()+"="+en.getValue());
- }
①. Map中的key用Set来存放,不允许重复,即同一个Map对象所对应的类,须重写hashCode( )和equals( )方法
②. HashMap判断两个key相等的标准是:两个key通过equals()方法返回 true,hashCode值也相等
③. HashMap判断两个value相等的标准是:两个value通过 equals() 方法返回true
④. 注意:两个HashMap的hashcode相同,则他们的equal()方法不一定相同,若两个HashMap的equal()相同,则他们的hashcode一定相同
⑤. 允许使用null键和null值,与HashSet一样,不保证映射的顺序
class="prettyprint hljs java" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">// HashMap集合键是Student值是String的案例
- public class Student {
- private String name;
- private int age;
-
- public Student() {
- }
-
- public Student(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 boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
- Student student = (Student) o;
- return age == student.age &&
- Objects.equals(name, student.name);
- }
-
- @Override
- public int hashCode() {
- return Objects.hash(name, age);
- }
-
- @Override
- public String toString() {
- return "Student{" +
- "name='" + name + '\'' +
- ", age=" + age +
- '}';
- }
- }
class="prettyprint hljs go" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">HashMap<Student,String>hm=new HashMap<>();
- hm.put(new Student("张三",23),"北京");
- String i=hm.put(new Student("张三",23),"上海");
- hm.put(new Student("李四",24),"广州");
- hm.put(new Student("王五",25),"深圳");
- System.out.println(i);
- //没有重写equals方法之前,调用的是Object类的,会比较地址值
- //没有重写equals方法和 hashCode方法的时候map中的元素有4个,重写后只有3个
- //注意这里的张三对应的值是上海,因为把北京覆盖了
- System.out.println(hm);//北京
- //{Student{name='张三', age=23}=上海, Student{name='李四', age=24}=广州, Student{name='王五',
class="prettyprint hljs go" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">LinkedHashMap<String,Integer>lhm=new LinkedHashMap<>();
- lhm.put("张三",23);
- lhm.put("李四",24);
- lhm.put("王五",25);
- lhm.put("赵六",26);
- System.out.println(lhm);//{张三=23, 李四=24, 王五=25, 赵六=26}
①. TreeMap和TreeSet一样
②. 自然排序:TreeMap 的所有的Key必须实现Comparable 接口,调用对象的comparaTo()方法和集合中的对象进行比较,根据CompableTo()方法返回的结果进行储存(所有的 Key 应该是同一个类的对象,否则将会抛出 ClasssCastException)
③. 选择器排序:创建TreeMap时,传入一个Comparator对象,该对象负责对TreeMap中的所有 key进行排序。此时不需要Map的Key实现Comparable接口
④. TreeMap集合键是Student值是String的案例
class="prettyprint hljs java" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">//自然排序
- public class Student implements Comparable<Student> {
- private String name;
- private int age;
-
- public Student() {
- }
-
- public Student(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 String toString() {
- return "Student{" +
- "name='" + name + '\'' +
- ", age=" + age +
- '}';
- }
-
- @Override
- public int compareTo(Student s) {
- //以年龄为主要条件,姓名为次要条件
- int num=this.age-s.age;
- return num==0?this.name.compareTo(s.name):num;
- }
- }
class="prettyprint hljs tcl" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">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);
- //{Student{name='李四', age=13}=上海, Student{name='张三', age=23}=北京,
- // Student{name='王五', age=33}=广州, Student{name='赵六', age=43}=北京}
class="prettyprint hljs dart" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">//选择器排序
- TreeMap
tm=new TreeMap<>(new Comparator() { - @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("王五",33),"广州");
- tm.put(new Student("赵六",43),"北京");
- System.out.println(tm);
- //{Student{name='张三', age=23}=北京, Student{name='李四', age=13}=上海,
- //Student{name='王五', age=33}=广州, Student{name='赵六', age=43}=北京}
class="prettyprint hljs yaml" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">HashMap和Hashtable的区别
- 共同点:
- 底层都是哈希算法,都是双列集合
- 区别
- 1.HashMap是线程不安全的,效率高,jdk1.2版本
- Hashtable是现场安全的,效率低,jdk1.0版本
- 2.HashMap可以存储null键和null值
- Hashtable不可以存储null键和null值