日期:2014-05-20 浏览次数:20678 次
import java.util.ArrayList;
import java.util.HashMap;
public class Test {
public static void main(String[] args) {
ArrayList<String> strList = new ArrayList<String>();
HashMap<String, String> strMap = new HashMap<String, String>();
strMap.put("1", "tt1");
strMap.put("2", "tt2");
strMap.put("3", "tt3");
strMap.put("4", "tt4");
strList.add("1");
strList.add("2");
strList.add("3");
strList.add("4");
//两个比较哪个快 TODO
if(strMap.containsKey("4")){
System.out.println("HashMap-->"+strMap.get("4"));
}
for (String string : strList) {
if(string.equals("4")){
System.out.println("ArrayList-->"+string);
}
}
}
}
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
for (int i = 0; i < 1000000; i++) {
list.add(String.valueOf(i));
}
long startList = System.nanoTime();
for (String string : list) {
if (string.equals("100000")) {
System.out.print("ArrayList-->");
long endList= System.nanoTime();
System.out.println("程序运行时间: "+(endList-startList)+"ms");
}
}
Map<String, String> map = new HashMap<String, String>();
for (int i = 0; i < 1000000; i++) {
map.put(String.valueOf(i), String.valueOf(i));
}
long startMap = System.currentTimeMillis();
if (map.containsKey("100000")) {
System.out.print("Map-->");
long endMap= System.nanoTime();
System.out.println("程序运行时间: "+(endMap-startMap)+"ms");
}
}