• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reusing the functional interfaces in java.util.function.

 
Ranch Hand
Posts: 157
Netbeans IDE Postgres Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 689
17
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 157
Netbeans IDE Postgres Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Bartender
Posts: 689
17
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 157
Netbeans IDE Postgres Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 157
Netbeans IDE Postgres Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way, finally I left my interfaces intact . As you commented, removing them reduced the clarity of my code.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you still move to using lambdas?
 
Avor Nadal
Ranch Hand
Posts: 157
Netbeans IDE Postgres Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 .
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good questions even if they are off topic. I would like to see more like yours.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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>
 
Avor Nadal
Ranch Hand
Posts: 157
Netbeans IDE Postgres Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 157
Netbeans IDE Postgres Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).
 
reply
    Bookmark Topic Watch Topic
  • New Topic