added more methods

This commit is contained in:
Pratik Das
2022-01-12 05:07:48 +05:30
parent 4f7a266d5b
commit 60275444ea
3 changed files with 118 additions and 5 deletions

View File

@@ -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<List<T>> sublists =
IntStream.rangeClosed(0, 1)
.mapToObj(i -> listToSplit.subList(endpoints[i], endpoints[i + 1]))
@@ -49,7 +55,7 @@ public class CollectionHelper {
}
public List<Integer> union(final List<Integer> collA, final List<Integer> collB){
Set<Integer> set = new HashSet<>();
Set<Integer> set = new LinkedHashSet<>();
set.addAll(collA);
set.addAll(collB);
@@ -70,6 +76,49 @@ public class CollectionHelper {
}
public Collection<List<Integer>> partition(final List<Integer> collA, final int chunkSize){
final AtomicInteger counter = new AtomicInteger();
final Collection<List<Integer>> result = collA.stream()
.collect(Collectors.groupingBy(it -> counter.getAndIncrement() / chunkSize))
.values();
return result;
}
public List<Integer> removeDuplicates(final List<Integer> collA){
List<Integer> listWithoutDuplicates = new ArrayList<>(
new LinkedHashSet<>(collA));
return listWithoutDuplicates;
}
public List<Integer> xor(final List<Integer> collA, final List<Integer> collB){
List<Integer> listOfAnotInB = collA.stream().filter(element->{
return !collB.contains(element);
}).collect(Collectors.toList());
List<Integer> listOfBnotInA = collB.stream().filter(element->{
return !collA.contains(element);
}).collect(Collectors.toList());
return Stream.concat(listOfAnotInB.stream(),
listOfBnotInA.stream())
.collect(Collectors.toList());
}
public List<Integer> not(final List<Integer> collA, final List<Integer> collB){
List<Integer> notList = collA.stream().filter(element->{
return !collB.contains(element);
}).collect(Collectors.toList());
return notList;
}
public List<Integer> subtract(final List<Integer> collA, final List<Integer> collB){
List<Integer> intersectElements = intersection(collA,collB);

View File

@@ -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<Integer> 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<Integer> 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<Integer> 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<Integer> sub = collectionHelper.add(
@@ -96,10 +122,48 @@ class CollectionHelperTest {
sub.toArray());
}
@Test
void testPartition() {
Collection<List<Integer>> partitions = collectionHelper.partition(
List.of(9, 8, 5, 4, 7, 15, 15), 2);
Iterator<List<Integer>> iter = partitions.iterator();
int loop = 0;
while(iter.hasNext()) {
List<Integer> 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<Integer> 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<Integer>[] subLists = collectionHelper.split(
List.of(9,8,5,4,7, 15, 15));
List.of(9, 8, 5, 4, 7, 15, 15));
Assertions.assertArrayEquals(