I found a way to transform List<Object> to a Map<Integer, Object>
but I need
List<Object> to Map<Integer, List<Object>
For example I have class
class Movie {
public int rank;
public String desc;
public Movie(int rank, String desc) {
this.rank = rank;
this.desc = desc;
}
}
And list:
List<Movie> movies = new ArrayList<Movie>();
movies.add(new Movie(1, "The Movie 0"));
movies.add(new Movie(2, "The Movie 1"));
movies.add(new Movie(2, "The Movie 2"));
What I would like to have is a Map of List for each rank..
List<Movie> -> Map<Integer (rank), List<Movie>>
with Guava I could do simple transformation like that
Map<Integer,Movie> mappedMovies = Maps.uniqueIndex(movies, new Function <Movie,Integer> () {
public Integer apply(Movie from) {
return from.getRank();
}});
But not to Map<Integer, List<Movie>>
I could find only on post regarding it
Java: how to transform from List<T> to Map<f1(T), List(f2(T))> without iterating
However one approach is using Java 8 and second approach for some reason returns ListMultimap<Integer, String>