Path: csiph.com!aioe.org!.POSTED!not-for-mail From: =?UTF-8?Q?Arne_Vajh=c3=b8j?= Newsgroups: comp.lang.java.programmer Subject: Re: Stream FlatMap Date: Thu, 21 Jan 2016 10:12:40 -0500 Organization: Aioe.org NNTP Server Lines: 123 Message-ID: References: <39a5793c-6a65-4402-9c11-69667a03f11d@googlegroups.com> NNTP-Posting-Host: ANasIS7Js9OyLQwgloeBLQ.user.gioia.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.5.1 X-Notice: Filtered by postfilter v. 0.8.2 Xref: csiph.com comp.lang.java.programmer:33497 On 1/19/2016 3:15 PM, Philipp Kraus wrote: > Thanks for the code excerpt. I have modified this code: > > public static Stream flattenToStream(List list) { > return list.stream().flatMap(item -> > item instanceof List ? > flattenToStream((List)item) : > Stream.of(item)); > } > > for my system, so it works well with my term, atom structure :-) Given the data structure then this is more or less how to do it. There are a couple of variations. I would do it like: import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; public class SimpleFlatten { public static Stream flatten(Object o) { return (o instanceof Collection) ? ((Collection)o).stream().flatMap(elm -> flatten(elm)) : (o instanceof Object[]) ? Arrays.stream((Object[])o).flatMap(elm -> flatten(elm)) : Stream.of(o); } public static void main(String[] args) { List all = new ArrayList<>(); all.add(1); all.add("A"); List sublist = new ArrayList<>(); sublist.add(2); sublist.add("B"); all.add(sublist); List innerlist = new ArrayList<>(); innerlist.add(4); Object[] subarray = new Object[] { 3, "C", innerlist }; all.add(subarray); System.out.println(flatten(all).collect(Collectors.toList())); } } But that is just me. If you want to avoid the instanceof, then you will need to change the data structure. Example: import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; public class OOFlatten { public static abstract class Wrapper { public abstract Stream stream(); } public static class ObjectWrapper extends Wrapper { private Object v; public ObjectWrapper(Object v) { this.v = v; } @Override public Stream stream() { return Stream.of(v); } } public static class CollectionWrapper extends Wrapper { private Collection v; public CollectionWrapper(Collection v) { this.v = v; } @Override public Stream stream() { return v.stream().flatMap(o -> o.stream()); } } public static class ArrayWrapper extends Wrapper { private Wrapper[] v; public ArrayWrapper(Wrapper[] v) { this.v = v; } @Override public Stream stream() { return Arrays.stream(v).flatMap(o -> o.stream()); } } public static Stream flatten(Wrapper o) { return o.stream(); } public static void main(String[] args) { List all = new ArrayList<>(); all.add(new ObjectWrapper(1)); all.add(new ObjectWrapper("A")); List sublist = new ArrayList<>(); sublist.add(new ObjectWrapper(2)); sublist.add(new ObjectWrapper("B")); all.add(new CollectionWrapper(sublist)); List innerlist = new ArrayList<>(); innerlist.add(new ObjectWrapper(4)); Wrapper[] subarray = new Wrapper[] { new ObjectWrapper(3), new ObjectWrapper("C"), new CollectionWrapper(innerlist) }; all.add(new ArrayWrapper(subarray)); System.out.println(flatten(new CollectionWrapper(all)).collect(Collectors.toList())); } } Arne