Map集合
来源:互联网 发布:mac imovie 丢失 编辑:程序博客网 时间:2024/05/22 23:28
1.添加
put(K key,V value);将指定键值关联存储到此映射;
putAll(Map< ? extends K,? extends V>m);从指定映射中将所有映射关系复制到此映射中;
2.删除
clear();移除所有;
remove(Object key);如果存在key,将其从其映射中移除;
3.判断
containsValue(Object value);如果此映射将一个或多个键映射到指定值,则返回 true
containsKey(Object key);如果此Map中包含指定key返回true;
4.获取
get(Object key);返回指定key的值,没有则返回null;
size();返回键值数;
values();返回此映射中包含的值的 Collection 视图;
entrySet();返回此映射中包含的映射关系的 Set 视图。
keySet();返回此映射中包含的键的 Set 视图
获取Map集合元素并打印方式一:
keySet():将map中所有的键存入到Set集合,因为Set具备迭代器,所以可以用迭代方式取出所有的键,在根据get方法,获取每个键对应的值;
import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.Set;public class MapDemo{public static void main(String[] args){Map<Integer,String> map = new HashMap<Integer,String>();method(map);}public static void method(Map<Integer,String> map){map.put(8, " 王五 ");map.put(2, " 赵六 ");map.put(7, " 小强 ");map.put(6, " 旺财 ");// 取出 map 中的所有元素。// 原理,通过 keySet 方法获取 map 中所有的键所在的 set 集合,在通过 set 的迭代器获取到每一个键。// 再对每一个键通过 map 集合的 get 方法获取其对应的值即可。Set<Integer> keySet = map.keySet();Iterator<Integer> it = keySet.iterator();while(it.hasNext()){Integer key = it.next();String value = map.get(key);System.out.println(key + ":" + value);}}}
获取Map集合元素并打印方式二:
entrySet():将map集合中的映射关系存入到set集合,而这个关系的数据类型是:Map.Entry,该类型其实是一个内部接口,即:Entry是Map接口中的一个内部类接口,常用方法:getKey();getValue();
import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.Set;public class MapDemo{ public static void main(String[] args){Map<Integer,String> map = new HashMap<Integer,String>();method(map); } public static void method(Map<Integer,String> map){map.put(8, "王五");map.put(2, "赵六");map.put(7, "小强");map.put(6, "旺财"); /* 通过Map转成Set就可以迭代。 找到了另一个方法,entrySet。 该方法将键和值的映射关系作为对象存储到了Set集合中,而这个映射关系的类型就是Map.Entry类型 */Set<Map.Entry<Integer,String>> entrySet = map.entrySet();Iterator<Map.Entry<Integer,String>> it = entrySet.iterator();while(it.hasNext()){Map.Entry<Integer,String> me = it.next();Integer key = me.getKey();String value = me.getValue();System. out.println(key + ":" + value);} }}
Map常用的子类:
|--Hashtable:内部结构是哈希表,是同步的。不允许null作为键,null作为值。
|--Properties:用来存储键值对型的配置文件的信息,可以和IO技术相结合。
|--HashMap:内部结构式哈希表,不是同步的。允许null作为键,null作为值。
|--TreeMap:内部结构式二叉树,不是同步的。可以对Map结合中的键进行排序。
Map扩展知识
Map集合被使用是因为具备映射关系。
import java.util.*;class MapDemo3{public static void main(String[] args) {HashMap<String,HashMap<String,String>> czbk = new HashMap<String,HashMap<String,String>>();HashMap<String,String> yure = new HashMap<String,String>();HashMap<String,String> jiuye = new HashMap<String,String>();czbk.put("yureban",yure);czbk.put("jiuyeban",jiuye);yure.put("01","zhangsan");yure.put("02","lishi");jiuye.put("01","wangwu");jiuye.put("02","zhaoliu");getStudentInfo(yure);}public static void getStudentInfo(HashMap<String,String> roomMap){Iterator<String> it = roomMap.keySet().iterator();while(it.hasNext()){String id = it.next();String name = roomMap.get(id);System.out.println(id+":"+name);}}
- Map 集合
- Map集合
- Map集合
- map集合
- Map集合
- map集合
- Map集合
- Map集合
- Map集合
- Map集合
- Map集合
- map集合
- Map集合
- Map集合
- Map集合
- Map集合
- Map集合
- Map集合
- STM32 adc
- Block与Block进阶
- shell的调试方法
- C语言之反序数组和求最大值和最小值
- 【android基础二】android重要控件——你有多熟悉listview?
- Map集合
- STM32F207(4) 上电关中断
- R和JSON的傻瓜式编程
- bzoj4305 数学
- bzoj 1690: [Usaco2007 Dec]奶牛的旅行
- 【spring模拟】IOC描述及模拟
- Java基础-实现文件搜索功能
- (二)H264语法及结构
- CentOS(6.5)安装lsb、查看系统版本信息