add stream() and parallelStream()

Both methods return an IntStream.
This commit is contained in:
2017-09-29 18:57:43 +02:00
parent fc1ca26d52
commit e7d88babb4
2 changed files with 100 additions and 1 deletions

View File

@@ -3,6 +3,9 @@ package org.lucares.collections;
import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import java.util.Spliterator.OfInt;
import java.util.stream.IntStream;
import java.util.stream.StreamSupport;
/**
* A list for primitive ints.
@@ -15,7 +18,6 @@ public class IntList implements Serializable, Cloneable {
// TODO support Iterator
// TODO add mod counts
// TODO support sublists
// TODO support Stream-API
// TODO add retainAll
// TODO add removeAll
// TODO add removeIf
@@ -25,6 +27,7 @@ public class IntList implements Serializable, Cloneable {
// TODO add indexOf
// TODO add toArray(int[] a)
// TODO remove bounds checks that are handled by java, e.g. negative indices
// TODO toString
private static final long serialVersionUID = -6823520157007564746L;
@@ -318,6 +321,46 @@ public class IntList implements Serializable, Cloneable {
}
}
/**
* Returns a sequential {@link IntStream} with this collection as its source.
*
* @return a sequential {@link IntStream}
*/
public IntStream stream() {
return Arrays.stream(data, 0, index);
}
/**
* Returns a parallel {@link IntStream} with this collection as its source.
*
* @return a parallel {@link IntStream}
*/
public IntStream parallelStream() {
final OfInt spliterator = Arrays.spliterator(data, 0, index);
return StreamSupport.intStream(spliterator, true);
}
@Override
public String toString() {
if (data == null)
return "null";
final int iMax = data.length - 1;
if (iMax == -1)
return "[]";
final StringBuilder result = new StringBuilder();
result.append('[');
for (int i = 0; i < index; i++) {
if (i > 0) {
result.append(", ");
}
result.append(data[i]);
}
return result.append(']').toString();
}
@Override
public int hashCode() {
if (index == 0) {