1

I have a Java class that uses Spark. I need to filter out the header from JavaRDD. This is how I want to do this.

String first = data.first();
JavaRDD<String> filteredData = data.filter((String s) -> {return !s.contains(first);});

However, this code data.filter((String s) -> {return !s.contains(first);}) cannot be compiled. IntelliJ IDE says "Lambda expressions are not supported at this language level".

1
  • 2
    Change your project language level to 8.0. Lambdas don't exist in JDK 7 and earlier.
    – duffymo
    Nov 26, 2015 at 12:57

1 Answer 1

4

You can use lambdas on Java 7, but it is a bit involved — you have to use something like Retrolambda.

Also you can do the same thing without lambdas. Lambdas can be easily represented via annonymous classes, however it is a lot more verbose.

final String first = data.first();
JavaRDD<String> filteredData = data.filter(new Function<String, Boolean>() {
  @Override public Boolean call(String s) {
    return !s.contains(first);
  }
});
7
  • Could I do a similar thing with Java 7? Nov 26, 2015 at 12:58
  • @KlausosKlausos I updated the answer.
    – pepyakin
    Nov 26, 2015 at 13:00
  • What if I don't want to integrate any library in my project? Can I filter out a header using instruments of Java 7? Nov 26, 2015 at 13:04
  • @KlausosKlausos answer updated
    – pepyakin
    Nov 26, 2015 at 13:11
  • @KlausosKlausos probably you should also update your question with this requirements.
    – pepyakin
    Nov 26, 2015 at 13:12

Not the answer you're looking for? Browse other questions tagged or ask your own question.