Win a copy of Microservices in Action this week in the Web Services 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
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

how to generate java 8 function using lambda  RSS feed

 
Greenhorn
Posts: 29
  • 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.
 
Bartender
Posts: 9499
184
  • 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: 29
    • 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: 21466
    97
    Chrome Eclipse IDE Java Spring Ubuntu VI Editor 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: 29
    • 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
    Bartender
    Posts: 9499
    184
    • 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:
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!