Menu

Examples and HOWTO's (How to stream ArrayList)

Anonymous
2015-02-05
2015-02-05
  • Anonymous

    Anonymous - 2015-02-05

    Hi! I was very happy to find such a library until I discovered that there are no examples or HOWTO's. I have no idea how to stream my ArrayList. list.stream() is not found. Is there any interface for wrapping lists in order to stream? Thanks in advance!

     

    Last edit: Anonymous 2015-02-05
  • Anonymous

    Anonymous - 2015-02-05

    I've found out that I can achieve this by using StreamSupport.of(list). Not very clear from the first time. But nevertheless, some examples are needed! Thanks one more time.

     
    • Stefan Zobel

      Stefan Zobel - 2015-02-05

      StreamSupport.of(list) would return a Stream<list>, i.e. your ArrayList would be the single element in the stream. That's not what you want. Use</list>

      1) public static <t> Stream<t> stream(Collection<? extends T> c)</t></t>

      or

      2) public static <t> Stream<t> parallelStream(Collection<? extends T> c)</t></t>

       
  • Anonymous

    Anonymous - 2015-02-05

    It is easiest to use the static methods

    1) public static <t> Stream<t> stream(Collection<? extends T> c)
    2) public static <t> Stream<t> parallelStream(Collection<? extends T> c)</t></t></t></t>

    in the j8.u.s.StreamSupport class.

    In addition there are

    3) public static <t> Stream<t> stream(Collection<? extends T> c, int characteristics)
    4) public static <t> Stream<t> stream(Collection<? extends T> c, int characteristics, boolean parallel)</t></t></t></t>

    in the same class. I would prefer 1) or 2), depending on whether you want a sequential or a parallel stream. Hope that helps.

     
  • Stefan Zobel

    Stefan Zobel - 2015-02-05

    It is mentioned in the "general" section in the release notes:

     - Use one of the j8.u.s.StreamSupport#stream(Collection, ...) methods or the
       j8.u.s.StreamSupport#parallelStream(Collection) method to create a Stream from
       a java.util.Collection
    

    Fortunately, this is more or less the only thing you need to know to use the library.

    Otherwise the API is identical to the official Java 8 API, with the exception of static and default interface methods which are always located in a corresponding "companion" class with same name as the interface but with an "s" appended ("Comparator" -> "Comparators").

    The only exception to this exception is the Stream interface, whose static and default methods are all located in StreamSupport. Hope that answers your question.

     

    Last edit: Stefan Zobel 2015-02-05
    • Anonymous

      Anonymous - 2015-02-05

      Yeah, your answer helped me a lot. I am sorry that I didn't notice that in releases section before, because I used Maven central repository instead. Thank for the quick response! Have a nice day!

       
      • Stefan Zobel

        Stefan Zobel - 2015-02-05

        No worries. Please ask when you have further questions.

         

Anonymous
Anonymous

Add attachments
Cancel