java基础——hashMap查找

package collection;

import character.Hero;

import java.util.*;

public class TestCollection {
    public static void main(String[] args) {

        List<Hero> heros = new ArrayList<>();
        for(int i =0;i<300*10000;i++){
            heros.add(new Hero("hero-"+(int)(Math.random()*9000+1000)));
        }
        System.out.println(heros.get(0));

        int index = 0;
        int n = 0;
        long start = System.currentTimeMillis();
        for(Hero h:heros){
            if("hero-5555".equals(h.name)){
                System.out.println((index++)+" : "+h.name);
                n++;
            }
        }
        long end = System.currentTimeMillis();
        System.out.println((end-start)+"毫秒");

        HashMap<String,List<Hero>> heroMap = new HashMap<String, List<Hero>>();
        for(Hero h:heros){
            List<Hero> list = heroMap.get(h.name);
            if(null==list){
                list = new ArrayList<>();
                heroMap.put(h.name,list);
            }
            list.add(h);
        }

        long start1 = System.currentTimeMillis();
        List<Hero> result = heroMap.get("hero-5555");

        long end1 = System.currentTimeMillis();
        System.out.printf("一共找到%d个英雄,耗时:%d毫秒",result.size(),(end1-start1));
    }

}

你可能感兴趣的:(java基础,java,hashmap)