在程序里生成了大量的new File()对象,不知道这样的代码对程序的性能有不好的影响吗?
代码如下:
Map<File, String> ct = new TreeMap<File, String>();
for(int i =0 ;i < 3000 ;i ++){
ct.put(new File("//aa//"+i+".txt"), "");
}
谢谢。
代码如下:
Map<File, String> ct = new TreeMap<File, String>();
for(int i =0 ;i < 3000 ;i ++){
ct.put(new File("//aa//"+i+".txt"), "");
}
谢谢。
Map内容如果太多,put什么都会占内存.
没见过用file对象做key的 用文件唯一标示做key吧 比如路径
前边路径相同,用 String 做 Key 不行吗? ct.put("//aa//"+i+".txt", "");
for(int i =0 ;i < 3000 ;i ++){
ct.put(new File("//aa//"+i+".txt"), "");
}的作用和List<File> list = new ArrayList<File>(3000);
for(int i =0 ;i < 3000 ;i ++){
list.add(new File("//aa//"+i+".txt"));
}
有什么本质区别?
new其它的对象这样做也是不太适合的。
最好是处理一个new一个,处理完了就销毁掉
最好的做作是在循环外面new,每次循环的时候把这个引用指向不同的对象