java集合框架之HashMap

时间:2023-03-09 07:15:17
java集合框架之HashMap

参考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=;i<;i++){
Hero hero=new Hero();
String str=String.valueOf(Math.random()*+).substring(,);
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=;
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());
resultList.put(hero.getName(),list);
}
       list.add(hero);
}
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 { }

java集合框架之HashMap

由此可见,不使用HashMap,只用for循环 比 使用HashMap 快12倍