map.containsKey、map.containsValue和 list.contains性能的对比

  • Post author:
  • Post category:其他


一、程序

public static void main(String[] args) {

List<String> list = new ArrayList<String>();

Map<String, String> map = new HashMap<String, String>();

long begin = System.currentTimeMillis();

for(int i=0; i<1000000; i++) {

String str = i +”ABCDsdmsdnsdnsksdsds”;

list.add(str);

map.put(str, str +”B”);

}

System.out.println(“end———–遍历:”+ (System.currentTimeMillis()-begin) +” 毫秒”);

long begin1 = System.currentTimeMillis();

System.out.println(list.contains(“999999ABCDsdmsdnsdnsksdsds”));

System.out.println(“end———–list:”+ (System.currentTimeMillis()-begin1) +” 毫秒”);

long begin2 = System.currentTimeMillis();

System.out.println(map.containsValue(“999999ABCDsdmsdnsdnsksdsdsB”));

System.out.println(“end———–map-Value:”+ (System.currentTimeMillis()-begin2) +” 毫秒”);

long begin3 = System.currentTimeMillis();

System.out.println(map.containsKey(“999999ABCDsdmsdnsdnsksdsds”));

System.out.println(“end———–map-Key:”+ (System.currentTimeMillis()-begin3) +” 毫秒”);

}

二、总结:

1、千万级及以上:map.containsKey > map.containsValue > list

2、百万级及以下:map.containsKey > list > map.containsValue

三、代码核心:

1、list.contains :

2、map.containsValue

3、map.containsKey



版权声明:本文为liyb1078422974原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。