参考 http://how2j.cn/k/collection/collection-hashmap/365.html#nowhere
HashMap 的键值对
HashMap 储存数据的方式是 -- 键值对
- package collection;
- import java.util.HashMap;
- public class TestCollection {public static void main(String[] args) {
- HashMap<String,String> dictionary = new HashMap<>();
- dictionary.put("adc", "物理英雄");
- dictionary.put("apc", "魔法英雄");
- dictionary.put("t", "坦克");
- System.out.println(dictionary.get("t"));
- }
- }
键不能重复, 值可以重复
对于 HashMap 而言, key 是唯一的, 不可以重复的.
所以, 以相同的 key 把不同的 value 插入到 Map 中会导致旧元素被覆盖, 只留下最后插入的元素.
不过, 同一个对象可以作为值插入到 map 中, 只要对应的 key 不一样
遍历 HashMap 几种方式
- // 第一种: 普遍使用, 二次取值
- System.out.println("通过 Map.keySet 遍历 key 和 value:");
- for (String key : map.keySet()) {
- System.out.println("key="+ key + "and value=" + map.get(key));
- }
- // 第二种
- System.out.println("通过 Map.entrySet 使用 iterator 遍历 key 和 value:");
- Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
- while (it.hasNext()) {
- Map.Entry<String, String> entry = it.next();
- System.out.println("key=" + entry.getKey() + "and value=" + entry.getValue());
- }
- // 第三种: 推荐, 尤其是容量大时
- System.out.println("通过 Map.entrySet 遍历 key 和 value");
- for (Map.Entry<String, String> entry : map.entrySet()) {
- System.out.println("key=" + entry.getKey() + "and value=" + entry.getValue());
- }
- // 第四种
- System.out.println("通过 Map.values() 遍历所有的 value, 但不能遍历 key");
- for (String v : map.values()) {
- System.out.println("value=" + v);
- }
- }
查找内容性能比较
准备一个 ArrayList 其中存放 3000000(三百万个)Hero 对象, 其名称是随机的, 格式是 hero-[4 位随机数]
- hero-3229
- hero-6232
- hero-9365
- ...
因为总数很大, 所以几乎每种都有重复, 把名字叫做 hero-5555 的所有对象找出来
要求使用两种办法来寻找
1. 不使用 HashMap, 直接使用 for 循环找出来, 并统计花费的时间
2. 借助 HashMap, 找出结果, 并统计花费的时间
- package swordOffer;
- import java.util.*;
- public class test {
- public static void main(String[] args) {
- ArrayList<Hero> arrayList=new ArrayList<>();
- // 初始化
- for (int i=0;i<3000000;i++){
- Hero hero=new Hero();
- String str=String.valueOf(Math.random()*9000+1000).substring(0,4);
- hero.setName("hero-"+str);
- arrayList.add(hero);
- }
- test(arrayList,"hero-5555");
- testHashMap(arrayList,"hero-5555");
- }
- private static void test(ArrayList<Hero> arrayList,String key){
- long timeBegin=System.currentTimeMillis();
- int count=0;
- for (Hero hero:arrayList){
- if(hero.getName().equals(key)){
- count++;
- }
- }
- long timeEnd=System.currentTimeMillis();
- System.out.println("没有使用 HashMap:ArrayList 其中存放 3000000(三百万个)Hero 对象, 其名称是随机的, 格式是 hero-[4 位随机数], 把名字叫做 hero-5555 的所有对象找出来用时:"+(timeEnd-timeBegin)+"毫秒; 共有"+count+"个 hero-5555");
- }
- private static void testHashMap(ArrayList<Hero> arrayList,String key){
- long timeBegin=System.currentTimeMillis();
- HashMap<String,ArrayList<Hero>> resultList=new HashMap<>();
- for (Hero hero:arrayList){
- if(resultList.get(hero.getName())!=null){
- ArrayList list=resultList.get(hero.getName());
- list.add(hero);
- resultList.put(hero.getName(),list);
- }else{
- ArrayList list=new ArrayList();
- list.add(hero);
- resultList.put(hero.getName(),list);
- }
- }
- long timeEnd=System.currentTimeMillis();
- System.out.println("使用 HashMap:ArrayList 其中存放 3000000(三百万个)Hero 对象, 其名称是随机的, 格式是 hero-[4 位随机数], 把名字叫做 hero-5555 的所有对象找出来用时:"+(timeEnd-timeBegin)+"毫秒; 共有"+resultList.get(key).size()+"个 hero-5555");
- }
- }
- class Hero {
- private String name;
- public Hero(){}
- public Hero(String name){
- name=name;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- }
- class ADHero extends Hero {
- }
由此可见, 不使用 HashMap, 只用 for 循环 比 使用 HashMap 快 12 倍
来源: https://www.cnblogs.com/lijingran/p/9080292.html