在学习HashMap的时候遇到一道题:
准备一个ArrayList其中存放3000000(三百万个)Hero对象,其名称是随机的,格式是hero-[4位随机数]
hero-3229
hero-6232
hero-9365
…
因为总数很大,所以几乎每种都有重复,把名字叫做 hero-5555的所有对象找出来。
要求借助HashMap,找出结果,并统计花费的时间
import charactor.Hero;
public class TestCollection {
public static void main(String[] args) {
List<Hero> hs =new ArrayList<>();
System.out.println("初始化开始");
for (int i = 0; i < 3000000; i++) {
Hero h = new Hero( "hero-" + random());
hs.add(h);
}
//名字作为key
//名字相同的hero,放在一个List中,作为value
HashMap<String,List<Hero>> heroMap =new HashMap();
for (Hero h : hs) {
List<Hero> list= heroMap.get( h.name);
if(list==null){
list = new ArrayList<>();
heroMap.put(h.name, list);
}
list.add(h);
}
System.out.println("初始化结束");
System.out.println("开始查找");
findByIteration(hs);
findByMap(heroMap);
}
private static List<Hero> findByMap(HashMap<String,List<Hero>> m) {
long start =System.currentTimeMillis();
List <Hero>result= m.get("hero-5555");
long end =System.currentTimeMillis();
System.out.printf("通过map查找,一共找到%d个英雄,耗时%d 毫秒%n",result.size(),end-start);
return result;
}
private static List<Hero> findByIteration (List<Hero> hs) {
long start =System.currentTimeMillis();
List<Hero> result =new ArrayList<>();
for (Hero h : hs) {
if(h.name.equals("hero-5555")){
result.add(h);
}
}
long end =System.currentTimeMillis();
System.out.printf("通过for查找,一共找到%d个英雄,耗时%d 毫秒%n", result.size(),end-start);
return result;
}
public static int random(){
return ((int)(Math.random()*9000)+1000);
}
}
- 比较费解为什么每次都要重置list
HashMap<String,List<Hero>> heroMap =new HashMap();
for (Hero h : hs) {
List<Hero> list= heroMap.get( h.name);
if(list==null){
list = new ArrayList<>();
heroMap.put(h.name, list);
}
list.add(h);
}
- 这里令list指向heroMap中key(h.name)对应的List集合(value)。
- 如果链表为空,则代表原来的map中并没有这组数据,则创建新的链表,并将这一组
<String,List<Hero>>
放入hashMap中,再应用list.add(h)
将新的对象值插入列表。 - 不为空则顺延加入新的对象