1
+ package com .baeldung ;
2
+
3
+ import org .eclipse .collections .api .list .primitive .ImmutableIntList ;
4
+ import org .eclipse .collections .api .list .primitive .MutableLongList ;
5
+ import org .eclipse .collections .api .map .primitive .MutableIntIntMap ;
6
+ import org .eclipse .collections .api .set .primitive .MutableIntSet ;
7
+ import org .eclipse .collections .impl .factory .primitive .*;
8
+ import org .eclipse .collections .impl .list .Interval ;
9
+ import org .eclipse .collections .impl .list .primitive .IntInterval ;
10
+ import org .eclipse .collections .impl .map .mutable .primitive .IntIntHashMap ;
11
+ import org .junit .Test ;
12
+
13
+ import java .util .stream .DoubleStream ;
14
+
15
+ import static org .junit .Assert .assertEquals ;
16
+
17
+
18
+ public class PrimitiveCollectionsUnitTest {
19
+
20
+ @ Test
21
+ public void whenListOfLongHasOneTwoThree_thenSumIsSix () {
22
+ MutableLongList longList = LongLists .mutable .of (1L , 2L , 3L );
23
+ assertEquals (6 , longList .sum ());
24
+ }
25
+
26
+ @ Test
27
+ public void whenListOfIntHasOneTwoThree_thenMaxIsThree () {
28
+ ImmutableIntList intList = IntLists .immutable .of (1 , 2 , 3 );
29
+ assertEquals (3 , intList .max ());
30
+ }
31
+
32
+ @ Test
33
+ public void whenConvertFromIterableToPrimitive_thenValuesAreEquals () {
34
+ Iterable <Integer > iterable = Interval .oneTo (3 );
35
+ MutableIntSet intSet = IntSets .mutable .withAll (iterable );
36
+ IntInterval intInterval = IntInterval .oneTo (3 );
37
+ assertEquals (intInterval .toSet (), intSet );
38
+ }
39
+
40
+ @ Test
41
+ public void testOperationsOnIntIntMap () {
42
+ MutableIntIntMap map = new IntIntHashMap ();
43
+ assertEquals (5 , map .addToValue (0 , 5 ));
44
+ assertEquals (5 , map .get (0 ));
45
+ assertEquals (3 , map .getIfAbsentPut (1 , 3 ));
46
+ }
47
+
48
+ @ Test
49
+ public void whenCreateDoubleStream_thenAverageIsThree () {
50
+ DoubleStream doubleStream = DoubleLists
51
+ .mutable .with (1.0 , 2.0 , 3.0 , 4.0 , 5.0 )
52
+ .primitiveStream ();
53
+ assertEquals (3 , doubleStream .average ().getAsDouble (), 0.001 );
54
+ }
55
+
56
+ @ Test
57
+ public void whenCreateMapFromStream_thenValuesMustMatch () {
58
+ Iterable <Integer > integers = Interval .oneTo (3 );
59
+ MutableIntIntMap map =
60
+ IntIntMaps .mutable .from (
61
+ integers ,
62
+ key -> key ,
63
+ value -> value * value );
64
+ MutableIntIntMap expected = IntIntMaps .mutable .empty ()
65
+ .withKeyValue (1 , 1 )
66
+ .withKeyValue (2 , 4 )
67
+ .withKeyValue (3 , 9 );
68
+ assertEquals (expected , map );
69
+ }
70
+ }
0 commit comments