This week's book giveaway is in the NodeJS forum.
We're giving away four copies of Serverless Applications with Node.js and have Slobodan Stojanovic & Aleksandar Simovic on-line!
See this thread for details.
Win a copy of Serverless Applications with Node.js this week in the NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

Optional's ifPresentOrElse method  RSS feed

 
Gerard Gauthier
Ranch Hand
Posts: 99
Java Linux Monad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Has anyone tried using this method(ifPresentOrElse) yet? I find it works great on collections of simple data but I have to start casting when the collection contains abstract classes. I'm not sure if its my code or a limitation of the method. I'd post the code but its pretty messy.

Has anyone else used this method?
 
Stephan van Hulst
Saloon Keeper
Posts: 9997
208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't use it a lot, because usually when my optionals are empty I want to do an early return from the method, which is not possible from the Runnable that you pass.

If you find that you're mixing casts and generics, usually you're not using your generics correctly. Why do you have to cast?
 
Gerard Gauthier
Ranch Hand
Posts: 99
Java Linux Monad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I was using ifPresentOrElse on a TreeSet<String> that I called stream on and then reduce(reduced to an Optional) and it would work fine, but if I tried TreeSet with an abstract class then I had to start casting the objects(because they were Object) in the ifPresentOrElse method, BTW, ifPresentOrElse worked with the TreeSet(with abstract class) when I  did the cast.  
 
Paul Clapham
Sheriff
Posts: 24295
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks to me like if you had var set = TreeSet<T>, where T was your abstract class, then set.stream().reduce(BinaryOperator<T> accumulator) should return an Optional<T>.

If your accumulator function can be written with only T as its type parameter then that shouldn't be a problem. But maybe you've got TreeSet<? extends T> in your declaration? That might make it harder.
 
Stephan van Hulst
Saloon Keeper
Posts: 9997
208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you show us your reduction?
 
Gerard Gauthier
Ranch Hand
Posts: 99
Java Linux Monad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Can you show us your reduction?



Sure. Here's an example that works without the cast.

 
Campbell Ritchie
Marshal
Posts: 63849
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you using compareTo() to test for String equality? What is wrong with using System.out.println(myOptional.orElse("Not found."));? Why are you using a Stream at all when you are simply going to find “Third”?
 
Gerard Gauthier
Ranch Hand
Posts: 99
Java Linux Monad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Why are you using compareTo() to test for String equality? What is wrong with using System.out.println(myOptional.orElse("Not found."));? Why are you using a Stream at all when you are simply going to find “Third”?



Actually I'm consider an iterator from the treeset. I can walk through the data and check each element.
 
Stephan van Hulst
Saloon Keeper
Posts: 9997
208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is not really a good example. You would just do stringSet.stream().findAny("Third"::equals).

Can you give us a real example, also demonstrating why you need the cast?
 
Gerard Gauthier
Ranch Hand
Posts: 99
Java Linux Monad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the code.. cough**, cough** messy code I was working on. I decided to use the foreach method to check each entry. I know. I know.

The code is a data structure that implements the basics of a filesystem. I'm not sure how to implement symlinks yet.

MyNode.java


MyFile.java


MyDirectory.java. This was the file that I was trying to implement the ifPresentOrElse method.


Main.java
 
Gerard Gauthier
Ranch Hand
Posts: 99
Java Linux Monad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:This is not really a good example. You would just do stringSet.stream().findAny("Third"::equals).

Can you give us a real example, also demonstrating why you need the cast?



I don't have that findAny() available with my Java.

This is not available with the Java I have installed.


My Java credentials.
openjdk 10.0.2 2018-07-17
OpenJDK Runtime Environment (build 10.0.2+13-Ubuntu-1ubuntu0.18.04.4)
OpenJDK 64-Bit Server VM (build 10.0.2+13-Ubuntu-1ubuntu0.18.04.4, mixed mode)
 
Campbell Ritchie
Marshal
Posts: 63849
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Gerard Gauthier wrote:. . . I don't have that findAny() available with my Java. . . .

The findAny(...) method was nitroduced in the first version off Stream, Java8; so was Optional.
 
Gerard Gauthier
Ranch Hand
Posts: 99
Java Linux Monad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Gerard Gauthier wrote:. . . I don't have that findAny() available with my Java. . . .

The findAny(...) method was nitroduced in the first version off Stream, Java8; so was Optional.



But you posted this.


Can you explain what you are passing to findAny and where that exists in the Stream library?
 
Dave Tolls
Rancher
Posts: 3989
47
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think they mean:


Missing the filter.
 
Gerard Gauthier
Ranch Hand
Posts: 99
Java Linux Monad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:I think they mean:


Missing the filter.



Ok.That makes sense.
 
Stephan van Hulst
Saloon Keeper
Posts: 9997
208
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, apologies. I work with C# a lot and I sometimes mix up the Stream API and the Enumerable API.

Anyway, your problem is that you throw files and folders on one big pile, even though you treat them very differently most of the time. Polymorphism is not the answer here. Just keep track of two different collections. While you're at it, you might want to use a Map instead of a Set. Whenever you are iterating through a collection to find a specific element, that is usually a hint that you want a Map instead.

Other issues with your code:

MyNode declares that it's a Comparable, but it doesn't implement it. Typically you would make a final implementation in the class that declares Comparable, because sub-classes can't really override the default implementation to make semantic changes without breaking the type contract.

There's no real reason for different node types to have a custom implementation of getNodeName(). Make a final implementation in the abstract base class.

Use strings for names and identifiers, and use strong types for everything else. Don't use a String for a node type, use an enum instead. However, I'm not convinced you need to keep track of the type at all. Just keep track of files and directories in separate collections.

Use a more descriptive name than displayAux(), especially if you don't document your methods.

Why are you using an Iterator for your addPathFile() and addPath() methods. Iterators are very unwieldy. If your intention was to be able to pass a sub-sequence to a recursive call, then List is easier to use because it has a subList() method. Better yet, you may want to wrap such a List into a custom class that represents a path in a file system.

Why is the padding() method public? Why is it not a verb? An easy way to generate the indentation is join("", nCopies(depth, pad)), with join() imported from String and nCopies imported from Collections. No need for a utility method.

This is how I would add a file to a directory, when not using a strongly typed custom MyPath class:
 
A berm makes a great wind break. And Iwe all like to break wind once in a while. Like this tiny ad:
global solutions you can do at home or in your backyard
https://www.kickstarter.com/projects/paulwheaton/better-world-boo
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!