Skip to content

Commit df7b0c7

Browse files
stsypanovMartin Buchholz
authored andcommitted
8274715: Implement forEach in Collections.CopiesList
Reviewed-by: martin
1 parent d4e8712 commit df7b0c7

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

src/java.base/share/classes/java/util/Collections.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5182,6 +5182,16 @@ public E get(int index) {
51825182
return element;
51835183
}
51845184

5185+
@Override
5186+
public void forEach(Consumer<? super E> action) {
5187+
Objects.requireNonNull(action);
5188+
int n = this.n;
5189+
E element = this.element;
5190+
for (int i = 0; i < n; i++) {
5191+
action.accept(element);
5192+
}
5193+
}
5194+
51855195
public Object[] toArray() {
51865196
final Object[] a = new Object[n];
51875197
if (element != null)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package micro.org.openjdk.bench.java.util;
2+
3+
import org.openjdk.jmh.annotations.*;
4+
import org.openjdk.jmh.infra.Blackhole;
5+
6+
import java.util.*;
7+
import java.util.concurrent.TimeUnit;
8+
9+
/**
10+
* @see <a href="https://bugs.openjdk.java.net/browse/JDK-8274715">JDK-8274715</a>
11+
*/
12+
@Fork(value = 3)
13+
@State(Scope.Thread)
14+
@BenchmarkMode(Mode.AverageTime)
15+
@OutputTimeUnit(TimeUnit.NANOSECONDS)
16+
@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
17+
@Measurement(iterations = 5, time = 2, timeUnit = TimeUnit.SECONDS)
18+
public class NCopiesBenchmarks {
19+
@Param({"10", "50", "100"})
20+
int size;
21+
22+
private List<Object> list;
23+
24+
@Setup
25+
public void prepare() {
26+
list = Collections.nCopies(size, new Object());
27+
}
28+
29+
@Benchmark
30+
public void forEach(Blackhole bh) {
31+
list.forEach(bh::consume);
32+
}
33+
34+
}

0 commit comments

Comments
 (0)