์๋
ํ์ธ์ ์ด์์
๋๋ค. Collection์ toArray ๋ฉ์๋์ Stream์ toArray ๋ฉ์๋์ ์ฐจ์ด์ ๋ํด์ ์์๋ณด๊ณ ์ ํด์.๐ง
Collection ์ธํฐํ์ด์ค๋ toArray ๋ฉ์๋๋ฅผ ๊ตฌํํ๋๋ก ํ๊ณ ์์ผ๋ฉฐ, Stream ์ธํฐํ์ด์ค ๋ํ toArray ๋ฉ์๋๋ฅผ ๊ตฌํํ๋๋ก ํ๊ณ ์์ด์.
Collection ์ธํฐํ์ด์ค๋ ๋ฐฐ์ด๋ก ๋ณํํ๋๋ฐ ์ฌ์ฉํ ์ ์๋ toArray ๋ฉ์๋๋ฅผ ์ ๊ณตํด์.
Object[] toArray(): ์ปฌ๋ ์
์ ์์๋ฅผ Object ํ์
์ ๋ฐฐ์ด๋ก ๋ฐํํด์.
<T> T[] toArray(T[] a): ๊ธฐ๋ณธ์ ์ผ๋ก ๋งค๊ฐ๋ณ์๋ก ๋ค์ด์จ a๋ฐฐ์ด์ Collection์ ๋ค์ด์๋ ์์๋ฅผ ๋ด์์ a๊ฐ์ฒด๋ฅผ ๋ฐํํด์ค์. ํ์ง๋ง ๋งค๊ฐ๋ณ์๋ก ๋ค์ด์จ a๋ฐฐ์ด์ ๊ธธ์ด๊ฐ Collection์ ๋ค์ด์๋ ์์์ ๊ฐ์(size)๋ณด๋ค ์๋ค๋ฉด ์๋ก์ด ๋ฐฐ์ด์ ์์๋ฅผ ๋ฐํํด์.
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
Integer[] myIntegers1 = new Integer[0]; // list์ size๋ณด๋ค ์์์.
Integer[] myIntegers2 = new Integer[list.size()];
Integer[] integers1 = list.toArray(myIntegers1);
Integer[] integers2 = list.toArray(myIntegers2);
System.out.println(myIntegers1 == integers1); // false
System.out.println(myIntegers2 == integers2); // true
Collection์ toArray๋ ๋ด๋ถ์ ์ผ๋ก System.arraycopy๋ฅผ ์ฌ์ฉํด์. System.arraycopy๋ native ์ฝ๋๋ฅผ ์ฌ์ฉํ์ฌ ์ฑ๋ฅ์ ์ผ๋ก ์ฐ์ํด์. ๋ฟ๋ง์๋๋ผ, ๋ด๋ถ์ ์ผ๋ก Arrays.copyOf ์ฌ์ฉ๋์ด์ง๋๋ฐ, Arrays.copyOf ๋ํ ๋ด๋ถ์ ์ผ๋ก System.arraycopy๋ฅผ ์ฌ์ฉํด์ ๋น์ทํ ์ฑ๋ฅ์ ๊ฐ์ ธ์. ์๋์ System.arraycopy์ Arrays.copyOf์ ๋ฐด์น๋งํฌ ๊ฒฐ๊ณผ์์ ํ์ธํ ์ ์์ด์.
Baeldung์์ ์งํํ System.arraycopy์ Arrays.copyOf์ ๋ฐด์น๋งํฌ ๊ฒฐ๊ณผ
Benchmark (SIZE) Mode Cnt Score Error Units
ObjectsCopyBenchmark.arraysCopyOfBenchmark 10 avgt 100 8.535 ยฑ 0.006 ns/op
ObjectsCopyBenchmark.arraysCopyOfBenchmark 1000000 avgt 100 2831316.981 ยฑ 15956.082 ns/op
ObjectsCopyBenchmark.systemArrayCopyBenchmark 10 avgt 100 9.278 ยฑ 0.005 ns/op
ObjectsCopyBenchmark.systemArrayCopyBenchmark 1000000 avgt 100 2826917.513 ยฑ 15585.400 ns/op
PrimitivesCopyBenchmark.arraysCopyOfBenchmark 10 avgt 100 9.172 ยฑ 0.008 ns/op
PrimitivesCopyBenchmark.arraysCopyOfBenchmark 1000000 avgt 100 476395.127 ยฑ 310.189 ns/op
PrimitivesCopyBenchmark.systemArrayCopyBenchmark 10 avgt 100 8.952 ยฑ 0.004 ns/op
PrimitivesCopyBenchmark.systemArrayCopyBenchmark 1000000 avgt 100 475088.291 ยฑ 726.416 ns/op
Performance of System.arraycopy() vs. Arrays.copyOf() | Baeldung
๐จ ์ข ์ข ๋ค๋ฅธ ๋ธ๋ก๊ทธ ๊ธ์์ Arrays.copyOf์ ์ฑ๋ฅ์ด System.arraycopy๋ณด๋ค ์ฐ์ํ๋ค๊ณ (๋ง๊ฒ๋ 2๋ฐฐ ์ด์) ์์ฑํด ๋์ ๊ธ์ด ๋ณด์ด๋๋ฐ ์ด๋ ํ๋ฆฐ ๋ด์ฉ์ด์์! Arrays.copyOf๋ ๋ด๋ถ์ ์ผ๋ก System.arraycopy๋ฅผ ์ฌ์ฉํ๊ธฐ ๋๋ฌธ์ System.arraycopy ๋ณด๋ค ์ฑ๋ฅ์ ์ผ๋ก ํฌ๊ฒ ์ฐ์ํ ์ ์์ด์.
Stream ์ธํฐํ์ด์ค๋ ์คํธ๋ฆผ์ ์์๋ฅผ ๋ฐฐ์ด๋ก ๋ณํํ๋ toArray ๋ฉ์๋๋ฅผ ์ ๊ณตํด์.
Object[] toArray(): ์ปฌ๋ ์
์ ์์๋ฅผ Object ํ์
์ ๋ฐฐ์ด๋ก ๋ฐํํด์.
<A> A[] toArray(IntFunction<A[]> generator) : generator ๋งค๊ฐ๋ณ์๋ฅผ ์ด์ฉํ์ฌ A[] ๋ฐฐ์ด์ ๋ฐํํด์.
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
Integer[] integers = list.stream().toArray(Integer[]::new);
list.stream().toArray(Integer[]::new)๋ฅผ ์ํํ ๋ ํด๋น ๋ฉ์๋๋ ์ปฌ๋ ์
์์์ ๊ฐ์๋ฅผ ์ ์ ์์ด์. ๋ฐ๋ผ์ Stream์ toArray ๋ฉ์๋๋ ์ปฌ๋ ์
์ ๋ชจ๋ ๊ฐ์ ์์งํ ๋ค์ ๋ฐฐ์ด์ ์๋กญ๊ฒ ์์ฑํ์ฌ ํด๋น ๋ฐฐ์ด์ ๋ณต์ฌํด์.
๋ฐ๋ผ์ Stream์ toArray ๋ฉ์๋๋ Collection์ toArray๋ณด๋ค ํจ์ฌ ๋๋ฆฌ๊ณ ๋ ๋ง์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์๋นํด์. Stream์ Object[] toArray() ๋ฉ์๋๋ ๋ง์ฐฌ๊ฐ์ง์์. ์๋์ ์ฝ๋๋ Stream์ Object[] toArray()์ ๊ตฌํ ์ฝ๋์์.
// Stream์ Object[] ๋ฐฐ์ด์ ๋ฐํํ๋ toArray() ๋ฉ์๋ ๊ตฌํ
@Override
public final Object[] toArray() {
return toArray(Object[]::new);
}
๋ฐ๋ผ์ ๋จ์ํ๊ฒ Collection์ ๋ฐฐ์ด๋ก ๋ฐํํ๋ ค ํ๋ค๋ฉด Stream์ toArray๋ฅผ ์ฌ์ฉํ๊ธฐ๋ณด๋ค, Collection์ toArray๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ๋ ํจ์จ์ ์ด์์.
