一、程序
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