当前位置:网站首页>Map接口
Map接口
2022-07-27 12:32:00 【涟涟涟涟】
关系

常用方法
1.put(K key, V value)
作用:
将键(key)/值(value)映射存放到Map集合中,简单点来说就相当于你输入的两个值key和values各是一个圆圈,然后他们被包围在一个大圆圈中,你调用的时候,如果没有调用其他的分离他们的方法,一般都是直接调用它们共同组成的大圆圈。
例:
package Hashmap;
import java.util.HashMap;
public class Test {
public static void main(String[] args) {
HashMap<String,Integer> hashmap=new HashMap<>();
hashmap.put("key", 35663);
System.out.println(hashmap.get("key"));
}
}
结果:
35663
2.get(Object key)
作用:
返回指定键key所映射的值,没有该key对应的值则返回 null。
注:只能是键。
例:
package Hashmap;
import java.util.HashMap;
public class Test {
public static void main(String[] args) {
HashMap<String,Integer> hashmap=new HashMap<>();
hashmap.put("key", 35663);
hashmap.put("srht", 32757);
hashmap.put("ht", 3465);
System.out.println(hashmap.get("key"));
}
}
结果:
35663
3.size()
作用:
返回Map集合中数据数量
例:
package Hashmap;
import java.util.HashMap;
public class Test {
public static void main(String[] args) {
HashMap<String,Integer> hashmap=new HashMap<>();
hashmap.put("key", 35663);
hashmap.put("srht", 32757);
hashmap.put("ht", 3465);
System.out.println(hashmap.size());
}
}
结果:
3
4.clear()
作用:
清空Map集合
例:
package Hashmap;
import java.util.HashMap;
public class Test {
public static void main(String[] args) {
HashMap<String,Integer> hashmap=new HashMap<>();
hashmap.put("key", 35663);
hashmap.put("srht", 32757);
hashmap.put("ht", 3465);
System.out.println(hashmap.size());
hashmap.clear();
System.out.println(hashmap.size());
}
}
结果:
3
0
5.isEmpty ()
作用:
判断Map集合中是否有数据,如果没有则返回true,否则返回false
例:
package Hashmap;
import java.util.HashMap;
public class Test {
public static void main(String[] args) {
HashMap<String,Integer> hashmap=new HashMap<>();
hashmap.put("key", 35663);
hashmap.put("srht", 32757);
hashmap.put("ht", 3465);
System.out.println(hashmap.isEmpty());
hashmap.clear();
System.out.println(hashmap.isEmpty());
}
}
结果:
false
true
6.remove(Object key)
作用:
删除Map集合中键的数据并返回其所对应value值。
例:
package Hashmap;
import java.util.HashMap;
public class Test {
public static void main(String[] args) {
HashMap<String,Integer> hashmap=new HashMap<>();
hashmap.put("key", 35663);
hashmap.put("srht", 32757);
hashmap.put("ht", 3465);
System.out.println(hashmap.remove("key"));
System.out.println(hashmap.size());
}
}
结果:
35663
2
7.values()
作用:
返回Map集合中所有value组成的以Collection数据类型格式数据。
例:
package Hashmap;
import java.util.HashMap;
public class Test {
public static void main(String[] args) {
HashMap<String,Integer> hashmap=new HashMap<>();
hashmap.put("key", 35663);
hashmap.put("srht", 32757);
hashmap.put("ht", 3465);
System.out.println(hashmap.remove("key"));
System.out.println(hashmap.values());
System.out.println(hashmap.size());
}
}
结果:
35663
[3465, 32757]
2
8.contains Key(Object key)
作用:
判断集合中是否包含指定键,包含返回 true,否则返回false
例:
package Hashmap;
import java.util.HashMap;
public class Test {
public static void main(String[] args) {
HashMap<String,Integer> hashmap=new HashMap<>();
hashmap.put("key", 35663);
hashmap.put("srht", 32757);
hashmap.put("ht", 3465);
System.out.println(hashmap.remove("key"));
System.out.println(hashmap.values());
System.out.println(hashmap.size());
System.out.println(hashmap.containsKey("dfshl"));
}
}
结果:
35663
[3465, 32757]
2
false
9.containsValue(Object value)
作用:
判断集合中是否包含指定值,包含返回 true,否则返回false
例:
package Hashmap;
import java.util.HashMap;
public class Test {
public static void main(String[] args) {
HashMap<String,Integer> hashmap=new HashMap<>();
hashmap.put("key", 35663);
hashmap.put("srht", 32757);
hashmap.put("ht", 3465);
System.out.println(hashmap.remove("key"));
System.out.println(hashmap.values());
System.out.println(hashmap.size());
System.out.println(hashmap.containsKey("dfshl"));
System.out.println(hashmap.containsValue(3465));
}
}
结果:
35663
[3465, 32757]
2
false
true
10.keySet()
作用:
返回Map集合中所有key组成的Set集合
例:
package Hashmap;
import java.util.HashMap;
public class Test {
public static void main(String[] args) {
HashMap<String,Integer> hashmap=new HashMap<>();
hashmap.put("key", 35663);
hashmap.put("srht", 32757);
hashmap.put("ht", 3465);
System.out.println(hashmap.remove("key"));
System.out.println(hashmap.values());
System.out.println(hashmap.size());
System.out.println(hashmap.containsKey("dfshl"));
System.out.println(hashmap.containsValue(3465));
System.out.println(hashmap.keySet());
}
}
结果:
35663
[3465, 32757]
2
false
true
[ht, srht]
11.entrySet()
作用:
将Map集合每个key-value转换为一个Entry对象并返回由所有的Entry对象组成的Set集合
例:
package Hashmap;
import java.util.HashMap;
public class Test {
public static void main(String[] args) {
HashMap<String,Integer> hashmap=new HashMap<>();
hashmap.put("key", 35663);
hashmap.put("srht", 32757);
hashmap.put("ht", 3465);
System.out.println(hashmap.entrySet());
}
}
结果:
[ht=3465, key=35663, srht=32757]
边栏推荐
- Openpyxl drawing radar map
- I/O实例操作
- 数据湖(二十):Flink兼容Iceberg目前不足和Iceberg与Hudi对比
- 虚拟偶像的歌声原来是这样生成的!
- Image segmentation vs Adobe Photoshop (PS)
- 开关量输入输出模块DAM-5055
- 广东财政多举措助力稳住粮食安全“压舱石”
- (10) STM32 - systick tick timer
- STS下载教程(include官网无法下载解决方案)
- The configuration change removed the routing filter, and the distributed router was overwhelmed: the Canadian network was paralyzed
猜你喜欢
随机推荐
JS string method summary
快抖抢救“失意人”
数据湖(二十):Flink兼容Iceberg目前不足和Iceberg与Hudi对比
POJ1988_Cube Stacking
[网摘][医学影像] 常用的DICOM缩略图解释以及Viewer converter 转换工具
关于离线缓存Application Cache /使用 manifest文件缓存
@Postconstruct annotations and initializingbean perform some initialization operations after bean instantiation
Julia beginner tutorial 2022
In the first half of the year, the number of fires decreased by 27.7%. Guangdong will improve the fire safety quality of the whole people in this way
JVM memory model
Will MySQL fail to insert data? Why?
Switching value input and output module dam-5055
Unity Shader 一 激光特效Shader[通俗易懂]
Configuration files in MySQL
[excerpt] [medical image] common DICOM thumbnail interpretation and viewer converter conversion tool
广东财政多举措助力稳住粮食安全“压舱石”
MySQL paging query instance_ MySQL paging query example explanation "suggestions collection"
Check the number of file descriptors opened by each process under the system
Top 10 in the 5.3 billion Bi Market: fansoft, Microsoft, Yonghong, sap, Baidu, IBM, SAS, smart, salesforce, Inspur soft
No matching distribution found for flask_ A solution to compat








