Map集合

在现实生活中,有非常多的东西,是和另外一种东西对应的,并且还是唯一的,比如:身份证号与个人,个人与手机,一夫一妻。。。等,这种关系就是对应关系,又叫做映射。Java为这种数据类型提供了专门的接口,就是Map<K,V>接口。

Map<K,V>集合的特点

  • Map<K,V>集合是一个双列集合,一个元素包含两个值。
  • Map<K,V>集合中的元素,key和value的数据类型可以相同,也可以不相同。
  • Map<K,V>集合中的元素,key是不允许重复的,但是value可以重复。
  • Map<K,V>集合中的元素,key和value是一一对应的。

Map<K,V>集合与Collection的区别

  • Collection中的集合,元素是孤立存在的,而Map<K,V>集合中的元素是成对出现的。
  • Collection集合称为单列集合,Map<K,V>集合称为双列集合。

Map<K,V>的常用方法

Map<K,V>中定义了很多方法,都是它的子类常用的方法,只要会用这几个方法,Map集合就等于会了,只用再了解一下,每一个子类的做用就行了。

方法

  • public V put(K key,V value); 把指定的键与指定折值添加到Map集合中。
  • public V remove(Object key); 把指定的键所对应的键值对元素在Map集合中删除,返回被删除元素的值。
  • public V get(Object key); 根据指定的键,在Map集合中取对应的值。
  • public Set<K> keySet(); 获取Map集合中所有的键,存到Set集合中去。
  • public Set<Map.Entry<K,V>> entrySet(); 获取Map集合中所有的键值对对象的集合。

使用

public V put(K key,V value); 把指定的键与指定折值添加到Map集合中。

    Map<Integer, String> map = new HashMap<>();
map.put(1, "a");
map.put(2, "b");
map.put(3, "c");
map.put(4, "d");
//只可以存储一个null键
map.put(null, "e");
//Map集合是无序的
System.out.println(map); //{null=e, 1=a, 2=b, 3=c, 4=d}

public V remove(Object key); 把指定的键所对应的键值对元素在Map集合中删除,返回被删除元素的值。

   //数据为上面的数据
//删除key为null的元素
String remove = map.remove(null);
//返回值是被删除的元素的value
System.out.println(remove); //e
System.out.println(map); //{1=a, 2=b, 3=c, 4=d}

public V get(Object key); 根据指定的键,在Map集合中取对应的值。

   String s = map.get(2);
System.out.println(s); //b
System.out.println(map); //{null=e, 1=a, 2=b, 3=c, 4=d}

public Set<K> keySet(); 获取Map集合中所有的键,存到Set集合中去。

   //取出Map集合中所有的key
Set<Integer> integers = map.keySet();
//对Map集合进行遍厉的时候才使用的
for (Integer integer :integers) {
//用get方法通过key来获取值
String s = map.get(integer);
System.out.println(s);
}
//也可以使用Set集合里面的迭代器来进行循环

public Set<Map.Entry<K,V>> entrySet(); 获取Map集合中所有的键值对对象的集合。

    //这也是一种遍厉Map集合的方法
Set<Map.Entry<Integer, String>> entries = map.entrySet();
for (Map.Entry<Integer, String> entry : entries) {
//用Map.Entry里面的getKey和getValue方法来获取key和value
System.out.print("key:" + entry.getKey());
System.out.println("value:" + entry.getValue());
/*
key:nullvalue:e
key:1value:a
key:2value:b
key:3value:c
key:4value:d
*/
}

Entry<K,V>Map接口,中是一个内部接口,并且在所有的实现类中实现,这个实现类的做用是用来记录Map集合中所有的Key和对应的Value

HashMap<K,V>集合

存储数据用的是哈希表结构,元素存取是无序。因为要保证唯一,所以必须要保证,key的对象必须要重写hashCode()方法和equals()方法。


