Skip to content

Commit 6027544

Browse files
author
Pratik Das
committed
added more methods
1 parent 4f7a266 commit 6027544

File tree

3 files changed

+118
-5
lines changed

3 files changed

+118
-5
lines changed

core-java/collectionops/src/main/java/io/pratik/CollectionHelper.java

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,19 @@
44
package io.pratik;
55

66
import java.util.ArrayList;
7+
import java.util.Collection;
78
import java.util.Collections;
89
import java.util.HashSet;
10+
import java.util.LinkedHashSet;
911
import java.util.List;
1012
import java.util.Set;
13+
import java.util.concurrent.atomic.AtomicInteger;
1114
import java.util.stream.Collectors;
1215
import java.util.stream.IntStream;
1316
import java.util.stream.Stream;
1417

18+
import com.google.common.collect.Lists;
19+
1520
/**
1621
* @author pratikdas
1722
*
@@ -22,6 +27,7 @@ public <T> List<T>[] split(List<T> listToSplit){
2227
// determine the endpoints to use in `list.subList()` method
2328
int[] endpoints = {0, (listToSplit.size() + 1)/2, listToSplit.size()};
2429

30+
2531
List<List<T>> sublists =
2632
IntStream.rangeClosed(0, 1)
2733
.mapToObj(i -> listToSplit.subList(endpoints[i], endpoints[i + 1]))
@@ -49,7 +55,7 @@ public List<Integer> addWithFilter(final List<Integer> collA, final List<Integer
4955
}
5056

5157
public List<Integer> union(final List<Integer> collA, final List<Integer> collB){
52-
Set<Integer> set = new HashSet<>();
58+
Set<Integer> set = new LinkedHashSet<>();
5359
set.addAll(collA);
5460
set.addAll(collB);
5561

@@ -70,6 +76,49 @@ public List<Integer> intersection(final List<Integer> collA, final List<Integer>
7076

7177
}
7278

79+
public Collection<List<Integer>> partition(final List<Integer> collA, final int chunkSize){
80+
final AtomicInteger counter = new AtomicInteger();
81+
82+
final Collection<List<Integer>> result = collA.stream()
83+
.collect(Collectors.groupingBy(it -> counter.getAndIncrement() / chunkSize))
84+
.values();
85+
86+
return result;
87+
88+
}
89+
90+
91+
public List<Integer> removeDuplicates(final List<Integer> collA){
92+
List<Integer> listWithoutDuplicates = new ArrayList<>(
93+
new LinkedHashSet<>(collA));
94+
95+
return listWithoutDuplicates;
96+
}
97+
98+
public List<Integer> xor(final List<Integer> collA, final List<Integer> collB){
99+
100+
List<Integer> listOfAnotInB = collA.stream().filter(element->{
101+
return !collB.contains(element);
102+
}).collect(Collectors.toList());
103+
104+
List<Integer> listOfBnotInA = collB.stream().filter(element->{
105+
return !collA.contains(element);
106+
}).collect(Collectors.toList());
107+
108+
return Stream.concat(listOfAnotInB.stream(),
109+
listOfBnotInA.stream())
110+
.collect(Collectors.toList());
111+
}
112+
113+
public List<Integer> not(final List<Integer> collA, final List<Integer> collB){
114+
115+
List<Integer> notList = collA.stream().filter(element->{
116+
return !collB.contains(element);
117+
}).collect(Collectors.toList());
118+
119+
return notList;
120+
}
121+
73122
public List<Integer> subtract(final List<Integer> collA, final List<Integer> collB){
74123
List<Integer> intersectElements = intersection(collA,collB);
75124

core-java/collectionops/src/test/java/io/pratik/tests/CollectionHelperTest.java

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
*/
44
package io.pratik.tests;
55

6+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
7+
8+
import java.util.Collection;
9+
import java.util.Iterator;
610
import java.util.List;
711

812
import org.junit.jupiter.api.AfterEach;
@@ -38,12 +42,12 @@ void tearDown() throws Exception {
3842
@Test
3943
void testUnion() {
4044
List<Integer> union = collectionHelper.union(
41-
List.of(9,8,5,4,7),
42-
List.of(1,3,99,4,7));
45+
List.of(9, 8, 5, 4, 7),
46+
List.of(1, 3, 99, 4, 7));
4347

4448

4549
Assertions.assertArrayEquals(
46-
List.of(1, 3, 99, 4, 5, 7, 8, 9).toArray(),
50+
List.of(9, 8, 5, 4, 7, 1, 3, 99).toArray(),
4751
union.toArray());
4852

4953
}
@@ -59,6 +63,28 @@ void testIntersection() {
5963
intersection.toArray());
6064
}
6165

66+
@Test
67+
void testXOR() {
68+
List<Integer> xorList = collectionHelper.xor(
69+
List.of(9, 8, 5, 4, 7),
70+
List.of(1, 99, 4, 7));
71+
72+
Assertions.assertArrayEquals(
73+
List.of(9, 8, 5, 1, 99).toArray(),
74+
xorList.toArray());
75+
}
76+
77+
@Test
78+
void testNOT() {
79+
List<Integer> xorList = collectionHelper.not(
80+
List.of(9,8,5,4,7),
81+
List.of(1,99,4,7));
82+
83+
Assertions.assertArrayEquals(
84+
List.of(9,8,5).toArray(),
85+
xorList.toArray());
86+
}
87+
6288
@Test
6389
void testAddition() {
6490
List<Integer> sub = collectionHelper.add(
@@ -96,10 +122,48 @@ void testSubtraction() {
96122
sub.toArray());
97123
}
98124

125+
@Test
126+
void testPartition() {
127+
Collection<List<Integer>> partitions = collectionHelper.partition(
128+
List.of(9, 8, 5, 4, 7, 15, 15), 2);
129+
130+
Iterator<List<Integer>> iter = partitions.iterator();
131+
132+
int loop = 0;
133+
while(iter.hasNext()) {
134+
List<Integer> element = iter.next();
135+
System.out.println(element);
136+
if(loop == 0)
137+
assertArrayEquals(List.of(9, 8).toArray(),element.toArray());
138+
else if(loop == 1)
139+
assertArrayEquals(List.of(5, 4).toArray(),element.toArray());
140+
else if(loop == 2)
141+
assertArrayEquals(List.of(7, 15).toArray(),element.toArray());
142+
else if(loop == 3)
143+
assertArrayEquals(List.of(15).toArray(),element.toArray());
144+
145+
++loop;
146+
}
147+
148+
149+
}
150+
151+
@Test
152+
void testRemoveDuplicates() {
153+
List<Integer> uniqueElements = collectionHelper.removeDuplicates(
154+
List.of(9, 8, 5, 4, 4, 7, 15, 15));
155+
156+
157+
158+
Assertions.assertArrayEquals(
159+
List.of(9, 8, 5, 4, 7, 15).toArray(),
160+
uniqueElements.toArray());
161+
}
162+
99163
@Test
100164
void testSplit() {
101165
List<Integer>[] subLists = collectionHelper.split(
102-
List.of(9,8,5,4,7, 15, 15));
166+
List.of(9, 8, 5, 4, 7, 15, 15));
103167

104168

105169
Assertions.assertArrayEquals(
Binary file not shown.

0 commit comments

Comments
 (0)