1. Stepan Koltsov
  2. bolts

Commits

Stepan Koltsov  committed 8425b69

optimize CollectionF.map

CollectionF.map now allocates resulting collection with initial
capacity equal to this collection size.

IteratorF has new toList(initialCapacity) method.

  • Participants
  • Parent commits d90603d
  • Branches trunk

Comments (0)

Files changed (3)

File src/main/java/ru/yandex/bolts/collection/IteratorF.java

View file
     /** Collect elements */
     ListF<E> toList();
 
+    ListF<E> toList(int initialCapacity);
+
     /** Collect elements to set */
     SetF<E> toSet();
 

File src/main/java/ru/yandex/bolts/collection/impl/AbstractCollectionF.java

View file
 import java.util.Collections;
 import java.util.Iterator;
 import java.util.List;
-import java.util.NoSuchElementException;
 
 import ru.yandex.bolts.collection.Cf;
 import ru.yandex.bolts.collection.CollectionF;
 import ru.yandex.bolts.function.Function;
 import ru.yandex.bolts.function.Function1B;
 import ru.yandex.bolts.function.Function1V;
-import ru.yandex.bolts.function.Function2;
 import ru.yandex.bolts.function.Function2I;
 import ru.yandex.bolts.function.forhuman.Comparator;
 
 
     public <B> ListF<B> map(Function<? super E, B> f) {
         if (isEmpty()) return Cf.list();
-        else return iterator().map(f).toList();
+        else return iterator().map(f).toList(size());
     }
 
     @Override

File src/main/java/ru/yandex/bolts/collection/impl/AbstractIteratorF.java

View file
         throw new UnsupportedOperationException();
     }
 
+    @Override
     public ListF<E> toList() {
+        return toListImpl(-1);
+    }
+
+    @Override
+    public ListF<E> toList(int initialCapacity) {
+        return toListImpl(initialCapacity);
+    }
+
+    private ListF<E> toListImpl(int initialCapacity) {
         if (!hasNext()) return CollectionsF.list();
 
-        ArrayListF<E> result = new ArrayListF<E>();
+        ArrayListF<E> result;
+        if (initialCapacity < 0) {
+            result = new ArrayListF<E>();
+        } else {
+            result = new ArrayListF<E>(initialCapacity);
+        }
         forEach(result.addF());
         return result.convertToReadOnly();
     }