一、ArrayList和HashSet的contains方法效率比较
结论放前面:HaseSet contains效率高
list
查询10000个对象
共耗费时间:29694毫秒
set
查询10000个对象
共耗费时间:975毫秒
测试代码
private void dowork() {
List<String> list = new ArrayList<String>();
Set<String> set = new HashSet<String>();
for(int i = 0; i <= 100000; i++){
list.add("haha"+i);
set.add("haha"+i);
}
long start = System.currentTimeMillis();
for(int i = 0; i<= 100000; i++){
// System.out.println(i+"__"+list.contains("haha"+i));
System.out.println(i+"__"+set.contains("haha"+i));
}
long end = System.currentTimeMillis();
System.out.println("查询10000个对象 \n共耗费时间:"+(end-start)+ "毫秒");
}
二:ConcurrentHashMap线程安全
Map<String, String> conMap = new ConcurrentHashMap<String, String>();
Map<String, String> map = new HashMap<String, String>();
conMap.get(null);//会报错
map.get(null);//不报错
线程安全的List
List<String> dataList = Collections.synchronizedList(new LinkedList<String>());
线程安全的Int
AtomicInteger count = new AtomicInteger(0);
count.incrementAndGet();//+1