public
class
test {
public
static
void
main(String[] args)
throws
Exception {
int
count=
10000
*
100
;
long
start= System.currentTimeMillis();
List<Integer> list=
new
ArrayList<>();
for
(
int
i =
0
; i < count; i++) {
list.add(i);
}
long
end=System.currentTimeMillis();
System.out.println(
"ArrayList无初始长度"
+(end-start));
List<Integer> list1=
new
ArrayList<>(count);
for
(
int
i =
0
; i < count; i++) {
list1.add(i);
}
start=System.currentTimeMillis();
System.out.println(
"ArrayList有初始长度"
+(start-end));
Map<Integer,Integer> map=
new
HashMap<>();
for
(
int
i =
0
; i < count; i++) {
map.put(i,i);
}
end=System.currentTimeMillis();
System.out.println(
"HashMap无初始长度"
+(end-start));
Map<Integer,Integer> map1=
new
HashMap<>(count);
for
(
int
i =
0
; i < count; i++) {
map1.put(i,i);
}
start=System.currentTimeMillis();
System.out.println(
"HashMap有初始长度"
+(start-end));
}
}