• Post Reply Bookmark Topic Watch Topic
  • New Topic

use .map() without return?  RSS feed

 
Sammy Ward
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to use the .map() operator in java without having a return statement?



this is the code i'm trying to run, but I get an error in the map operator saying it's missing a return statement.
 
Pete Letkeman
Ranch Foreman
Posts: 904
26
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My Lambda knowledge is not as good as others, but I suspect that it is possible to do what you would like to do.
If I recall correctly then you need a "return" statement when you provide a semi brace brackets, but you don't always need to do that.
For instance you can haveor you can have
Given that what if you tried something like this Or a few variations there of.

Here are a few links to Lambda tutorials which may help you out:
  • http://tutorials.jenkov.com/java/lambda-expressions.html
  • https://dzone.com/articles/java-lambda-expressions-basics
  • https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html

  • A few followup thoughts and questions:
  • Are you able to provide a little more code then just that one line?
  • Does this code need to use RxJava?
  • Is this code specific to Android development?
           If so, then what version of Android as not all versions of Android implement Java the same way.
           If not then please provide much more code.

  •  
    salvin francis
    Bartender
    Posts: 1653
    37
    Eclipse IDE Google Web Toolkit Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I think that functions are used to convert an argument to a result, hence it accepts an input and returns a changed output. What you are looking for is probably a Consumer and not a Function.
    I think you will have to call forEach(Consumer<? super T> action) method instead of map method. The forEach method will consume each item in the stream

    PS: My knowledge about streams is not that great too
     
    salvin francis
    Bartender
    Posts: 1653
    37
    Eclipse IDE Google Web Toolkit Java
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    assuming that networkRepository.getResponse() returns a stream of Files, you should be able to write:

     
    Campbell Ritchie
    Marshal
    Posts: 56536
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Which class is that map() method defined in? There are seven map() methods in the standard Java® API. Only five take a single parameter, and guessing that you aren't working in a stream of primitives or an Optional, that leaves one suitable method: this one. So let's assume for the time being that I have found the method you wanted. Look at its details from the link above. Its return type is Stream<R> which means it takes one Stream, the object on which it is called, and returns a reference to a different Stream. You can also tell that because the link calls it an intermediate operation.
    Its parameter type is Function<? super T,? extends R> where the T is the type of elements of the “old” Stream and the R is a result which will be the type of the “new” Stream. Now, let's look at the details of Function. For a start,
    That API link wrote:Functional Interface:
        This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
    We now know it has one abstract method and that abstract method is what you are implementing via that λ.This is that abstract method. You will see it takes a T as its parameter and returns an R. That determines how you have to implement it: you must supply one parameter which is the T and it must return an R. So no, you cannot implement Function#apply without returning something. This is how you might implement it as an anonymous class. Later on I shall whittle that down to a λ.That does something simple, mapping a String to the number of keystrokes it contains. Now let's start whittling it down. You already know it is a Function, so who needs the name of the class, or the {}?Now, we already can guess the name of the method because we know there is only one method actually having to be implemented, so we can lose the method name and modifiers and return type:-Now, use the -> arrow token to join the parameters to the method body:-What you are writing is a formula to go from a String to an Integer, and you now have an expression which represents a String on the left of -> and you want to reduce what is on the right of the -> to a simple expression too, so we can get rid of {} and return and some whitespace:-We know s is a String so we can delete String:-Because there is only one token, s, inside the (), we can delete the ().Now look at the difference from what you had. You aren't returning anything, so the compiler won't be happy about that. You could add return true; to your λ, but that will produce a Stream<Boolean>, which you probably don't want. Salvin is surely correct to suggest using forEach, particularly since forEach permits side effects like writing to files.
     
    Campbell Ritchie
    Marshal
    Posts: 56536
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    All you are really doing in my λ in the previous post is calling the length() method, so you can probably shorten the λ to a method reference:-You can miss out the cast which was only there to remind you about boxing conversions. The method reference doesn't obviate the return type because length() returns an int, which can be boxed to an Integer.
     
    Consider Paul's rocket mass heater.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!