Skip to content

Commit

Permalink
Adding ArrayList8.mapAndCollect() + it's test
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexBolot committed Mar 16, 2018
1 parent b45e63e commit b64e63b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/main/java/CodingUtils/ArrayList8.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/*................................................................................................................................
. Copyright (c)
.
. The ArrayList8 Class was Coded by : Alexandre BOLOT
.
. Last Modified : 16/03/18 08:28
. Last Modified : 16/03/18 14:59
.
. Contact : bolotalex06@gmail.com
...............................................................................................................................*/
Expand Down Expand Up @@ -155,5 +156,10 @@ public <R> Stream<R> map (@NotNull Function<? super E, ? extends R> mapper)
{
return this.stream().map(mapper);
}

public <R> ArrayList8<R> mapAndCollect (@NotNull Function<? super E, ? extends R> mapper)
{
return this.stream().map(mapper).collect(Collectors.toCollection(ArrayList8::new));
}
//endregion
}
38 changes: 37 additions & 1 deletion src/test/java/CodingUtils/ArrayList8Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
.
. The ArrayList8Test Class was Coded by : Alexandre BOLOT
.
. Last Modified : 16/03/18 09:08
. Last Modified : 16/03/18 14:59
.
. Contact : bolotalex06@gmail.com
...............................................................................................................................*/
Expand Down Expand Up @@ -723,6 +723,42 @@ public void map_Null ()
}
//endregion

//region --------------- map (x3) ------------------------
@Test
public void mapAndCollect_Right ()
{
for (int i = 0; i < 2000; i++)
{
ArrayList8<TestObject> list1 = new ArrayList8<TestObject>()
{{
IntStream.range(0, randDelta(10, 5)).forEach(i -> add(randTestObject()));
}};

ArrayList8<Integer> collect = list1.mapAndCollect(testObject -> testObject.val1);

IntStream.range(0, list1.size()).forEach(index -> assertEquals(list1.get(index).val1, collect.get(index), 0.0001));
}
}

@Test
public void mapAndCollect_Empty ()
{
ArrayList8<TestObject> list1 = new ArrayList8<>();

ArrayList8<Integer> collect = list1.mapAndCollect(testObject -> testObject.val1);

IntStream.range(0, list1.size()).forEach(i -> assertEquals(list1.get(i).val1, collect.get(i), 0.0001));
}

@Test (expected = IllegalArgumentException.class)
public void mapAndCollect_Null ()
{
ArrayList8<TestObject> list1 = new ArrayList8<>();

list1.mapAndCollect(null);
}
//endregion

//region --------------- OtherMethods --------------------
@NotNull
private TestObject randTestObject ()
Expand Down

0 comments on commit b64e63b

Please sign in to comment.