Results 1 to 19 of 19
Like Tree1Likes
  • 1 Post By jim829

Thread: Stream to collection

  1. #1
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    268
    Rep Power
    2

    Default Stream to collection

    I have the following

    Java Code:
    list.stream().sorted((s, s1) -> s.getName().compareTo(s1.getName())).distinct();
    I want to save the results to a collection.

    I saw an video tutorial on these by Oracle learning library and they mentioned an into(collection) method but I looked at the api and there isn't one.

    How can I do it?
    Last edited by superhaNds; 03-25-2014 at 01:08 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,812
    Rep Power
    25

    Default Re: Stream to collection

    Is this one of those C programming quiz type questions to see how short or obscure a statement can be coded?

    Can you post a simple java program that shows what you are trying to do?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    268
    Rep Power
    2

    Default Re: Stream to collection

    I don't understand the first comment, I will ignore it.

    I am just playing around. I have a student class with name and some other strings and I added some to a list, I call this on list and I want to save the results to a another collection

    Java Code:
    public class Whateeever
    
    {
    
        public static void main(String[] args) {
            Student s2 = new Student(new Name("kostas", "bril"), "CS12300", "examplf@.com");
            Student s11 = new Student(new Name("nick", "carik"), "CS12200", "ayahoof@.com");
            Student s3 = new Student(new Name("danv", "alemau"), "CS12100", "googl@.com");
            Student s4 = new Student(new Name("andre", "iguoadala"), "CS12400", "hotmail@.com");
            Student s5 = new Student(new Name("chees", "pekals"), "CS19308", "avg@.com");
            Student s6 = new Student(new Name("qwr", "faader"), "CS24300", "norton@.com");
            Student s7 = new Student(new Name("qwr", "faader"), "CS24300", "norton@.com");
    
    
            List<Student> list = new ArrayList<>();
            list.add(s2);
            list.add(s11);
            list.add(s3);
            list.add(s4);
            list.add(s5);
            list.add(s6);
            list.add(s7);
    
            List<Student> list2 = new ArrayList<>();
    
            list.stream().sorted((s, s1) -> s.getName().compareTo(s1.getName())).distinct().forEach(s -> System.out.println(s.getName()));
    
            list.stream().sorted((s, s1) -> s.getName().compareTo(s1.getName())).distinct();
    
            // i want to store the results of the stream to list2
        }
    }
    Last edited by superhaNds; 03-25-2014 at 01:08 AM.

  4. #4
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    268
    Rep Power
    2

    Default Re: Stream to collection

    found it

    list2 = list.stream().sorted((s, s1) -> s.getName().compareTo(s1.getName())).distinct().co llect(Collectors.toList());

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,920
    Rep Power
    5

    Default Re: Stream to collection

    I was sort of wondering when Java 8 would rear its <fill in your adjective here> head in this forum?

    Regards,
    Jim
    Norm likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,269
    Rep Power
    6

    Default Re: Stream to collection

    Java 8 is not a week old and already asking questions in a forum :s Give people a chance to actually learn to apply the new features. For the next few months if you want to use Java 8 you'll just have to do all the work of figuring stuff out yourself.

    This is a very decent introduction:

    Java 8 Tutorial - Benjamin Winterberg
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    268
    Rep Power
    2

    Default Re: Stream to collection

    Ok. But it was under the impression that people were already familiar with the new concepts. And that the official release was irrelevant.

    I even attended a presentation on lambdas and streams a few months ago, but understood nothing.

    BTW. this tutorial clarified things for me: Jump-Starting Lambda Programming - YouTube

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,269
    Rep Power
    6

    Default Re: Stream to collection

    Then you're probably more informed than most of us friendly friend, I'm going to ask YOU questions when I get around to learning all this newfangled nonsense ;)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,729
    Blog Entries
    7
    Rep Power
    21

    Default Re: Stream to collection

    There has been a tutorial running in Java Magazine for more than a year (the subscription is free). It's all very interesting ...

    subscribe here: Java Magazine

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,269
    Rep Power
    6

    Default Re: Stream to collection

    Yeah the whole issue of today is dedicated to Java 8 actually. Makes a nice change from the usual JEE focus.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,178
    Rep Power
    20

    Default Re: Stream to collection

    Quote Originally Posted by superhaNds View Post
    Ok. But it was under the impression that people were already familiar with the new concepts. And that the official release was irrelevant.
    In my case, I'm familiar with them, but since they don't appear in any production code I write (and won't for the best part of a year at a minimum), they're not something I've bothered to store away...:)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  12. #12
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,269
    Rep Power
    6

    Default Re: Stream to collection

    I'll be lucky if in a year time I can have Java 7 available on production machines at work :s
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  13. #13
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Stream to collection

    Yeah... there aren't many compelling reasons to use Java 7, let alone Java 8.
    Get in the habit of using standard Java naming conventions!

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,812
    Rep Power
    25

    Default Re: Stream to collection

    Should there be a special section on the forum for 1.8 stuff?
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Stream to collection

    *shrug* Lambda expressions seem weird enough to merit their own subforum. But then again, so did generics when they first came out, and now they seem normal.
    Get in the habit of using standard Java naming conventions!

  16. #16
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,920
    Rep Power
    5

    Default Re: Stream to collection

    Based on my observation I would say no. You have one on Threads and synchronization, AWT and Swing, but folks still ask those questions in either New to java or Advanced java. And there isn't one on Generics which was a major syntactic change in 1.5. So if one were to be added (big if) I would say do it for Lambda expressions. But then you have all the new stream stuff too. But no matter what you do, folks will still ask their questions in the two basic forums.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  17. #17
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,269
    Rep Power
    6

    Default Re: Stream to collection

    Agreed, creating special subforums does not mean people are going to use them; at best you'll have more moderation work to move threads around.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  18. #18
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,178
    Rep Power
    20

    Default Re: Stream to collection

    Quote Originally Posted by kjkrum View Post
    Yeah... there aren't many compelling reasons to use Java 7, let alone Java 8.
    Apart from the lack of updates to 6 for the past year or so.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  19. #19
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,269
    Rep Power
    6

    Default Re: Stream to collection

    The VM is better too; more optimization routines, smarter garbage collector. For server purposes (my bread and butter) Java 7 is most definitely a better choice than Java 6.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. How often should a stream be closed?
    By TacoManStan in forum New To Java
    Replies: 1
    Last Post: 11-30-2013, 10:30 AM
  2. IO stream problem
    By fedex646 in forum New To Java
    Replies: 1
    Last Post: 06-21-2013, 07:19 PM
  3. IO stream
    By sadeem in forum New To Java
    Replies: 4
    Last Post: 04-10-2011, 09:08 PM
  4. TCP stream cypher
    By Koren3 in forum Networking
    Replies: 2
    Last Post: 05-20-2009, 11:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •