当前位置:网站首页>Map接口及其子实现类
Map接口及其子实现类
2022-06-23 07:46:00 【Liang FuFu】
1. Map接口
1.(1)Map<K,V>集合:能够描述现实世界事物的实体类
(2)将键映射到值的对象,表示一系列键值对元素
(3)Map针对键有效,跟值无关,值可以重复,但是键必须是唯一的
2.子实现类:
(1)HashMap:基于哈希表实现Map接口
特点:允许null值和null键存在,线程不安全的类,不能保证迭代顺序永久不变
(2)TreeMap:底层是一个红黑树结构
1.1 Map的基本功能
1. 获取长度:
public int size():获取集合元素数
添加功能:
public V put(K key,V value):添加键值对元素,有返回值:如果key不重复,返回值为null,通过返回值是否为null判断是否重复添加.以来Object的hashCode()和equals()方法
删除功能:
public V remove(Object key):删除指定的键,并返回删除的值
public void clear():暴力删除,清空所有键值对对象
判断功能:
public boolean containsKey(Object key):判断是否包含指定键
public boolean containsValue(Object value):判断是否包含指定的值
public boolean isEmpty():判断集合是否为空
1.2 Map集合的遍历方式
1.方式一:获取所有键的集合
public Set<K> keyset():将所有的键获取
public V get (Object key):获取键所对应的值
2.实现代码:
public class MapDemo{
public static void main(String[] args){
Map<String,String> map = new HashMap<>();
map.put("杨过","小龙女");
map.put("郭靖","黄蓉");
map.put("尔康","紫薇") ;
map.put("陈玄风","梅超风") ;
map.put("贾宝玉" ,"林黛玉") ;
//获取所有键的集合
Set<String> setKey = map.keySet();
//获取所有键,遍历Set集合
for(String key:setKet){
String value = map.get(key);
sout(key+"\t"+value);
}
}
}
3.方式二:获取所有键值对对象
Set<Map.Entry<K,V>> entrySet();获取所有键值对对象,一个键值对对象称为映射项
使用接口中的方法:
K getKey():获取键
V getValue():获取值
4.实现代码:
public class MapDemo{
public static void main(String[] args){
Map<String,String> map = new HashMap<>();
map.put("杨过","小龙女") ;
map.put("郭靖","黄蓉") ;
map.put("尔康","紫薇") ;
map.put("陈玄风","梅超风") ;
map.put("贾宝玉" ,"林黛玉") ;
map.put("令狐冲" ,"任盈盈")
Set<Map.Entry<String,String>> entry = map.entrySet();
for(Map.Entry<String,String> en:entry){
String key = en.getKey();
String value = en.getValue();
sout(key+"\t"+value);
}
}
}
2.HashMap集合
1.如果针对键是jdk提供的类型,不需要重写hashCode()和equals()
自定义类型需要重写这两种方法
3.TreeMap集合
两种排序方式:
1.实现Comparabele接口,重写其中的compareTo方法
2.实现Comparator接口,重写compare方法
边栏推荐
- 看了5本书,我总结出财富自由的这些理论
- @What is the difference between controller and @restcontroller?
- Leetcode 173 Binary search tree iterator (2022.06.22)
- 2. probability theory - axiom of probability theory
- 浅谈ThreadLocal和InheritableThreadLocal,源码解析
- 浅析 Open API 设计规范
- Google common syntax
- ArcMap batch delete points closer
- 5本财富自由好书的精华
- 船长阿布的灵魂拷问
猜你喜欢

深度学习------不同方法实现vgg16

11 string function

vtk.js鼠标左键滑动改变窗位和窗宽

Gif verification code analysis

Playwirght深度入门

vtk. JS left mouse button sliding to change window level and window width

Captain Abu's soul torture

Openvino series 19 Openvino and paddleocr for real-time video OCR processing

openni. utils. OpenNIError: (OniStatus.ONI_STATUS_ERROR, b‘DeviceOpen using default: no devices found‘

SQL注入常用到的绕过方式-ctf
随机推荐
Talk about routing design in service governance
建立一有序的顺序表,并实现下列操作: 1.把元素x插入表中并保持有序; 2.查找值为x的元素,若找到将其删除; 3.输出表中各元素的值。
View the file once a second and send the result of the last line of the file to the syslog server
What are open source software, free software, copyleft and CC? Can't you tell them clearly?
Regular expression use cases
深度学习------不同方法实现lenet-5模型
Two bug fixes in aquatone tool
socket编程——select模型
Take you to tiktok. That's it
Kwai 350014
google常用语法
Captain Abu's soul torture
MFC radio button grouping
Guava cache usage summary
Vulnhub | DC: 4 |【实战】
忽略超长参数违规
Acwing第 56 場周賽【完結】
Learn to draw Er graph in an article
aquatone工具 中的2个bug修复
5本财富自由好书的精华