Bael-422 Vavr Collections (#2304)

* BAEL-422 Examples for tutorial on Vavr Collection API

* Fixed as per review comments
This commit is contained in:
Naresh Babu P B 2017-07-22 19:08:37 +05:30 committed by Zeger Hendrikse
parent ee6656a744
commit 226acd5a19
1 changed files with 57 additions and 38 deletions

View File

@ -1,5 +1,18 @@
package com.baeldung.vavr.collections; package com.baeldung.vavr.collections;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import java.util.Comparator;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.junit.Test;
import io.vavr.Tuple;
import io.vavr.Tuple2; import io.vavr.Tuple2;
import io.vavr.collection.Array; import io.vavr.collection.Array;
import io.vavr.collection.CharSeq; import io.vavr.collection.CharSeq;
@ -15,16 +28,6 @@ import io.vavr.collection.Stream;
import io.vavr.collection.TreeMap; import io.vavr.collection.TreeMap;
import io.vavr.collection.TreeSet; import io.vavr.collection.TreeSet;
import io.vavr.collection.Vector; import io.vavr.collection.Vector;
import org.junit.Test;
import java.util.Comparator;
import java.util.stream.Collectors;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
public class CollectionAPIUnitTest { public class CollectionAPIUnitTest {
@ -66,28 +69,16 @@ public class CollectionAPIUnitTest {
assertEquals(3, queue.size()); assertEquals(3, queue.size());
assertEquals(5, secondQueue.size()); assertEquals(5, secondQueue.size());
secondQueue.dequeue() Tuple2<Integer, Queue<Integer>> result = secondQueue.dequeue();
.map((k, v) -> { Integer headValue = result.apply((head, tail) -> head);
assertEquals(new Integer(1), k); assertEquals(new Integer(1), headValue);
return v.dequeue();
}) Iterator<Integer> iterator = result.apply((head, tail) -> tail.iterator());
.map((k, v) -> {
assertEquals(new Integer(2), k); assertEquals(new Integer(2), iterator.next());
return v.dequeue(); assertEquals(new Integer(3), iterator.next());
}) assertEquals(new Integer(4), iterator.next());
.map((k, v) -> { assertEquals(new Integer(5), iterator.next());
assertEquals(new Integer(3), k);
return v.dequeue();
})
.map((k, v) -> {
assertEquals(new Integer(4), k);
return v.dequeue();
})
.map((k, v) -> {
assertEquals(new Integer(5), k);
assertTrue(v.isEmpty());
return null;
});
} }
@Test @Test
@ -101,7 +92,7 @@ public class CollectionAPIUnitTest {
.sum() .sum()
.longValue(); .longValue();
assertEquals(20L, evenSum); assertEquals(20, evenSum);
assertEquals(new Integer(5), intStream.get(5)); assertEquals(new Integer(5), intStream.get(5));
} }
@ -169,8 +160,7 @@ public class CollectionAPIUnitTest {
@Test @Test
public void givenSortedSet_whenReversed_thenCorrect() { public void givenSortedSet_whenReversed_thenCorrect() {
Comparator<String> reverseCompare = Comparator.reverseOrder(); SortedSet<String> set = TreeSet.of(Comparator.reverseOrder(), "Green", "Red", "Blue");
SortedSet<String> set = TreeSet.of(reverseCompare, "Green", "Red", "Blue");
Iterator<String> iterator = set.iterator(); Iterator<String> iterator = set.iterator();
assertEquals("Red", iterator.next()); assertEquals("Red", iterator.next());
@ -212,18 +202,47 @@ public class CollectionAPIUnitTest {
java.util.List<Integer> javaList = java.util.Arrays.asList(1, 2, 3, 4); java.util.List<Integer> javaList = java.util.Arrays.asList(1, 2, 3, 4);
List<Integer> vavrList = List.ofAll(javaList); List<Integer> vavrList = List.ofAll(javaList);
assertTrue(vavrList instanceof io.vavr.collection.List); assertEquals(4, vavrList.size());
assertEquals(new Integer(1), vavrList.head());
java.util.stream.Stream<Integer> javaStream = javaList.stream(); java.util.stream.Stream<Integer> javaStream = javaList.stream();
Set<Integer> vavrSet = HashSet.ofAll(javaStream); Set<Integer> vavrSet = HashSet.ofAll(javaStream);
assertTrue(vavrSet instanceof io.vavr.collection.Set); assertEquals(4, vavrSet.size());
assertEquals(new Integer(2), vavrSet.tail()
.head());
}
@Test
public void givenJavaStream_whenCollected_thenCorrect() {
List<Integer> vavrList = IntStream.range(1, 10)
.boxed()
.filter(i -> i % 2 == 0)
.collect(List.collector());
assertEquals(4, vavrList.size());
assertEquals(new Integer(2), vavrList.head());
} }
@Test @Test
public void givenVavrList_whenConverted_thenCorrect() { public void givenVavrList_whenConverted_thenCorrect() {
java.util.Set<Integer> collect = List.of(1, 2, 3) Integer[] array = List.of(1, 2, 3)
.toJavaArray(Integer.class);
assertEquals(3, array.length);
java.util.Map<String, Integer> map = List.of("1", "2", "3")
.toJavaMap(i -> Tuple.of(i, Integer.valueOf(i)));
assertEquals(new Integer(2), map.get("2"));
}
@Test
public void givenVavrList_whenCollected_thenCorrect() {
java.util.Set<Integer> javaSet = List.of(1, 2, 3)
.collect(Collectors.toSet()); .collect(Collectors.toSet());
assertEquals(3, javaSet.size());
assertEquals(new Integer(1), javaSet.iterator()
.next());
} }
@Test @Test