This week's book giveaway is in the Java 9 forum.
We're giving away four copies of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications and have Sander Mak & Paul Bakker on-line!
See this thread for details.
Win a copy of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications this week in the Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Claude Moore
Ranch Hand
Posts: 911
9
IBM DB2 Java Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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 ?
 
Les Morgan
Rancher
Posts: 776
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • 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
Ranch Hand
Posts: 911
9
IBM DB2 Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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.
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 37382
531
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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
Ranch Hand
Posts: 911
9
IBM DB2 Java Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 37382
531
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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))
 
Campbell Ritchie
Marshal
Posts: 56193
171
  • Mark post as helpful
  • send pies
  • 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.
 
Stephan van Hulst
Saloon Keeper
Posts: 7927
143
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 37382
531
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doh! Excellent point Stephen!
 
Claude Moore
Ranch Hand
Posts: 911
9
IBM DB2 Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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
Saloon Keeper
Posts: 7927
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
Ranch Hand
Posts: 911
9
IBM DB2 Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!