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


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

Re: Stream FlatMap

From Eric Sosman <esosman@comcast-dot-net.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: Stream FlatMap
Date 2016-01-18 20:34 -0500
Organization A noiseless patient Spider
Message-ID <n7k3mr$6pq$1@dont-email.me> (permalink)
References <39a5793c-6a65-4402-9c11-69667a03f11d@googlegroups.com>

Show all headers | View raw


On 1/18/2016 10:57 AM, Philipp Kraus wrote:
> Hello,
>
> I have got a list like:
>
> List<?> x = [1,2, [4,5, ["a", "b"], "c", "d"], ["x", "y"], "z"];
>
> and I would like to flat this list into
>
> y = [1, 2, 4, 5, "a", "b", "c", "d", "x", "y", "z"]
>
> The list can store any object type so it can be a raw value or any collection typ (with recursion).
> How can I flat this list with the streams?

     Got it!!!

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Foo {

    public static Stream<Object> flatten(Object thing) {
       return (thing instanceof List)
             ? ((List<Object>) thing).stream().flatMap((item) -> 
flatten(item))
             : Stream.of(thing);
    }

    public static void main(String[] unused) {
       List<?> list = Arrays.asList(
             1,
             2,
             Arrays.asList(
                   4,
                   5,
                   Arrays.asList("a", "b"),
                   "c",
                   "d"
             ),
             Arrays.asList("x", "y"),
             "z"
       );

       System.out.println(list);
       System.out.println();
       List<?> flattened = flatten(list).collect(Collectors.toList());
       System.out.println(flattened);
    }
}

-- 
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next 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