|
23 | 23 | import java.util.Iterator;
|
24 | 24 | import java.util.Map;
|
25 | 25 | import java.util.Objects;
|
| 26 | +import java.util.Spliterator; |
| 27 | +import java.util.Spliterators; |
| 28 | +import java.util.function.Consumer; |
26 | 29 | import java.util.stream.Stream;
|
| 30 | +import java.util.stream.StreamSupport; |
27 | 31 |
|
28 | 32 | /**
|
29 | 33 | * An immutable map implementation based on open hash map.
|
@@ -170,11 +174,18 @@ public void remove() {
|
170 | 174 | * @return a {@link Stream} of the map entries as {@link Entry}
|
171 | 175 | */
|
172 | 176 | public Stream<Entry<KType, VType>> stream() {
|
173 |
| - Stream.Builder<Entry<KType, VType>> streamBuilder = Stream.builder(); |
174 |
| - for (ObjectObjectCursor<KType, VType> cursor : map) { |
175 |
| - streamBuilder.add(new Entry<>(cursor.key, cursor.value)); |
176 |
| - } |
177 |
| - return streamBuilder.build(); |
| 177 | + Iterator<ObjectObjectCursor<KType, VType>> mapIterator = map.iterator(); |
| 178 | + return StreamSupport.stream(new Spliterators.AbstractSpliterator<>(map.size(), Spliterator.SIZED) { |
| 179 | + @Override |
| 180 | + public boolean tryAdvance(Consumer<? super Entry<KType, VType>> action) { |
| 181 | + if (mapIterator.hasNext() == false) { |
| 182 | + return false; |
| 183 | + } |
| 184 | + ObjectObjectCursor<KType, VType> cursor = mapIterator.next(); |
| 185 | + action.accept(new Entry<>(cursor.key, cursor.value)); |
| 186 | + return true; |
| 187 | + } |
| 188 | + }, false); |
178 | 189 | }
|
179 | 190 |
|
180 | 191 | @Override
|
|
0 commit comments