diff --git a/README.md b/README.md index 55a4322..f7ed4d9 100644 --- a/README.md +++ b/README.md @@ -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 : klemek betterlists - 1.3 + 1.4 ``` @@ -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 contacts = BetterArrayList.fromList(someFunction()); BetterList 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 contacts = BetterArrayList.fromList(someFunction()); -BetterList contactsMails = contacts.select(c -> c.getEmail().split(";")); +BetterList contactsNumbers = contacts.selectMany(c -> c.getAllPhoneNumbers()); +BetterList contactsMails = contacts.selectManyArrays(c -> c.getEmail().split(";")); ``` diff --git a/download/betterlists-1.4.jar b/download/betterlists-1.4.jar new file mode 100644 index 0000000..3630d44 Binary files /dev/null and b/download/betterlists-1.4.jar differ diff --git a/src/fr/klemek/betterlists/BetterList.java b/src/fr/klemek/betterlists/BetterList.java index 6940787..3da76dc 100644 --- a/src/fr/klemek/betterlists/BetterList.java +++ b/src/fr/klemek/betterlists/BetterList.java @@ -307,7 +307,23 @@ public interface BetterList extends List { * @return A List whose elements are the result of invoking the one-to-many * transform function on each element of the input sequence. */ - default BetterList selectMany(Function selector) { + default BetterList selectMany(Function> selector) { + BetterList 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 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 BetterList selectManyArrays(Function selector) { BetterList out = new BetterArrayList<>(); for (T element : this) out.addAll(Arrays.asList(selector.apply(element))); diff --git a/test/fr/klemek/betterlists/test/BetterListsTests.java b/test/fr/klemek/betterlists/test/BetterListsTests.java index e9b517d..72e1f01 100644 --- a/test/fr/klemek/betterlists/test/BetterListsTests.java +++ b/test/fr/klemek/betterlists/test/BetterListsTests.java @@ -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 bal1 = new BetterArrayList<>(); bal1.add(new Dummy(1d, "hel;lo")); bal1.add(new Dummy(2d, "hel;lo")); - BetterArrayList bal2 = (BetterArrayList) bal1.selectMany(du -> du.s.split(";")); + BetterArrayList bal2 = (BetterArrayList) 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> bal1 = new BetterArrayList<>(); + bal1.add(Arrays.asList("hel","lo")); + bal1.add(Arrays.asList("hel","lo")); + + BetterArrayList bal2 = (BetterArrayList) bal1.selectMany(du -> du); Assert.assertEquals(4, bal2.size()); Assert.assertEquals("hel", bal2.get(0)); Assert.assertEquals("lo", bal2.get(1));