Meaningless Drivel is fun!*
The moose likes Java in General and the fly likes Reusing the functional interfaces in java.util.function. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Reusing the functional interfaces in java.util.function." Watch "Reusing the functional interfaces in java.util.function." New topic
Forums: Java in General Features new in Java 8
Author

Reusing the functional interfaces in java.util.function.

Avor Nadal
Ranch Hand

Joined: Sep 15, 2010
Posts: 105

Hello:

The interfaces that exist in the package java.util.function cover many types of cases. I've realized that I could replace all the functional interfaces in my current project with their corresponding equivalents from this one. Is it OK to reuse them in our projects? Or should we still make our own functional interfaces?

Thank you.
Mike. J. Thompson
Ranch Hand

Joined: Apr 17, 2014
Posts: 252
    
    4
They are there so you can reuse them, but I'm not sure it would be worth the effort to remove interfaces you already have.

I'm also in two minds as to whether it would actually better to have interfaces with names that fit into your problem domain. I think code might lose some clarity with the Java provided interfaces. I will have to see how code looks when it is using them.
Avor Nadal
Ranch Hand

Joined: Sep 15, 2010
Posts: 105

Mike. J. Thompson wrote:They are there so you can reuse them, but I'm not sure it would be worth the effort to remove interfaces you already have.


Thank you for your answer ;) . In that case, I'll do it. Because they're not many and the cost of removing them is very little. It will reduce the complexity of my package, I think.


Mike. J. Thompson wrote:I'm also in two minds as to whether it would actually better to have interfaces with names that fit into your problem domain. I think code might lose some clarity with the Java provided interfaces. I will have to see how code looks when it is using them.


That worries me too. However, I'm used to use temporal variables to give these pieces of code a proper name in the current context. I know that this kills the brevity of lambdas, but it helps me understand the code:



Even I'm thinking about starting to use comments next to every lambda argument and remove so many temporal variables:



What do you think it's better?
Mike. J. Thompson
Ranch Hand

Joined: Apr 17, 2014
Posts: 252
    
    4
In my opinion, if you have to comment the code then it probably isn't clear enough. In your example I would say that the comment gets in the way since it doesn't say anything that isn't obvious from the code.

If the lambda gets complicated enough that it requires a comment I think I will move it into a method with a descriptive name so it doesn't require a comment, and then use method references to pass it around.
Avor Nadal
Ranch Hand

Joined: Sep 15, 2010
Posts: 105

Mike. J. Thompson wrote:In my opinion, if you have to comment the code then it probably isn't clear enough. In your example I would say that the comment gets in the way since it doesn't say anything that isn't obvious from the code.

If the lambda gets complicated enough that it requires a comment I think I will move it into a method with a descriptive name so it doesn't require a comment, and then use method references to pass it around.


You're right. I've to admit that my example was poor, he he he. Thank you.
Avor Nadal
Ranch Hand

Joined: Sep 15, 2010
Posts: 105

By the way, finally I left my interfaces intact . As you commented, removing them reduced the clarity of my code.
Mike. J. Thompson
Ranch Hand

Joined: Apr 17, 2014
Posts: 252
    
    4
Did you still move to using lambdas?
Avor Nadal
Ranch Hand

Joined: Sep 15, 2010
Posts: 105

Yes. However, only in very specific places by now.

By the way, I'm using NetBeans IDE. Every time that it detects a simple "for" loop, it recommends me to use a functional operation (for example, Map.stream ().forEach ()). I believe that it's exaggerated. I doubt that a Collection of less of 10 elements can benefit from that, Right?

PS: I'm sorry for such off-topic .
Terry Birch
Greenhorn

Joined: Nov 06, 2011
Posts: 7
Good questions even if they are off topic. I would like to see more like yours.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7696
    
  20

Avor Nadal wrote:Every time that it detects a simple "for" loop, it recommends me to use a functional operation (for example, Map.stream ().forEach ()). I believe that it's exaggerated. I doubt that a Collection of less of 10 elements can benefit from that, Right?

The only thing I can think of is that it might be pointing you to a more efficient (or flexible) implementation; but, like you, I think that warning you every time is a bit OTT.
I'm quite happy to admit that, these days, a compiler can probably optimize far better than I can; but if that's the case: DO IT; don't warn me about it.

Winston

PS: Aaaggh!
<rant>
Just looked at the Spliterator class and discovered that it has integer "characteristic" constants valued for OR-able sets. For a version 8 class? Hasn't Oracle heard about Enums and EnumSets? Or do they just not pay their developers enough to get good ones (or peers enough to do code reviews)?
</rant>

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Avor Nadal
Ranch Hand

Joined: Sep 15, 2010
Posts: 105

Winston Gutkowski wrote:...a compiler can probably optimize far better than I can; but if that's the case: DO IT; don't warn me about it.


Good thought, he he he.

Because I can't live in calm seeing so many warnings in my IDE, I believe that I will simply disable them :P .
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7696
    
  20

Avor Nadal wrote:Because I can't live in calm seeing so many warnings in my IDE, I believe that I will simply disable them :P .

Well, hopefully you can just disable those warnings, because there are plenty of others that are worth knowing about.

Winston
Avor Nadal
Ranch Hand

Joined: Sep 15, 2010
Posts: 105

Winston Gutkowski wrote:Well, hopefully you can just disable those warnings, because there are plenty of others that are worth knowing about.

Winston


Oh, yes, yes. Only that ones, of course.
Mike. J. Thompson
Ranch Hand

Joined: Apr 17, 2014
Posts: 252
    
    4
Yes, I think the warning you're getting is more about code style than anything else. It would be equivalent to telling you to use a for-each loop rather than a for-loop.

Personally that doesn't sound like a compiler warning to me. It should probably be a PMD or Checkstyle rule. I would reserve compiler warnings for things that might break at runtime.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19679
    
  18

Winston Gutkowski wrote:PS: Aaaggh!
<rant>
Just looked at the Spliterator class and discovered that it has integer "characteristic" constants valued for OR-able sets. For a version 8 class? Hasn't Oracle heard about Enums and EnumSets? Or do they just not pay their developers enough to get good ones (or peers enough to do code reviews)?
</rant>

To make it weirder, Collector was added with a method of the same name, but that does return a Set of enums. Makes you wonder how they could do it so differently in the same release with classes related to the same feature (streams).


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reusing the functional interfaces in java.util.function.