diff --git a/core-java/collectionops/src/main/java/io/pratik/CollectionHelper.java b/core-java/collectionops/src/main/java/io/pratik/CollectionHelper.java index ec551ef..62357c0 100644 --- a/core-java/collectionops/src/main/java/io/pratik/CollectionHelper.java +++ b/core-java/collectionops/src/main/java/io/pratik/CollectionHelper.java @@ -4,14 +4,19 @@ package io.pratik; import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; import java.util.HashSet; +import java.util.LinkedHashSet; import java.util.List; import java.util.Set; +import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; import java.util.stream.IntStream; import java.util.stream.Stream; +import com.google.common.collect.Lists; + /** * @author pratikdas * @@ -22,6 +27,7 @@ public class CollectionHelper { // determine the endpoints to use in `list.subList()` method int[] endpoints = {0, (listToSplit.size() + 1)/2, listToSplit.size()}; + List> sublists = IntStream.rangeClosed(0, 1) .mapToObj(i -> listToSplit.subList(endpoints[i], endpoints[i + 1])) @@ -49,7 +55,7 @@ public class CollectionHelper { } public List union(final List collA, final List collB){ - Set set = new HashSet<>(); + Set set = new LinkedHashSet<>(); set.addAll(collA); set.addAll(collB); @@ -70,6 +76,49 @@ public class CollectionHelper { } + public Collection> partition(final List collA, final int chunkSize){ + final AtomicInteger counter = new AtomicInteger(); + + final Collection> result = collA.stream() + .collect(Collectors.groupingBy(it -> counter.getAndIncrement() / chunkSize)) + .values(); + + return result; + + } + + + public List removeDuplicates(final List collA){ + List listWithoutDuplicates = new ArrayList<>( + new LinkedHashSet<>(collA)); + + return listWithoutDuplicates; + } + + public List xor(final List collA, final List collB){ + + List listOfAnotInB = collA.stream().filter(element->{ + return !collB.contains(element); + }).collect(Collectors.toList()); + + List listOfBnotInA = collB.stream().filter(element->{ + return !collA.contains(element); + }).collect(Collectors.toList()); + + return Stream.concat(listOfAnotInB.stream(), + listOfBnotInA.stream()) + .collect(Collectors.toList()); + } + + public List not(final List collA, final List collB){ + + List notList = collA.stream().filter(element->{ + return !collB.contains(element); + }).collect(Collectors.toList()); + + return notList; + } + public List subtract(final List collA, final List collB){ List intersectElements = intersection(collA,collB); diff --git a/core-java/collectionops/src/test/java/io/pratik/tests/CollectionHelperTest.java b/core-java/collectionops/src/test/java/io/pratik/tests/CollectionHelperTest.java index 4c2bda8..a906d4e 100644 --- a/core-java/collectionops/src/test/java/io/pratik/tests/CollectionHelperTest.java +++ b/core-java/collectionops/src/test/java/io/pratik/tests/CollectionHelperTest.java @@ -3,6 +3,10 @@ */ package io.pratik.tests; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import java.util.Collection; +import java.util.Iterator; import java.util.List; import org.junit.jupiter.api.AfterEach; @@ -38,12 +42,12 @@ class CollectionHelperTest { @Test void testUnion() { List union = collectionHelper.union( - List.of(9,8,5,4,7), - List.of(1,3,99,4,7)); + List.of(9, 8, 5, 4, 7), + List.of(1, 3, 99, 4, 7)); Assertions.assertArrayEquals( - List.of(1, 3, 99, 4, 5, 7, 8, 9).toArray(), + List.of(9, 8, 5, 4, 7, 1, 3, 99).toArray(), union.toArray()); } @@ -59,6 +63,28 @@ class CollectionHelperTest { intersection.toArray()); } + @Test + void testXOR() { + List xorList = collectionHelper.xor( + List.of(9, 8, 5, 4, 7), + List.of(1, 99, 4, 7)); + + Assertions.assertArrayEquals( + List.of(9, 8, 5, 1, 99).toArray(), + xorList.toArray()); + } + + @Test + void testNOT() { + List xorList = collectionHelper.not( + List.of(9,8,5,4,7), + List.of(1,99,4,7)); + + Assertions.assertArrayEquals( + List.of(9,8,5).toArray(), + xorList.toArray()); + } + @Test void testAddition() { List sub = collectionHelper.add( @@ -96,10 +122,48 @@ class CollectionHelperTest { sub.toArray()); } + @Test + void testPartition() { + Collection> partitions = collectionHelper.partition( + List.of(9, 8, 5, 4, 7, 15, 15), 2); + + Iterator> iter = partitions.iterator(); + + int loop = 0; + while(iter.hasNext()) { + List element = iter.next(); + System.out.println(element); + if(loop == 0) + assertArrayEquals(List.of(9, 8).toArray(),element.toArray()); + else if(loop == 1) + assertArrayEquals(List.of(5, 4).toArray(),element.toArray()); + else if(loop == 2) + assertArrayEquals(List.of(7, 15).toArray(),element.toArray()); + else if(loop == 3) + assertArrayEquals(List.of(15).toArray(),element.toArray()); + + ++loop; + } + + + } + + @Test + void testRemoveDuplicates() { + List uniqueElements = collectionHelper.removeDuplicates( + List.of(9, 8, 5, 4, 4, 7, 15, 15)); + + + + Assertions.assertArrayEquals( + List.of(9, 8, 5, 4, 7, 15).toArray(), + uniqueElements.toArray()); + } + @Test void testSplit() { List[] subLists = collectionHelper.split( - List.of(9,8,5,4,7, 15, 15)); + List.of(9, 8, 5, 4, 7, 15, 15)); Assertions.assertArrayEquals( diff --git a/spring-boot/spring-boot-elasticsearch/target/productsearchapp-0.0.1-SNAPSHOT.jar b/spring-boot/spring-boot-elasticsearch/target/productsearchapp-0.0.1-SNAPSHOT.jar deleted file mode 100644 index 3a5d5c6..0000000 Binary files a/spring-boot/spring-boot-elasticsearch/target/productsearchapp-0.0.1-SNAPSHOT.jar and /dev/null differ