From b31f756500da112186c2bb8c9d739ca202424900 Mon Sep 17 00:00:00 2001 From: Andreas Huber Date: Sun, 17 Feb 2019 09:11:09 +0100 Subject: [PATCH] addAll for a collection of LongLists The benefit is, that we only need one memory allocation. --- .../java/org/lucares/collections/LongList.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/primitiveCollections/src/main/java/org/lucares/collections/LongList.java b/primitiveCollections/src/main/java/org/lucares/collections/LongList.java index 95d328c..e2e7c84 100644 --- a/primitiveCollections/src/main/java/org/lucares/collections/LongList.java +++ b/primitiveCollections/src/main/java/org/lucares/collections/LongList.java @@ -2,6 +2,7 @@ package org.lucares.collections; import java.io.Serializable; import java.util.Arrays; +import java.util.Collection; import java.util.List; import java.util.Random; import java.util.Spliterator.OfLong; @@ -380,6 +381,21 @@ public final class LongList implements Serializable, Cloneable { size += list.size(); } + /** + * Add all values of the given lists. + * + * @param longLists collection of the lists + * @throws NullPointerException if the collection of {@link LongList} is null or + * contains null + */ + public void addAll(final Collection longLists) { + final int sizeNewElements = longLists.stream().mapToInt(LongList::size).sum(); + ensureCapacity(sizeNewElements); + for (final LongList longList : longLists) { + addAll(longList); + } + } + /** * Removes elements from the list. *