16、Map-Hashmap
来源:互联网 发布:it人才缺口 编辑:程序博客网 时间:2024/06/10 03:01
一、Map集合:
1、以键值对形式存在的集合Map<K,V>
2、key不可重复的,Value可以重复
3、Key和Value可以允许为null。
Map接口有哪些常用的实现类?
Map
AbastractMap
HashMap
LinkedHashMap
TreeMap
WeakHashMap
二、HashMap
1、定义HashMap
Map<K,V> map=new HashMap<K,V>();
构造方法:初始容量16,最大值为2的30次方,默认加载因子0.75。
这两个参数是影响HashMap性能的重要因素。
2、常用方法:
put(k,v) 添加一个元素
get(k) 通过key获取Value
remove(k) 通过key删除对应的键值对,返回值为Value
size() map的长度
containsKey(K) 判断该K是否在map集合中,如何判断一个map中是否存在某个键 不能使用get(key)。
containsValue(V) 判断该V是否在map集合中。
putAll(map) 将一个map集合copy给两一个Map集合。 需要赋值的map.putAll(有值的map)。
clear() 清空一个Map集合
isEmpty() 判断一个map集合是否有键值对。
values() 获取map集合的所有Value值,返回类型是Collection
3、如何循环遍历(4种方法)
a、keySet()+for循环
b、keySet()+Iterator
c、entrySet()+for循环
d、entrySet()+Iterator
基本代码:
public class HashMapTest {public static void main(String[] args) {Map<String, String> map = new HashMap<>();//1、增加map.put("YG20160101", "岳");map.put("YG21060102", "阎");map.put("YG21060103", "徐");map.put("YG21060104", "殷");map.put("YG21060105", "张");map.put("YG21060106", "赵");map.put("YG21060107", "刘");map.put("YG21060108", "梁");map.put("YG21060109", "孙");//2、获取System.out.println("根据Key获取的value是:" + map.get("YG20160101"));//3.通过key删除对应的键值对,返回valueString value = map.remove("YG20160101");System.out.println("删除的value是:"+ value);//4.修改,先删除后添加map.put("YG20160100", value);System.out.println("Map中的value元素 " + map);//5.长度System.out.println("Map的长度是: " + map.size());//6.判断是否包含Key,不能用get(Key)System.out.println("Map是否包含Key(\"YG20160109\") " + map.containsKey("YG21060109"));//7.判断是否包含ValueSystem.out.println("Map是否包含Value(\"孙治宾\") " + map.containsValue("孙治宾"));//8.putAll 需要赋值的map2 用map2.putAll(map);Map<String,String> map2 = new HashMap<>();map2.putAll(map);System.out.println("Map2 元素是 "+map2);//9.map是否为空Map <String, String> map3 = new HashMap<>();map3 = null; //isEmpty()判断的是map中元素是否为空//System.out.println(map3.isEmpty());//map3空指针了//10.values 获取map中的值Collection<String> values = map2.values();for(String s : values){System.out.println("值: "+s);}System.out.println("——————————————————1————————————————————");//方法一 keySet + for循环Set<String> keySet = map.keySet();//APIfor(String key : keySet){String _value = map2.get(key);System.out.println("键:"+ key +"值:" + _value);}System.out.println("———————————————————2———————————————————");//方法二 keySet + iteratorIterator< String> iterator = keySet.iterator();while(iterator.hasNext()){String key = iterator.next();String _value = map2.get(key);System.out.println("键:"+ key +"值:" + _value);}System.out.println("———————————————————3———————————————————");//方法三 entrySet()Set<Entry<String, String>> entrySet = map2.entrySet();for(Entry<String,String> entry : entrySet){String key = entry.getKey();String _value = entry.getValue();System.out.println("键:"+ key +"值:" + _value);}System.out.println("————————————————————4——————————————————");//方法四 Set<Entry<String, String>> entrySet2 = map2.entrySet();Iterator<Entry<String, String>> iterator2 = entrySet2.iterator();while(iterator.hasNext()){Entry<String,String> entry = iterator2.next();String key = entry.getKey();String _value = entry.getValue();System.out.println("键:"+ key +"值:" + _value);}}
public static void randomRedBall(){//统计生成的1-33出现的次数
Map <Integer,Integer> map = new HashMap<>(); int i = 100000; while(i > 0){ Random random = new Random(); int key = random.nextInt(33)+1; if(map.containsKey(key)){ int value = map.get(key); value++; map.put(key, value); }else { map.put(key, 1); } i--; } System.out.println(map);
}
}
- 16、Map-Hashmap
- map hashmap
- map、hashmap
- Map--HashMap
- Map-HashMap
- map hashMap
- HashMap map= new HashMap();Map map=new HashMap(); 不是Map map=new Map();
- 认识Map之HashMap
- Map与hashMap
- Map ,Hashmap,HashTable,LinkedHashMap
- 遍历HashMap和Map
- Map--HashMap--简单操作
- 【Java】Map之HashMap
- 关于map和hashmap
- map和hashmap
- map-hashMap分组(example)
- Map集合HashMap嵌套
- Map--HashMap实现分析
- [算法题] 最接近零的子数组和 Subarray Sum Closest
- WPF listbox控件用法介绍
- python爬虫——基于selenium用火狐模拟登陆爬搜索关键词的微博
- linux下的apache的安装
- 回溯算法之装载问题
- 16、Map-Hashmap
- java基础
- 软件测试兼职
- WEB站点请求耗时分析-基于MongoDB的日志分析系统
- 第九周项目2-我的数组类
- 有状态会话bean和无状态会话bean
- [BZOJ1010][HNOI2008]玩具装箱toy(斜率优化dp)
- 稳压二极管的使用
- 《Android JNI》02 对java传进来的整形参数进行操作并返回