Search in sources :

Example 1 with List

use of io.vavr.collection.List in project tutorials by eugenp.

the class CollectionAPIUnitTest method givenParams_whenListAPI_thenCorrect.

@Test
public void givenParams_whenListAPI_thenCorrect() {
    List<String> list = List.of("Java", "PHP", "Jquery", "JavaScript", "JShell", "JAVA");
    List list1 = list.drop(2);
    assertFalse(list1.contains("Java") && list1.contains("PHP"));
    List list2 = list.dropRight(2);
    assertFalse(list2.contains("JAVA") && list2.contains("JShell"));
    List list3 = list.dropUntil(s -> s.contains("Shell"));
    assertEquals(list3.size(), 2);
    List list4 = list.dropWhile(s -> s.length() > 0);
    assertTrue(list4.isEmpty());
    List list5 = list.take(1);
    assertEquals(list5.single(), "Java");
    List list6 = list.takeRight(1);
    assertEquals(list6.single(), "JAVA");
    List list7 = list.takeUntil(s -> s.length() > 6);
    assertEquals(list7.size(), 3);
    List list8 = list.distinctBy((s1, s2) -> s1.startsWith(s2.charAt(0) + "") ? 0 : 1);
    assertEquals(list8.size(), 2);
    Iterator<List<String>> iterator = list.grouped(2);
    assertEquals(iterator.head().size(), 2);
    Map<Boolean, List<String>> map = list.groupBy(e -> e.startsWith("J"));
    assertEquals(map.size(), 2);
    assertEquals(map.get(false).get().size(), 1);
    assertEquals(map.get(true).get().size(), 5);
    String words = List.of("Boys", "Girls").intersperse("and").reduce((s1, s2) -> s1.concat(" " + s2)).trim();
    assertEquals(words, "Boys and Girls");
}
Also used : IntStream(java.util.stream.IntStream) java.util(java.util) Queue(io.vavr.collection.Queue) Assert.assertNotSame(org.junit.Assert.assertNotSame) BitSet(io.vavr.collection.BitSet) SortedMap(io.vavr.collection.SortedMap) TreeMap(io.vavr.collection.TreeMap) List(io.vavr.collection.List) io.vavr.collection(io.vavr.collection) Assert.assertSame(org.junit.Assert.assertSame) SortedSet(io.vavr.collection.SortedSet) TreeSet(io.vavr.collection.TreeSet) HashSet(io.vavr.collection.HashSet) Iterator(io.vavr.collection.Iterator) Vector(io.vavr.collection.Vector) Tuple(io.vavr.Tuple) Set(io.vavr.collection.Set) HashMap(io.vavr.collection.HashMap) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Collectors(java.util.stream.Collectors) Tuple2(io.vavr.Tuple2) Assert.assertFalse(org.junit.Assert.assertFalse) Map(io.vavr.collection.Map) Assert.assertEquals(org.junit.Assert.assertEquals) List(io.vavr.collection.List) Test(org.junit.Test)

Example 2 with List

use of io.vavr.collection.List in project tutorials by eugenp.

the class CollectionAPIUnitTest method givenEmptyList_whenStackAPI_thenCorrect.

@Test
public void givenEmptyList_whenStackAPI_thenCorrect() {
    List<Integer> intList = List.empty();
    List<Integer> intList1 = intList.pushAll(List.rangeClosed(5, 10));
    assertEquals(intList1.peek(), Integer.valueOf(10));
    List intList2 = intList1.pop();
    assertEquals(intList2.size(), (intList1.size() - 1));
}
Also used : List(io.vavr.collection.List) Test(org.junit.Test)

Aggregations

List (io.vavr.collection.List)2 Test (org.junit.Test)2 Tuple (io.vavr.Tuple)1 Tuple2 (io.vavr.Tuple2)1 io.vavr.collection (io.vavr.collection)1 BitSet (io.vavr.collection.BitSet)1 HashMap (io.vavr.collection.HashMap)1 HashSet (io.vavr.collection.HashSet)1 Iterator (io.vavr.collection.Iterator)1 Map (io.vavr.collection.Map)1 Queue (io.vavr.collection.Queue)1 Set (io.vavr.collection.Set)1 SortedMap (io.vavr.collection.SortedMap)1 SortedSet (io.vavr.collection.SortedSet)1 TreeMap (io.vavr.collection.TreeMap)1 TreeSet (io.vavr.collection.TreeSet)1 Vector (io.vavr.collection.Vector)1 java.util (java.util)1 Collectors (java.util.stream.Collectors)1 IntStream (java.util.stream.IntStream)1