Skip to content

Commit 1913d4d

Browse files
Eliminate duplication of List in toList
Fixes #1218
1 parent 73b7518 commit 1913d4d

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

rxjava-core/src/main/java/rx/internal/operators/OperatorToObservableList.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import rx.Subscriber;
2020

2121
import java.util.ArrayList;
22+
import java.util.Collections;
2223
import java.util.LinkedList;
2324
import java.util.List;
2425

@@ -42,6 +43,7 @@ public final class OperatorToObservableList<T> implements Operator<List<T>, T> {
4243
public Subscriber<? super T> call(final Subscriber<? super List<T>> o) {
4344
return new Subscriber<T>(o) {
4445

46+
private boolean completed = false;
4547
final List<T> list = new LinkedList<T>();
4648

4749
@Override
@@ -52,7 +54,8 @@ public void onStart() {
5254
@Override
5355
public void onCompleted() {
5456
try {
55-
o.onNext(new ArrayList<T>(list));
57+
completed = true;
58+
o.onNext(Collections.unmodifiableList(list));
5659
o.onCompleted();
5760
} catch (Throwable e) {
5861
onError(e);
@@ -66,7 +69,9 @@ public void onError(Throwable e) {
6669

6770
@Override
6871
public void onNext(T value) {
69-
list.add(value);
72+
if (!completed) {
73+
list.add(value);
74+
}
7075
}
7176

7277
};

0 commit comments

Comments
 (0)