Skip to content

Commit b6830cc

Browse files
SeunMattzhendrikse
authored andcommitted
Updated Vavr Collections Example Code (eugenp#2550)
* added updated example codes * updated example code StringToCharStream * deleted StringToCharStream.java locally * removed redundant file * added code for apache commons collection SetUtils * refactored example code * added example code for bytebuddy * added example code for PCollections * update pom * refactored tests for PCollections * spring security xml config * spring security xml config * remove redundant comment * example code for apache-shiro * updated example code for Vavr Collections * updated Vavr's Collection example * updated Vavr Collection file * updated example code for Apache Shiro * updated Vavr Collections example
1 parent 3c4faf9 commit b6830cc

1 file changed

Lines changed: 58 additions & 9 deletions

File tree

vavr/src/test/java/com/baeldung/vavr/collections/CollectionAPIUnitTest.java

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
import io.vavr.Tuple;
44
import io.vavr.Tuple2;
5-
import io.vavr.collection.Array;
6-
import io.vavr.collection.CharSeq;
5+
import io.vavr.collection.*;
6+
import io.vavr.collection.BitSet;
7+
import io.vavr.collection.HashMap;
78
import io.vavr.collection.HashSet;
89
import io.vavr.collection.Iterator;
910
import io.vavr.collection.List;
@@ -12,13 +13,12 @@
1213
import io.vavr.collection.Set;
1314
import io.vavr.collection.SortedMap;
1415
import io.vavr.collection.SortedSet;
15-
import io.vavr.collection.Stream;
1616
import io.vavr.collection.TreeMap;
1717
import io.vavr.collection.TreeSet;
1818
import io.vavr.collection.Vector;
1919
import org.junit.Test;
2020

21-
import java.util.Comparator;
21+
import java.util.*;
2222
import java.util.stream.Collectors;
2323
import java.util.stream.IntStream;
2424

@@ -124,6 +124,9 @@ public void givenQueue_whenEnqueued_thenCorrect() {
124124

125125
Queue<Integer> tailQueue = result._2;
126126
assertFalse(tailQueue.contains(secondQueue.get(0)));
127+
128+
Queue<Queue<Integer>> queue1 = queue.combinations(2);
129+
assertEquals(queue1.get(2).toCharSeq(), CharSeq.of("23"));
127130
}
128131

129132
@Test
@@ -153,17 +156,23 @@ public void givenStream_whenProcessed_thenCorrect() {
153156

154157
@Test
155158
public void givenArray_whenQueried_thenCorrect() {
159+
160+
Array<Integer> rArray = Array.range(1, 5);
161+
assertFalse(rArray.contains(5));
162+
163+
Array<Integer> rArray2 = Array.rangeClosed(1, 5);
164+
assertTrue(rArray2.contains(5));
165+
166+
Array<Integer> rArray3 = Array.rangeClosedBy(1,6,2);
167+
assertEquals(rArray3.size(), 3);
168+
156169
Array<Integer> intArray = Array.of(1, 2, 3);
157170
Array<Integer> newArray = intArray.removeAt(1);
158-
159-
assertEquals(3, intArray.size());
160171
assertEquals(2, newArray.size());
161172
assertEquals(3, newArray.get(1).intValue());
162173

163174
Array<Integer> array2 = intArray.replace(1, 5);
164175
assertEquals(array2.get(0).intValue(), 5);
165-
166-
167176
}
168177

169178
@Test
@@ -199,6 +208,15 @@ public void givenHashSet_whenModified_thenCorrect() {
199208
assertEquals(3, set.size());
200209
assertEquals(4, newSet.size());
201210
assertTrue(newSet.contains("Yellow"));
211+
212+
HashSet<Integer> set0 = HashSet.rangeClosed(1,5);
213+
HashSet<Integer> set1 = HashSet.rangeClosed(3, 6);
214+
215+
assertEquals(set0.union(set1), HashSet.rangeClosed(1,6));
216+
assertEquals(set0.diff(set1), HashSet.rangeClosed(1,2));
217+
assertEquals(set0.intersect(set1), HashSet.rangeClosed(3,5));
218+
219+
202220
}
203221

204222
@Test
@@ -222,13 +240,39 @@ public void givenSortedSet_whenReversed_thenCorrect() {
222240
}
223241

224242
@Test
225-
public void givenMap_whenIterated_thenCorrect() {
243+
public void giveBitSet_whenApiMethods_thenCorrect() {
244+
245+
BitSet<Integer> bitSet = BitSet.of(1,2,3,4,5,6,7,8);
246+
247+
BitSet<Integer> bitSet1 = bitSet.takeUntil(i -> i > 4);
248+
assertEquals(bitSet1.size(), 4);
249+
250+
}
251+
252+
@Test
253+
public void givenMap_whenMapApi_thenCorrect() {
226254
Map<Integer, List<Integer>> map = List.rangeClosed(0, 10)
227255
.groupBy(i -> i % 2);
228256

229257
assertEquals(2, map.size());
230258
assertEquals(6, map.get(0).get().size());
231259
assertEquals(5, map.get(1).get().size());
260+
261+
Map<String, String> map1
262+
= HashMap.of("key1", "val1", "key2", "val2", "key3", "val3");
263+
264+
Map<String, String> fMap
265+
= map1.filterKeys(k -> k.contains("1") || k.contains("2"));
266+
assertFalse(fMap.containsKey("key3"));
267+
268+
Map<String, String> fMap2
269+
= map1.filterValues(v -> v.contains("3"));
270+
assertEquals(fMap2.size(), 1);
271+
assertTrue(fMap2.containsValue("val3"));
272+
273+
Map<String, Integer> map2 = map1.map(
274+
(k, v) -> Tuple.of(k, Integer.valueOf(v.charAt(v.length() - 1) + "") ));
275+
assertEquals(map2.get("key1").get().intValue(), 1);
232276
}
233277

234278
@Test
@@ -238,6 +282,11 @@ public void givenTreeMap_whenIterated_thenCorrect() {
238282

239283
assertEquals(1, map.keySet().toJavaArray()[0]);
240284
assertEquals("Four", map.get(4).get());
285+
286+
TreeMap<Integer, String> treeMap2 =
287+
TreeMap.of(Comparator.reverseOrder(), 3,"three", 6, "six", 1, "one");
288+
assertEquals(treeMap2.keySet().mkString(), "631");
289+
241290
}
242291

243292
@Test

0 commit comments

Comments
 (0)