This week's book giveaways are in the Scala and Android forums.
We're giving away four copies each of Machine Learning Systems: Designs that scale and Xamarin in Action: Creating native cross-platform mobile apps and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Ranch Hand
Posts: 974
9
IBM DB2 Java Netbeans IDE Spring
  • 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 ?
 
Rancher
Posts: 786
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: 974
9
IBM DB2 Java Netbeans IDE Spring
  • 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.
 
author & internet detective
Marshal
Posts: 38250
623
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: 974
9
IBM DB2 Java Netbeans IDE Spring
  • 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
Marshal
Posts: 38250
623
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))
 
Marshal
Posts: 59108
180
  • 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.
 
Saloon Keeper
Posts: 8898
167
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 38250
623
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: 974
9
IBM DB2 Java Netbeans IDE Spring
  • 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: 8898
167
  • 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: 974
9
IBM DB2 Java Netbeans IDE Spring
  • 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.
 
What kind of corn soldier are you? And don't say "kernel" - that's only for this tiny ad:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!