Merge pull request #7726 from rodrigolgraciano/BAEL-3173

BAEL-3173
This commit is contained in:
rpvilao 2019-09-05 19:11:26 +02:00 committed by GitHub
commit f87160da11
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 91 additions and 3 deletions

View File

@ -9,9 +9,14 @@
<version>1.0-SNAPSHOT</version>
<name>libraries-primitive</name>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/it.unimi.dsi/fastutil -->
<dependency>
<!-- https://mvnrepository.com/artifact/it.unimi.dsi/fastutil -->
<dependency>
<groupId>it.unimi.dsi</groupId>
<artifactId>fastutil</artifactId>
<version>8.2.2</version>
@ -36,5 +41,18 @@
<version>1.19</version>
<scope>test</scope>
</dependency>
<!-- Eclipse Collections -->
<dependency>
<groupId>org.eclipse.collections</groupId>
<artifactId>eclipse-collections-api</artifactId>
<version>10.0.0</version>
</dependency>
<dependency>
<groupId>org.eclipse.collections</groupId>
<artifactId>eclipse-collections</artifactId>
<version>10.0.0</version>
</dependency>
</dependencies>
</project>
</project>

View File

@ -0,0 +1,70 @@
package com.baeldung;
import org.eclipse.collections.api.list.primitive.ImmutableIntList;
import org.eclipse.collections.api.list.primitive.MutableLongList;
import org.eclipse.collections.api.map.primitive.MutableIntIntMap;
import org.eclipse.collections.api.set.primitive.MutableIntSet;
import org.eclipse.collections.impl.factory.primitive.*;
import org.eclipse.collections.impl.list.Interval;
import org.eclipse.collections.impl.list.primitive.IntInterval;
import org.eclipse.collections.impl.map.mutable.primitive.IntIntHashMap;
import org.junit.Test;
import java.util.stream.DoubleStream;
import static org.junit.Assert.assertEquals;
public class PrimitiveCollectionsUnitTest {
@Test
public void whenListOfLongHasOneTwoThree_thenSumIsSix() {
MutableLongList longList = LongLists.mutable.of(1L, 2L, 3L);
assertEquals(6, longList.sum());
}
@Test
public void whenListOfIntHasOneTwoThree_thenMaxIsThree() {
ImmutableIntList intList = IntLists.immutable.of(1, 2, 3);
assertEquals(3, intList.max());
}
@Test
public void whenConvertFromIterableToPrimitive_thenValuesAreEquals() {
Iterable<Integer> iterable = Interval.oneTo(3);
MutableIntSet intSet = IntSets.mutable.withAll(iterable);
IntInterval intInterval = IntInterval.oneTo(3);
assertEquals(intInterval.toSet(), intSet);
}
@Test
public void testOperationsOnIntIntMap() {
MutableIntIntMap map = new IntIntHashMap();
assertEquals(5, map.addToValue(0, 5));
assertEquals(5, map.get(0));
assertEquals(3, map.getIfAbsentPut(1, 3));
}
@Test
public void whenCreateDoubleStream_thenAverageIsThree() {
DoubleStream doubleStream = DoubleLists
.mutable.with(1.0, 2.0, 3.0, 4.0, 5.0)
.primitiveStream();
assertEquals(3, doubleStream.average().getAsDouble(), 0.001);
}
@Test
public void whenCreateMapFromStream_thenValuesMustMatch() {
Iterable<Integer> integers = Interval.oneTo(3);
MutableIntIntMap map =
IntIntMaps.mutable.from(
integers,
key -> key,
value -> value * value);
MutableIntIntMap expected = IntIntMaps.mutable.empty()
.withKeyValue(1, 1)
.withKeyValue(2, 4)
.withKeyValue(3, 9);
assertEquals(expected, map);
}
}