• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to generate java 8 function using lambda  RSS feed

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I try to generate java 8 function with using java lambda. Below is the java 8 codes



Then I made the following codes using java lambda,



But the above code throws exception.
Cannot invoke map((<no type> cells) -> {}) on the array type String[]

Your any reply will be deeply appreciated. Best regards.
 
Saloon Keeper
Posts: 8766
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are several mistakes in your code:

  • split() returns an array, and arrays don't have a map() function.
  • You're treating the inner lambda expression argument as an array, while the idea of streams is that you process individual elements of whatever you're streaming.
  • Your inner lambda expression performs an action that would consume an individual cell, without returning anything. That's type assignable to Consumer<String>, not Put.

  • You can just write a simple method that parses a Put from a String. You can then use that method in higher order functions using a method handle.

    I think Put is a very iffy name for a class. What does it represent and what are its responsibilities? What does Bytes.toBytes() do? If it encodes a String, then you can just use String.getBytes(Charset).
     
    Joseph Hwang
    Greenhorn
    Posts: 27
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you for your reply. I am a beginner on lambda expression. Would you inform me of the simple lambda expression for class PutFunction on the 1st codes.
    Best regards.
     
    Sheriff
    Posts: 21313
    87
    Chrome Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    What Function interface is that? It's not java.util.function.Function, because that has method apply which doesn't throw any checked exceptions, and not method call which is allowed to throw a checked exception.
     
    Joseph Hwang
    Greenhorn
    Posts: 27
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The problem is solved with this code,



    And more, Function is not java.util.function.Function, but org.apache.spark.api.java.function.Function;
    Any way, Thank you for your replies!
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 8766
    163
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    It doesn't make sense to write methods like that. You're better off putting the str argument in your method parameter list, and removing the lambda expression. When you need a Function<String, Put>, you can just use a method handle instead of calling the method yourself.

    For instance, let's say that we have a method foo that accepts a Function<String, Put>. Instead of doing this:
    Do this:
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!