Map 接口有哪些类
Map接口
Map提供了一种映射关系,其中的元素是以键值对(key-value)的形式存储的,能够实现根据key快速查找value;
Map中的键值对以Entry类型的对象实例形式存在;
建(key值)不可重复,value值可以重复,一个value值可以和很多key值形成对应关系,每个建最多只能映射到一个值。
Map支持泛型,形式如:Map<K,V>
Map中使用put(K key,V value)方法添加
Map接口中定义的常用方法
具体使用在实现类中讨论
int size();//获取Map集合大小(即元素数量)
boolean isEmpty();//判断是否为空
boolean containsKey(Object key);//判断是否包含某个键
boolean containsValue(Object value);//判断是否包含某个值
V get(Object key);//获取某个键对应的值
V put(K key, V value);//添加键值对(K,V)
V remove(Object key);//移除某个键对应的键值对
void putAll(Map<? extends K, ? extends V> m);//添加另一个Map集合
void clear();//清空所有键值对
Set<K> keySet();//获取键的集合
Collection<V> values();//获取值的集合
Set<Map.Entry<K, V>> entrySet();//获取键值对实体的集合
interface Entry<K,V>//Map中的内部接口
HashMap
基于哈希表的 Map 接口的实现。此实现提供所有可选的映射操作,并允许使用 null 值和 null 键。(除了非同步和允许使用 null 之外,HashMap 类与 Hashtable 大致相同。)除实现了Map接口外还实现了Cloneable,Serializable,继承了AbstractMap抽象类
此类不保证映射的顺序,特别是它不保证该顺序恒久不变。
特点:
键无序,唯一,类似于Set集合
值有序,可重复,类似于List
底层数据结构是哈希表,保证键唯一
允许键为null,值为null
// HashMap<String, Student> hm = new HashMap<String, Student>();
// hm.put("2018050401", new Student("2018050401", "张三", 18, 80.0));
// hm.put("2018050402", new Student("2018050402", "李四", 18, 80.0));
// hm.put("2018050403", new Student("2018050403", "李四", 18, 80.0));
// hm.put("2018050404", new Student("2018050404", "王五", 18, 80.0));
// hm.put("2018050404", new Student("2018050404", "王五", 18, 80.0));
//
// // 方式一: 通过键找值
// Set<String> keys = hm.keySet();
// for (String key : keys) {
// Student s = hm.get(key);
// System.out.println(key + "|" + s.getId() + "|" + s.getName() + "|" + s.getAge() + "|" + s.getScore());
// }
HashMap<Student, String> hm = new HashMap<Student, String>();
hm.put(new Student("2018050401", "张三", 18, 80.0),"2018050401");
hm.put(new Student("2018050402", "李四", 18, 80.0),"2018050402");
hm.put(new Student("2018050403", "李四", 18, 80.0), "2018050403");
hm.put(new Student("2018050404", "王五", 18, 80.0), "2018050404");
hm.put(new Student("2018050404", "王五", 18, 80.0), "2018050404");
// 方式二: 通过键值对对象找键找值
Set<Entry<Student, String>> keyValues = hm.entrySet();
for (Entry<Student, String> keyValue : keyValues) {
Student s = keyValue.getKey();
String value = keyValue.getValue();
System.out.println(s.getId() + "|" + s.getName() + "|" + s.getAge() + "|" + s.getScore() + "=" + value);
}
LinkedHashMap
Map 接口的哈希表和链表实现,具有可预知的迭代顺序
特点:
键有序,唯一,
值有序,可重复,类似于List
底层数据结构是哈希表和链表,哈希表保证键唯一,链表保证键有序
LinkedHashMap<Integer, String> lhm = new LinkedHashMap<Integer, String>();
lhm.put(01, "张三1");
lhm.put(02, "张三2");
lhm.put(03, "张三3");
lhm.put(04, "张三4");
lhm.put(05, "张三5");
Set<Integer> keys = lhm.keySet();
for (Integer key : keys) {
System.out.println(key + "|" + lhm.get(key));
}
TreeMap
基于红黑树(Red-Black tree)的 NavigableMap 实现。该映射根据其键的自然顺序进行排序,或者根据创建映射时提供的 Comparator 进行排序,
具体取决于使用的构造方法。
特点:
键可排序,唯一,
值有序,可重复,类似于List
底层数据结构是自平衡的二叉树,可排序
排序方式类似于TreeSet,分为自然排序和比较器排序,具体取决于使用的构造方法
TreeMap<Integer, String> tm = new TreeMap<Integer, String>();
tm.put(24, "Hello1");
tm.put(14, "Hello2");
tm.put(34, "Hello3");
tm.put(124, "Hello4");
tm.put(24, "Hello5");
tm.put(24, "Hello6");
tm.put(24, "Hello7");
tm.put(244, "Hello8");
tm.put(624, "Hello9");
tm.put(24, "Hello10");
Set<Integer> keys = tm.keySet();
for (Integer key : keys) {
String value = tm.get(key);
System.out.println(key + "|" + value);
}
HashTable
此类实现一个哈希表,该哈希表将键映射到相应的值。任何非 null 对象都可以用作键或值
特点:
不允许null键和null值
线程安全,效率低
HashMap和Hashtable的区别:
HashMap是不安全的不同步的效率高的 允许null键和null值
Hashtable是安全的同步的效率低的 不允许null键和null值
底层都是哈希表结构
Hashtable<String, String> hashtable = new Hashtable<String, String>();
hashtable.put("刘备", "孙尚香");
hashtable.put("孙策", "大乔");
hashtable.put("周瑜", "小乔");
hashtable.put("吕布", "貂蝉");
System.out.println(hashtable);
Enumeration<String> keys = hashtable.keys();
while (keys.hasMoreElements()) {
String key = keys.nextElement();
String value = hashtable.get(key);
System.out.println(key + "|" + value);
}
WeakHashMap
以弱键 实现的基于哈希表的 Map。在 WeakHashMap 中,当某个键不再正常使用时,将自动移除其条目。更精确地说,对于一个给定的键,其映射的存在并不阻止垃圾回收器对该键的丢弃,这就使该键成为可终止的,被终止,然后被回收。
丢弃某个键时,其条目从映射中有效地移除,因此,该类的行为与其他的 Map 实现有所不同。
WeakHashMap<String,String> whm = new WeakHashMap<>();
whm.put(new String("hello1"), "world1");
whm.put(new String("hello2"), "world2");
whm.put(new String("hello3"), "world3");
whm.put("hello4", "world3");
System.out.println(whm);
System.gc();
System.runFinalization();
System.out.println(whm);
EnumMap
键是枚举类型
EnumMap<Direction, String> em = new EnumMap<>(Direction.class);
em.put(Direction.UP, "向上移动");
em.put(Direction.DOWN, "向下移动");
em.put(Direction.LEFT, "向左移动");
em.put(Direction.RIGHT, "向右移动");
Set<Direction> keys = em.keySet();
for (Direction key : keys) {
String value = em.get(key);
System.out.println(key + "|" + value);
}
Map 接口有哪些类的更多相关文章
- Java基础 - Map接口的实现类 : HashedMap / LinkedHashMap /TreeMap 的构造/修改/遍历/ 集合视图方法/双向迭代输出
Map笔记: import java.util.*; /**一:Collection接口的 * Map接口: HashMap(主要实现类) : HashedMap / LinkedHashMap /T ...
- Java基础Map接口+Collections工具类
1.Map中我们主要讲两个接口 HashMap 与 LinkedHashMap (1)其中LinkedHashMap是有序的 怎么存怎么取出来 我们讲一下Map的增删改查功能: /* * Ma ...
- Map接口、HashMap类、LinkedHashSet类
java.util.Map<K, V>接口 双列集合,key不可以重复 Map方法: 1.public V put(K key, V value):键值对添加到map,如果key不重复返回 ...
- Map接口常用实现类学习
HashMap 1.6的HashMap:数组加单向链表结构 最重要的内部类Entry,全类名是java.util.HashMap.Entry,是个静态类,实现了Map.Entry接口.HashMap. ...
- 双列集合Map接口 & Collections工具类
HashMap 常用方法 遍历方式 iterator迭代器 ITIT HashTable 继承字典 Hashtable--Properties 文件读写 总结 Collections工具类
- LinkedHashSet、Map、Map接口HashMap、Hashtable,TreeSet、TreeMap、如何选择使用集合实现类,Collections工具类
一.Set接口实现类LinkedHashSet 实现继承图: 1.LinkedHashSet的全面说明 1) LinkedHashSet是 HashSet的子类 2) LinkedHashSet底层是 ...
- 集合之六:Map接口
Map接口概述 Map接口中的集合和Collection接口中的集合在存储数据的格式上有很大的不同,Map接口下的内容是以<K , V> ,键值对的形式存储的,我们查询API,Map接口的 ...
- Java容器之Map接口
Map 接口: 1. 实现 Map 接口的类是用来存储键-值(key-value)对: 2. Map 接口的实现类有 HashMap 和 TreeMap 等: 3. Map 类中存储的键-值对,通过键 ...
- 自顶向下理解Java集合框架(三)Map接口
Map基本概念 数据结构中Map是一种重要的形式.Map接口定义的是查询表,或称查找表,其用于储存所谓的键/值对(key-value pair),其中key是映射表的索引. JDK结构中还存在实现Ma ...
随机推荐
- css重置的各种版本总结
个人手机端常用到的: @charset "utf-8"; body, h1, h2, h3, h4, h5, h6, hr, p, blockquote, dl, dt, dd, ...
- Self-Driving Database
最近一直在做 ML in Database 相关的工作.偶然发现CMU 19spring的15-721课程竟然专门安排了这个专题,不禁欣喜若狂,赶紧去学习了一下. Andy提出了self-drivin ...
- java的idea项目文件夹合并,怎么分开
一,如下图所示 二,只需要将这个去掉
- TCP/IP四层协议
1.数据链路层 数据链路层实现了网卡接口的网络驱动程序,处理数据在物理媒介(以太网,令牌环)上的传输,常用协议包含ARP(地址解析协议),RARP(逆地址解析协议)两个协议,他们实现了IP地址和物理地 ...
- UEditor使用报错Cannot set property 'innerHTML' of undefined
仿用UEditor的setContent的时候报错,报错代码如下Uncaught TypeError: Cannot set property ‘innerHTML’ of undefined.调试u ...
- 用R语言提取数据框中日期对应年份(列表转矩阵)
用R语言提取数据框中日期对应年份(列表转矩阵) 在数据处理中常会遇到要对数据框中的时间做聚类处理,如从"%m/%d/%Y"中提取年份. 对应操作为:拆分成列表——列表转矩阵——利用 ...
- poj 3714 Raid(平面最近点对)
Raid Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 7473 Accepted: 2221 Description ...
- 对table最后一行显示与隐藏
//显示table最后一行,如果用block的话,可能会影响到页面的样式 $("#table tr").get($("#table tr").length - ...
- pdf幻灯片:圆锥曲线中的“三定”问题探究(一)
预留的广告位! 下载该pdf文件,然后在adobe reader 的"视图"中使用"全屏模式"播放该幻灯片 #include <iostream> ...
- pandas、matplotlib、Numpy模块的简单学习
目录 一.pandas模块 二.matplotlib模块 1.条形图 2. 直方图 3.折线图 4.散点图+直线图 三.numpy 一.pandas模块 pandas是BSD许可的开源库,为Pytho ...