List<Map<String,String>> list = new ArrayList<>();
//造数据
for(int i = 0;i<20000;i++){
Map<String,String> m = new HashMap<>();
list.add(m);
m.put("count","test"+i);
}
//map 方式
long start = System.currentTimeMillis();
Map<String,String> map = new HashMap<>();
for(Map<String,String> m:list){
map.put(m.get("count"),m.get("count"));
}
for(Map<String,String> m:list){
map.get(m.get("count"));
}
long end = System.currentTimeMillis();
System.out.println(end-start);
// list 方式取值
start = System.currentTimeMillis();
for(Map<String,String> m:list){
for(Map<String,String> m1:list){
if(m1.get("count").equals(m.get("count"))){
m1.get("count");
break;
}
}
}
end = System.currentTimeMillis();
System.out.println(end-start);
List<Map<String,String>> list = new ArrayList<>();
for(int i = 0;i<2000000;i++){
Map<String,String> m = new HashMap<>();
list.add(m);
m.put("count","test"+i);
}
long start = System.currentTimeMillis();
int len = list.size();
List<String> str = new ArrayList<>();
str.add("1");
str.add("2");
str.add("3");
str.add("5");
str.add("7");
str.add("7");
str.add("9");
str.add("10");
str.add("11");
final boolean b = str.contains("1");
for (int i = 0; i < len; i++) {
if(b){
list.get(i).get("count");
}
}
long end = System.currentTimeMillis();
System.out.println(end-start);
start = System.currentTimeMillis();
for (int i = 0; i < len; i++) {
if(str.contains("1")){
list.get(i).get("count");
}
}
end = System.currentTimeMillis();
System.out.println(end-start);
Connected to the target VM, address: '127.0.0.1:60132', transport: 'socket'
47
39
Disconnected from the target VM, address: '127.0.0.1:60132', transport: 'socket'
Process finished with exit code 0
总结 利用map来进行获取 获取数据 效率要高于使用循环处理
尽量避免在循环中运算