Equivalent to Ruby #map or #collect in Java?

Let's say I have an array movies = get_movies()

In ruby ​​I often do movies.map {|movie| movie.poster_image_url }or somesuch.

What can I do, it looks like Java? And just as elegant and red and easy to read. I know that there are many ways I can do this, but if there is a good way to do this, it will make me not want to use Groovy or anything, let me know. I'm sure Java has some amazing ways to do such things.

This is my Java code so far using the TheMovieDB API Shell API from https://github.com/holgerbrandl/themoviedbapi/ .

        TmdbMovies movies = new TmdbApi(BuildConfig.MOVIEDB_API_KEY).getMovies();
        MovieResultsPage results = movies.getPopularMovieList("en", 1);
        // The following line is RubyJava and needs to your help!
        results.getResults().map {|e| e.getPosterPath() };
        // or ... more RubyJava results.getResults().map(&:getPosterPath()); 

A bit more about # map / # collect in Ruby if you know a lot of Java but are not familiar with ruby. http://ruby-doc.org/core-1.9.3/Array.html#method-i-collect

The closest thing I've seen to answer this question from some quick look so far ... https://planet.jboss.org/post/java_developers_should_learn_ruby

They also look very close. http://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html

So Many Options: Functional Programming in Java

This is also Android ... Is there anything good that is available for Android developers out of the box and make this kind of programming easy? This is a functional programming style, right?

-

, : " for" ( ) " - ", , Java Ruby! , , " " : https://github.com/google/guava/wiki/FunctionalExplained. < - ( TL, DR ', , "", Guava ")

+4
2

map , .

collection.stream()
  .map(obj -> obj.someMethod())
  .collect(Collectors.toList()));

map , collect.

, , , :

http://winterbe.com/posts/2014/03/16/java-8-tutorial/

+2

( ).foreach() - , , java8 - , foreach().

http://www.mkyong.com/java8/java-8-foreach-examples/


, Android, , java8, .foreach(), -, . Java8, "", Streams.

0

Source: https://habr.com/ru/post/1620939/


All Articles