Skip to content

Commit b64e63b

Browse files
committed
Adding ArrayList8.mapAndCollect() + it's test
1 parent b45e63e commit b64e63b

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

src/main/java/CodingUtils/ArrayList8.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77
import java.util.function.BinaryOperator;
88
import java.util.function.Function;
99
import java.util.function.Predicate;
10+
import java.util.stream.Collectors;
1011
import java.util.stream.Stream;
1112

1213
/*................................................................................................................................
1314
. Copyright (c)
1415
.
1516
. The ArrayList8 Class was Coded by : Alexandre BOLOT
1617
.
17-
. Last Modified : 16/03/18 08:28
18+
. Last Modified : 16/03/18 14:59
1819
.
1920
. Contact : [email protected]
2021
...............................................................................................................................*/
@@ -155,5 +156,10 @@ public <R> Stream<R> map (@NotNull Function<? super E, ? extends R> mapper)
155156
{
156157
return this.stream().map(mapper);
157158
}
159+
160+
public <R> ArrayList8<R> mapAndCollect (@NotNull Function<? super E, ? extends R> mapper)
161+
{
162+
return this.stream().map(mapper).collect(Collectors.toCollection(ArrayList8::new));
163+
}
158164
//endregion
159165
}

src/test/java/CodingUtils/ArrayList8Test.java

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
.
1919
. The ArrayList8Test Class was Coded by : Alexandre BOLOT
2020
.
21-
. Last Modified : 16/03/18 09:08
21+
. Last Modified : 16/03/18 14:59
2222
.
2323
. Contact : [email protected]
2424
...............................................................................................................................*/
@@ -723,6 +723,42 @@ public void map_Null ()
723723
}
724724
//endregion
725725

726+
//region --------------- map (x3) ------------------------
727+
@Test
728+
public void mapAndCollect_Right ()
729+
{
730+
for (int i = 0; i < 2000; i++)
731+
{
732+
ArrayList8<TestObject> list1 = new ArrayList8<TestObject>()
733+
{{
734+
IntStream.range(0, randDelta(10, 5)).forEach(i -> add(randTestObject()));
735+
}};
736+
737+
ArrayList8<Integer> collect = list1.mapAndCollect(testObject -> testObject.val1);
738+
739+
IntStream.range(0, list1.size()).forEach(index -> assertEquals(list1.get(index).val1, collect.get(index), 0.0001));
740+
}
741+
}
742+
743+
@Test
744+
public void mapAndCollect_Empty ()
745+
{
746+
ArrayList8<TestObject> list1 = new ArrayList8<>();
747+
748+
ArrayList8<Integer> collect = list1.mapAndCollect(testObject -> testObject.val1);
749+
750+
IntStream.range(0, list1.size()).forEach(i -> assertEquals(list1.get(i).val1, collect.get(i), 0.0001));
751+
}
752+
753+
@Test (expected = IllegalArgumentException.class)
754+
public void mapAndCollect_Null ()
755+
{
756+
ArrayList8<TestObject> list1 = new ArrayList8<>();
757+
758+
list1.mapAndCollect(null);
759+
}
760+
//endregion
761+
726762
//region --------------- OtherMethods --------------------
727763
@NotNull
728764
private TestObject randTestObject ()

0 commit comments

Comments
 (0)