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


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

Re: Stream FlatMap

Path csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail
From Eric Sosman <esosman@comcast-dot-net.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: Stream FlatMap
Date Mon, 18 Jan 2016 20:34:55 -0500
Organization A noiseless patient Spider
Lines 55
Message-ID <n7k3mr$6pq$1@dont-email.me> (permalink)
References <39a5793c-6a65-4402-9c11-69667a03f11d@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=windows-1252; format=flowed
Content-Transfer-Encoding 7bit
Injection-Date Tue, 19 Jan 2016 01:32:11 -0000 (UTC)
Injection-Info mx02.eternal-september.org; posting-host="2d081e2f7155c28005d4472f67c2f43d"; logging-data="6970"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/dm19bHZR2i8JgWSzw870k"
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.5.1
In-Reply-To <39a5793c-6a65-4402-9c11-69667a03f11d@googlegroups.com>
Cancel-Lock sha1:cbXhfXVN7ACGBQt4ep33+ifxW4A=
Xref csiph.com comp.lang.java.programmer:33477

Show key headers only | 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