Skip to content

Commit 229b813

Browse files
terhesbjavadev
authored andcommitted
Improve U.concat(iterables) method.
1 parent 461ff53 commit 229b813

File tree

2 files changed

+8
-12
lines changed

2 files changed

+8
-12
lines changed

src/main/java/com/github/underscore/U.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3169,17 +3169,11 @@ public static <T> T[] concat(final T[] first, final T[] ... other) {
31693169
*/
31703170
@SuppressWarnings("unchecked")
31713171
public static <T> List<T> concat(final Iterable<T> first, final Iterable<T> ... other) {
3172-
int length = 0;
3173-
for (Iterable<T> otherItem : other) {
3174-
length += size(otherItem);
3175-
}
3176-
final T[] result = Arrays.copyOf(toArray(first), size(first) + length);
3177-
int index = 0;
3178-
for (Iterable<T> otherItem : other) {
3179-
System.arraycopy(toArray(otherItem), 0, result, size(first) + index, size(otherItem));
3180-
index += size(otherItem);
3172+
List<T> list = newArrayList(first);
3173+
for (Iterable<T> iter : other) {
3174+
list.addAll(newArrayList(iter));
31813175
}
3182-
return Arrays.asList(result);
3176+
return list;
31833177
}
31843178

31853179

src/test/java/com/github/underscore/UnderscoreTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,15 @@ public void compareStrings() {
147147
@SuppressWarnings("unchecked")
148148
public void concat() {
149149
assertEquals(asList(1, 2, 3, 4), asList(U.concat(new Integer[] {1, 2}, new Integer[] {3, 4})));
150+
assertEquals(asList(1.0, 2.0), asList(U.concat(new Double[] {1.0, 2.0})));
150151
assertEquals(asList(1, 2, 3, 4), U.concat(asList(1, 2), asList(3, 4)));
151-
assertEquals(asList(1, 2, 3, 4), new U(asList(1, 2)).concatWith(asList(3, 4)));
152+
assertEquals(asList("a", "b"), U.concat(asList("a", "b")));
153+
assertEquals(asList(1, 2, 3, 4), new U<Integer>(asList(1, 2)).concatWith(asList(3, 4)));
152154
assertEquals("[1, 2, 3, 4]", U.chain(asList(1, 2)).concat(asList(3, 4)).value().toString());
153155
assertEquals("[1, 2, 3, 4, 5, 6]", U.chain(asList(1, 2)).concat(asList(3, 4), asList(5, 6)).value().toString());
154156
assertEquals(asList(1, 2, 3, 4), asList(U.concat(new Integer[] {1, 2}, new Integer[] {3}, new Integer[] {4})));
155157
assertEquals(asList(1, 2, 3, 4), U.concat(asList(1, 2), asList(3), asList(4)));
156-
assertEquals(asList(1, 2, 3, 4), new U(asList(1, 2)).concatWith(asList(3), asList(4)));
158+
assertEquals(asList(1, 2, 3, 4), new U<Integer>(asList(1, 2)).concatWith(asList(3), asList(4)));
157159
}
158160

159161
/*

0 commit comments

Comments
 (0)