• Post Reply Bookmark Topic Watch Topic
  • New Topic

Passing a collection  RSS feed

 
Paul Peterson
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't like writing repetitive code for similar circumstances.  One thing I have written and slightly modified several times is a method to save data to a file.  I have part of my solution using lambdas, but how do I send a collection as a parameter without specifying what kind of collection it will be in the method signature?
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It depends on what you want to do with the collection. If all you want to do is iterate over all the elements, you pass an Iterable<? extends YourType>, and then perform some action on each element in an enhanced for-loop. If you want to modify a collection without reading from it, you pass a Collection<? super YourType>. If you want to both read and write, you simply pass a Collection<YourType>.

Maybe you can tell us more about the type of code duplication you're experiencing, and we can give better help.
 
Paul Peterson
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your info.  I think I stated my question incorrectly.  My question is about the method that is receiving the collection.  I think I have a handle on how I'm going to send the collections, I just am not sure what to put in the parameter for the method that will receive the collection.
 
Paul Peterson
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, your nudge got me pointed in the right direction, I think.  It looks like Collection<?> may handle what I am trying to do.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you are trying to do interests me. When you get it to work could you post your abbreviated solution here?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Peterson wrote:I don't like writing repetitive code for similar circumstances.  One thing I have written and slightly modified several times is a method to save data to a file.  I have part of my solution using lambdas, but how do I send a collection as a parameter without specifying what kind of collection it will be in the method signature?

As a general rule, eliminating duplication is a Good Thing™. However, your mention of lambdas and generic a collection parameter raises a small red flag for me. It's small and it may be nothing but it's something to watch out for nonetheless.  Are you looking to write to the same file? Is the data you're writing related or not? Are you always writing to the same file or is the file dependent on the data you want to write? How big are these lambdas? You wrote "lambdas" (plural) - do you have a different lambda expression for each type of collection element you want to write? How different are these lambdas from each other? Have you asked yourself any of these questions?
 
Paul Peterson
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm nearly there, just one more question should clear this one up.

This is what I have at the moment:



I am getting an error message on lines 11 and 12 for the variable in the predicate and consumer saying "incompatible types: Object cannot be converted to CAP#1 where CAP#1 is a fresh type-variable CAP#1 extends Object from capture of ?"
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This could be simplified to
or maybe (? not sure about this)
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where are you using the "out" variable?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Peterson wrote:
I am getting an error message on lines 11 and 12 for the variable in the predicate and consumer saying "incompatible types: Object cannot be converted to CAP#1 where CAP#1 is a fresh type-variable CAP#1 extends Object from capture of ?"

Wouldn't that just be:

I'd question the necessity of doing all this. Seems to me you're simply trying to eliminate the duplication of lines 3-10 and the boilerplate try-catch code around the actual write operations on lines 12-14. Look in the Spring Framework for cues, in particular classes like JdbcTemplate and JdbcDaoSupport. These classes hide all the boilerplate infrastructure-related code and allow the application developer to focus on the domain-specific code by simply overriding the template method and provide the specific logic for writing rows data to a repository. In your case the file is your repository and the specific logic that would go in a template method is what you have probably coded in your predicate and consumer lambdas.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Where are you using the "out" variable?

Good question. There's a call to flush() on line 15 but there's no way those lambdas are going to be able to access out to write anything to it.
 
Paul Peterson
Ranch Hand
Posts: 106
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like I may be taking the wrong direction on this.  Heading back to the drawing board
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:
Carey Brown wrote:Where are you using the "out" variable?

Good question. There's a call to flush() on line 15 but there's no way those lambdas are going to be able to access out to write anything to it.

Well, it is possible, and we can repair the error that Paul reported with something like: (I replaced the cio calls, since I don't have this class)

Output:

LetsWriteItOut
LetsWriteItOut
Data Saved
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:
Well, it is possible, and we can repair the error that Paul reported with something like: (I replaced the cio calls, since I don't have this class)

Nice one, Piet. 

I think you can make that
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, have a cow for that, Piet. 
 
Paul Peterson
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am thrilled that you found a solution to my problem.  I'm going to give that a try on the next project.  I'm nearly done with this one and need to keep moving forward.  At the moment I'm cleaning up some of my code so it will be available for recruiters and prospective employers.  Definite time crunch to get as much done as quickly as possible.  I really need to start working soon!
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Actually, have a cow for that, Piet. 

Thank you!
Junilu Lacar wrote:
I think you can make that


Yes that will work too, of course. It is just that I was raised writing functions like: f(x, y) = ... , so I am one of the (very?) few who like to see the parameters involved explicitly and dislike method references. I guess I'm just too old for these method references...
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Peterson wrote:I am thrilled that you found a solution to my problem.  I'm going to give that a try on the next project.  I'm nearly done with this one and need to keep moving forward.  At the moment I'm cleaning up some of my code so it will be available for recruiters and prospective employers.  Definite time crunch to get as much done as quickly as possible.  I really need to start working soon!


There's a Go (the programming language) Proverb I really like and if anything, I'd hire and fire based on it: Clear is better than clever.

I think a lot of recruiters and employers these days would agree with that. Don't get too carried away with the lambdas; there's a point where it becomes less clear and more like you're just trying to be clever.
 
Paul Peterson
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:

There's a Go (the programming language) Proverb I really like and if anything, I'd hire and fire based on it: Clear is better than clever.

I think a lot of recruiters and employers these days would agree with that. Don't get too carried away with the lambdas; there's a point where it becomes less clear and more like you're just trying to be clever.


Thank you for your suggestion.  I will keep that in mind while I clean up my code.

I'm so glad to have found this forum where rookies can ask questions, even when they're asking the wrong question.  Everyone is very helpful and pleasant!

Thank You!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!