时间:2023-02-27 10:33:59 | 栏目:JAVA代码 | 点击:次
Interface Map k:键的类型;V:值的类型
将键映射到值的对象;不能包含重复的键;每个键可以映射到最多一个值
public static void main(String[] args) {
//创建Map集合对象
Map<String,String> m=new HashMap<String,String>();
//添加元素使用put方法,默认自然排序
m.put("02","李四");
m.put("04","赵六");
m.put("01","张三");
m.put("03","王五");
System.out.println(m);
}
}
三、Map集合的常用方法
| 方法名 | 说明 |
|---|---|
| V put(K key,V value) | 添加元素,添加重复键值元素会覆盖 |
| V remove(Object key) | 根据键删除键值对元素 |
| void clear() | 清除所有的键值对元素 |
| Boolean containsKey(Object key) | 判断集合是否包含指定的键,包含返回true |
| Boolean containsValue(Object value) | 判断集合是否包含指定的值,包含返回true |
| Boolean isEmpty() | 判断集合是否为空 |
| int size() | 获取集合的长度,也就是键值对的个数 |
public class MapDemo01 {
public static void main(String[] args) {
//创建Map集合对象
Map<String,String> m=new HashMap<String,String>();
//添加元素,put方法
m.put("1","张三");
m.put("2","李四");
m.put("3","王五");
m.put("4","赵六");
// System.out.println(m);
//根据键删除键值对元素
System.out.println(m.remove("3"));//切记键是什么类型就写什么类型,不然会返回null
System.out.println(m);
//清除所有键值对元素
m.clear();
//Boolean isEmpty()判断集合是否为空
System.out.println(m.isEmpty());
// System.out.println(m);
//Boolean containsKey(Object key);判断集合中是否包含指定的键
System.out.println(m.containsKey("5"));//切记键是什么类型就写什么类型,不然会返回null
//Boolean containsValue(Object value)判断集合是否包含指定的值,包含返回true
System.out.println(m.containsValue("张三"));
//int size()获取集合的长度,也就是键值对的个数
System.out.println(m.size());
}
}
?
| 方法名 | 说明 |
|---|---|
| V get(Object key) | 根据键获取值 |
| Set<K>keySet() | 获取所有键的集合 |
| Collection<V>values() | 获取所有值的集合 |
| Set<Map.Entry<K,V>>entrySet() | 获取所有键值对对象的集合 |
public class MapDemo02 {
public static void main(String[] args) {
//创建Map对象
Map<String,String> m=new HashMap<String,String>();
//添加元素
m.put("1","张三");
m.put("3","李四");
m.put("4","王五");
m.put("2","赵六");
// System.out.println(m);
//V get(Object key)根据键获取值
System.out.println(m.get("3"));//要注意键的类型,类型不对会报null
//Set<K>keySet()获取所有键的集合,因为返回的是个集合,所以用增强for遍历
Set<String> k=m.keySet();
for (String key:k){
System.out.println(key);
}
//Collection<V>values()获取所有值的集合,注意,他会按照键的排序对值进行排序
Collection<String> c=m.values();
for (String v:c){
System.out.println(v);
}
?
}
}
setKey()getValues方法public static void main(String[] args) {
//方式一
//创建Map集合对象
Map<String,String> m=new HashMap<String,String>();
//添加键值对
m.put("1","张三");
m.put("3","李四");
m.put("4","王五");
m.put("2","赵六");
//获取所有键的集合
Set<String>s=m.keySet();
//遍历
for (String key:s){
//再通过键获取相对应的值
String value=m.get(key);
System.out.println(key+","+value);
}
}
}
getKey()获取键
getValue()获取值
public static void main(String[] args) {
// //方式一
// //创建Map集合对象
// Map<String,String> m=new HashMap<String,String>();
// //添加键值对
// m.put("1","张三");
// m.put("3","李四");
// m.put("4","王五");
// m.put("2","赵六");
// //获取所有键的集合
// Set<String>s=m.keySet();
// //遍历
// for (String key:s){
// //再通过键获取相对应的值
// String value=m.get(key);
// System.out.println(key+","+value);
// }
//方式二
//创建Map集合对象
Map<String,String> m=new HashMap<String,String>();
//添加键值对
m.put("1","张三");
m.put("3","李四");
m.put("4","王五");
m.put("2","赵六");
//获取所有键值对的集合Set<Map.Entry<K,V>>entrySet()
Set<Map.Entry<String,String>> s= m.entrySet();
//遍历该集合
for (Map.Entry<String,String> ss:s){
//通过键值对对象获取键值
String key=ss.getKey();
//通过键值对对象获取值
String value=ss.getValue();
System.out.println(key+","+value);
?
}
}
}