• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Using Stream to generate a list of POJOs... (just playing a bit)

 
Bartender
Posts: 1385
39
IBM DB2 Netbeans IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Need your help / advices.

I want to generate a list of POJOs using Stream. So, I defined a simple POJO :



and an helper class, Counter:



the most concise way I found to generate a List of MyPOJO is the following:



It works. But how would judge it ? I'd say it's at least... convoluted.
Is there any idiomatic way to do the same ?
 
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Claude,

If I am correct in my understanding, then you want to know how many PoJo's there are (objects that have been created).  This seems like an excellent place to take advantage of a Class Variable.



With each "new MYPoJo()", the creation of an object of type MyPoJo, then the counter gets incremented by 1.  This Class Varaible, iPoJo , is referenced in each instance of a MyPoJo object.

Les
 
Claude Moore
Bartender
Posts: 1385
39
IBM DB2 Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, not exactly.I used a constructor for MyPojo class taking an int as parameter just for simplicity, and to not be able to use default constructor using MyPOJO::new syntax in my stream expression.The goal wasn't to count created instances.
 
author & internet detective
Posts: 42135
937
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Claude,
Good question. Have a cow.

First Stream.<MyPOJO> is redundant. The compiler can infer the type so you don't need the generic.

Beyond that, I'd separate the two calls. Generate makes a list of numbers and mapToObject actually lets you create the POJO. I also added indentation to make it easier to see what it going on.

 
Claude Moore
Bartender
Posts: 1385
39
IBM DB2 Netbeans IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jeanne,
first, thanks for the cow !  Second, your solution is very smart : I studied it a bit , and found another approach using iterate() method to generate the IntStream:



This way, I don't need my helper Counter class anymore.  Despite this, your solution it's simply great, thanks for sharing it !


 
Jeanne Boyarsky
author & internet detective
Posts: 42135
937
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good idea getting rid of the counter! I wasn't sure if it was used laster which is why I didn't propose getting rid of it.

You can use a method reference for this to make it a tiny bit shorter.
.mapToObj(x -> new MyPOJO(x))
 
Marshal
Posts: 80639
472
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's more, a counter like that counts hw many objects have been created, but there is no straightforward way to count how many objects have lost all their references and have become unreachable and eligible for garbage collection. Even if finalize() weren't deprecated, it wouldn't help much.
 
Bartender
Posts: 15741
368
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Jeanne Boyarsky
author & internet detective
Posts: 42135
937
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Doh! Excellent point Stephen!
 
Claude Moore
Bartender
Posts: 1385
39
IBM DB2 Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:


The only "problem" with this approach is that MyPojo doesn't have a default constructor.But I like your solution very much!!
 
Stephan van Hulst
Bartender
Posts: 15741
368
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't need a parameterless constructor. The class has a constructor that consumes an int, which counts as an IntFunction, and can be referrenced by MyPojo::new.

In fact, if your class only had a parameterless constructor, my approach wouldn't have worked.
 
Claude Moore
Bartender
Posts: 1385
39
IBM DB2 Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:You don't need a parameterless constructor. The class has a constructor that consumes an int, which counts as an IntFunction, and can be referrenced by MyPojo::new.

In fact, if your class only had a parameterless constructor, my approach wouldn't have worked.



You're right, Stephan !! Shame on me. I didn't realize that MyPOJO(int x) would have been called.
reply
    Bookmark Topic Watch Topic
  • New Topic