BetterLists 1.4

This commit is contained in:
Klemek
2018-06-18 15:10:34 +02:00
parent c9f641a4a4
commit 55dcf214f5
4 changed files with 41 additions and 8 deletions
+6 -5
View File
@@ -30,7 +30,7 @@ NOTE : Please note that, unlike C# LINQ, these functions are not optimized at lo
## Download
* [betterlists-1.3.jar](../../raw/master/download/betterlists-1.3.jar)
* [betterlists-1.4.jar](../../raw/master/download/betterlists-1.4.jar)
## Maven
@@ -49,7 +49,7 @@ You can use this project as a maven dependency with this :
<dependency>
<groupId>klemek</groupId>
<artifactId>betterlists</artifactId>
<version>1.3</version>
<version>1.4</version>
</dependency>
</dependencies>
```
@@ -71,7 +71,7 @@ You can use this project as a maven dependency with this :
| [orderBy / orderByDescending](#orderby-orderbydescending) | Sorts the elements of a sequence in ascending order by using a specified comparator. (You can user orderByDescending to change the order) |
| [reverse](#reverse) | Inverts the order of the elements in the sequence. |
| [select](#select) | Projects each element of a sequence into a new form. |
| [selectMany](#selectmany) | Projects each element of a sequence into a new list and flattens the resulting sequences into one sequence. |
| [selectMany / selectManyArrays](#selectmany-selectmanyarrays) | Projects each element of a sequence into a new list and flattens the resulting sequences into one sequence. |
| [skip / skipWhile](#skip-skipwhile) | Bypasses elements in the sequence as long as a specified condition is true and then returns the remaining elements. |
| [sum](#sum) | Computes the sum of the sequence of Double values that are obtained by invoking a transform function on each element of the input sequence. |
| [take / takeWhile](#take-takewhile) | Returns a specified number of contiguous elements from the start of the sequence. |
@@ -178,12 +178,13 @@ BetterArrayList<Contact> contacts = BetterArrayList.fromList(someFunction());
BetterList<String> contactsMails = contacts.select(c -> c.getEmail());
```
### selectMany
### selectMany / selectManyArrays
Projects each element of a sequence into a new list and flattens the resulting sequences into one sequence.
```Java
BetterArrayList<Contact> contacts = BetterArrayList.fromList(someFunction());
BetterList<String> contactsMails = contacts.select(c -> c.getEmail().split(";"));
BetterList<String> contactsNumbers = contacts.selectMany(c -> c.getAllPhoneNumbers());
BetterList<String> contactsMails = contacts.selectManyArrays(c -> c.getEmail().split(";"));
```
Binary file not shown.
+17 -1
View File
@@ -307,7 +307,23 @@ public interface BetterList<T> extends List<T> {
* @return A List whose elements are the result of invoking the one-to-many
* transform function on each element of the input sequence.
*/
default <E> BetterList<E> selectMany(Function<T, E[]> selector) {
default <E> BetterList<E> selectMany(Function<T, Collection<? extends E>> selector) {
BetterList<E> out = new BetterArrayList<>();
for (T element : this)
out.addAll(selector.apply(element));
return out;
}
/**
* Projects each element of a sequence into a new list and flattens the
* resulting sequences into one sequence.
*
* @param <E> The type of the projected values lists
* @param selector - A transform function to apply to each element.
* @return A List whose elements are the result of invoking the one-to-many
* transform function on each element of the input sequence.
*/
default <E> BetterList<E> selectManyArrays(Function<T, E[]> selector) {
BetterList<E> out = new BetterArrayList<>();
for (T element : this)
out.addAll(Arrays.asList(selector.apply(element)));
@@ -5,6 +5,8 @@ import org.junit.Assert;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.NoSuchElementException;
public class BetterListsTests {
@@ -241,12 +243,26 @@ public class BetterListsTests {
}
@Test
public void testSelectMany() {
public void testSelectManyArrays() {
BetterArrayList<Dummy> bal1 = new BetterArrayList<>();
bal1.add(new Dummy(1d, "hel;lo"));
bal1.add(new Dummy(2d, "hel;lo"));
BetterArrayList<String> bal2 = (BetterArrayList<String>) bal1.selectMany(du -> du.s.split(";"));
BetterArrayList<String> bal2 = (BetterArrayList<String>) bal1.selectManyArrays(du -> du.s.split(";"));
Assert.assertEquals(4, bal2.size());
Assert.assertEquals("hel", bal2.get(0));
Assert.assertEquals("lo", bal2.get(1));
Assert.assertEquals("hel", bal2.get(2));
Assert.assertEquals("lo", bal2.get(3));
}
@Test
public void testSelectMany() {
BetterArrayList<List<String>> bal1 = new BetterArrayList<>();
bal1.add(Arrays.asList("hel","lo"));
bal1.add(Arrays.asList("hel","lo"));
BetterArrayList<String> bal2 = (BetterArrayList<String>) bal1.selectMany(du -> du);
Assert.assertEquals(4, bal2.size());
Assert.assertEquals("hel", bal2.get(0));
Assert.assertEquals("lo", bal2.get(1));