4 ms·
As nobody seems to have posted a Java solution yet: import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; import s
by catdog 10y ago
As nobody seems to have posted a Java solution yet:
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static java.util.Arrays.*;
public class Flatten {
public static void main(String[] args) {
List<Object> nested = asList(1, asList(2, 3), asList(4, asList(5,6)));
List<?> flat = flatten(nested);
System.out.println(nested);
System.out.println(flat);
}
public static List<?> flatten(List<?> nested) {
return nested.stream()
.flatMap(x -> x instanceof List ? flatten((List<?>) x).stream() : Stream.of(x))
.collect(Collectors.toList());
}
}
I would say it's a bit of an odd question if you mainly target Java as it's very uncommon to encounter such a construct there, alone because the type system can't handle it properly.
- SwellJoe 10y agoJava never fails to astound in its verbosity, even today, even with much better utility functions for things like lists (flatMap/collect are new to me). But, now that I've taken the time to read through it, I see the actual flatten function isn't actually all that different an implementation from the Perl and Python variants other folks have suggested. But, Java sure does make you work for that list data structure. I know it's verbose because of type definitions, and because there's no bare functions in Java, but it sure does balloon up small programs with a lot of boilerplate, and tends to hide the point of the program behind stuff that doesn't look like the purpose of the program.
- vivaamerica 10y agoType-safe, bonus unit test. import org.junit.Test; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; import static junit.framework.TestCase.assertEquals; public class FlattenList { @Test public void test() { // [1, [2, 3], [4, [5, 6]], [7, [8, [9, 10]]]] final FlattenableList<Integer> list = new FlattenableList<>(); list.add(new SingleFlattenable<>(1)); list.add(new ListOfSingles<>(Arrays.asList(2, 3))); list.add(new Flattenable<Integer>(){{ add(new SingleFlattenable<>(4)); add(new ListOfSingles<>(Arrays.asList(5, 6))); }}); list.add(new Flattenable<Integer>(){{ add(new SingleFlattenable<>(7)); add(new Flattenable<Integer>(){{ add(new SingleFlattenable<>(8)); add(new ListOfSingles<>(Arrays.asList(9, 10))); }}); }}); assertEquals(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), list.flatten()); } public static class FlattenableList<T> { final List<Flattenable<T>> list = new ArrayList<>(); void add(final Flattenable<T> item) { list.add(item); } List<T> flatten() { return list.stream().flatMap(Flattenable::flatten).collect(Collectors.toList()); } } private static class Flattenable<T> { final List<Flattenable<T>> contents = new ArrayList<>(); Stream<T> flatten() { return contents.stream().flatMap(Flattenable::flatten); } void add(final Flattenable<T> content) { contents.add(content); } } private static class ListOfSingles<T> extends Flattenable<T> { final List<T> list; ListOfSingles(final List<T> list) { this.list = list; } @Override Stream<T> flatten() { return list.stream(); } } private static class SingleFlattenable<T> extends Flattenable<T> { final T item; SingleFlattenable(final T item) { this.item = item; } @Override Stream<T> flatten() { return Stream.of(item); } } }