public static void compare() {
List<String> list = new ArrayList<String>();
Set<String> set = new HashSet<String>();
for(int i=0;i<100000;i++) {
list.add("a"+i);
}
long t1=0L,t2=0L;
t1 = System.currentTimeMillis();
for(String s : list) {
set.add(s);
}
t2 = System.currentTimeMillis();
System.out.println("forEach遍历的时间为:"+(t2-t1)+"mm");
////two
t1 = System.currentTimeMillis();
for(int i=0; i<list.size(); i++) {
String str = list.get(i);
set.add(list.get(i));
}
t2 = System.currentTimeMillis();
System.out.println("size遍历的时间为:"+(t2-t1)+"mm");
////three
t1 = System.currentTimeMillis();
Iterator<String> iterator = list.iterator();
while(iterator.hasNext()) {
String str = iterator.next();
set.add((String)iterator.next());
}
t2 = System.currentTimeMillis();
System.out.println("iterator遍历的时间为:"+(t2-t1)+"mm");
}

forEach遍历的时间为:45mm
size遍历的时间为:14mm
iterator遍历的时间为:7mm


相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-12-31
  • 2022-01-12
  • 2022-12-23
  • 2022-02-19
  • 2021-11-15
  • 2021-10-12
猜你喜欢
  • 2021-09-16
  • 2021-08-11
  • 2021-10-08
  • 2022-02-15
  • 2022-01-27
相关资源
相似解决方案