因为基本数据类型里面都重写了这两个方法,所以我自定义一个对象做为key值,看看重写了这两个方法和不重写了这两个方法的区别。

重写:

    Map<Student, String> stuMap = new HashMap<>();
stuMap.put(new Student("张三", 18), "不重写");
stuMap.put(new Student("张三", 18), "不重写");
stuMap.put(new Student("王五", 13), "不重写");
stuMap.put(new Student("赵六", 15), "不重写");
System.out.println(stuMap);//四个数据全有
//{Student{name='王五', age=13}=不重写, Student{name='张三', age=18}=不重写,
// Student{name='张三', age=18}=不重写, Student{name='赵六', age=15}=不重写}

不重写:

    Map<Student, String> stuMap = new HashMap<>();
stuMap.put(new Student("张三", 18), "重写");
stuMap.put(new Student("张三", 18), "重写");
stuMap.put(new Student("王五", 13), "重写");
stuMap.put(new Student("赵六", 15), "重写");
System.out.println(stuMap);//重复的张三没有了
//{Student{name='张三', age=18}=不重写, Student{name='赵六', age=15}=不重写,
// Student{name='王五', age=13}=不重写}

上面的重复虽然保证唯一了,但是没有保证有序,所以我们就要用到LinkedHashMap集合。

LinkedHashMap<K,V>集合

LinkedHashMapHashMap下面的子类,HashMap的所有的方法,LinkedHashMap都有,但是LinkedHashMapHashMap好用的一点是,它可以保证存取的顺序一致。

    Map<Student, String> stuMap = new LinkedHashMap<>();
stuMap.put(new Student("张三", 18), "重写");
stuMap.put(new Student("张三", 18), "重写");
stuMap.put(new Student("王五", 13), "重写");
stuMap.put(new Student("赵六", 15), "重写");
System.out.println(stuMap);//顺序和存的顺序一样了
//{Student{name='张三', age=18}=重写, Student{name='王五', age=13}=重写,
//Student{name='赵六', age=15}=重写}

上面的这两个实现类都是同步的!


细节决定成败!

个人愚见,如有不对,恳请扶正!

最新文章

  1. Java中,包的概念、常量、静态成员、继承
  2. 深入浅出RxJava
  3. Add Digits, Maximum Depth of BinaryTree, Search for a Range, Single Number,Find the Difference
  4. jenkins打包成功,部署失败
  5. mysql中set autocommit=0与start transaction区别
  6. Odoo constraints 使用教程
  7. 启动 Eclipse 弹出“Failed to load the JNI shared library jvm.dll”错误的解决方法!&amp;&amp;在eclipse.ini中为eclipse指定jdk启动
  8. F5中源地址转换(AutoMap)模式下后端服务器获取客户端真正的IP地址
  9. pagination分页插件
  10. 在非MFC程序中使用调试宏 ASSERT(),VERIFY()和 TRACE()
  11. Spring MVC RedirectAttributes的用法解决办法
  12. python实战===生成随机数
  13. Spring Boot Document Part II(上)
  14. 如何高效的学习 TensorFlow 代码?
  15. F - Communication System
  16. 单击Gridview中LinkButton,获取当前行索引及某单元格值,进行相关处理
  17. MYSQL 解决中文字符集乱码问题的方法
  18. 图床神器:七牛云 + Mpic + FScapture
  19. swagger配置和简单使用
  20. 7.9 Models -- Connection to An HTTP Server

热门文章

  1. Elasticsearch快速入门和环境搭建
  2. Zabbix企业分布式监控工具
  3. GO语言复合类型05---递归
  4. Go语言的函数03---返回值
  5. 高并发Flask服务部署
  6. MinkowskiBroadcast广播
  7. 如何查看app启动的activity
  8. oracle审计表迁移
  9. 狂神说Linux笔记:Vim和账号、用户组、磁盘管理
  10. Centos Yum 安装 Mysql 5.7