Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #33497

Re: Stream FlatMap

From Arne Vajhøj <arne@vajhoej.dk>
Newsgroups comp.lang.java.programmer
Subject Re: Stream FlatMap
Date 2016-01-21 10:12 -0500
Organization Aioe.org NNTP Server
Message-ID <n7qshb$pp1$1@gioia.aioe.org> (permalink)
References <39a5793c-6a65-4402-9c11-69667a03f11d@googlegroups.com> <n7k3mr$6pq$1@dont-email.me> <a3a0221a-a5e6-4e0d-9018-4aa770e3eeb5@googlegroups.com> <a9c0a5df-cfab-4bf0-90bb-c99d9573a72c@googlegroups.com>

Show all headers | View raw


On 1/19/2016 3:15 PM, Philipp Kraus wrote:
> Thanks for the code excerpt. I have modified this code:
> 	
> public static Stream<Object> 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<Object> all = new ArrayList<>();
         all.add(1);
         all.add("A");
         List<Object> sublist = new ArrayList<>();
         sublist.add(2);
         sublist.add("B");
         all.add(sublist);
         List<Object> 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<Wrapper> v;
		public CollectionWrapper(Collection<Wrapper> 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<Wrapper> all = new ArrayList<>();
         all.add(new ObjectWrapper(1));
         all.add(new ObjectWrapper("A"));
         List<Wrapper> sublist = new ArrayList<>();
         sublist.add(new ObjectWrapper(2));
         sublist.add(new ObjectWrapper("B"));
         all.add(new CollectionWrapper(sublist));
         List<Wrapper> 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



Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

Stream FlatMap Philipp Kraus <philipp.kraus.flashpixx@gmail.com> - 2016-01-18 07:57 -0800
  Re: Stream FlatMap Sebastian <news@seyweiler.dyndns.org> - 2016-01-18 23:11 +0100
    Re: Stream FlatMap Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-01-18 18:18 -0500
  Re: Stream FlatMap Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-01-18 20:34 -0500
    Re: Stream FlatMap Sebastian <sebastian.millies@softwareag.com> - 2016-01-19 04:12 -0800
      Re: Stream FlatMap Philipp Kraus <philipp.kraus.flashpixx@gmail.com> - 2016-01-19 12:15 -0800
        Re: Stream FlatMap Arne Vajhøj <arne@vajhoej.dk> - 2016-01-21 10:12 -0500

csiph-web