0

I have Movie entity as shown below. I need to group all entities by movie code, so implemented as below. Now I need to sort the values of hash map by scan date time in descending order {latest first}. Can I do it during grouping-by process. Here Key is List of Movie entities.

 // Movie Entity Model Class
 public MovieEntity {

          String movieCode;
          OffsetDateTime scanDateTime;
          String id;
      }

// Fetch all movies from other data source.
List<MovieEntity> movieEntities = fetchMovies();

// Convert fetched data by grouping-by movie code
Map<String, List<MovieEntity>> movieResponseEntityMap =
                        movieEntities
                          .stream()
                          .collect(Collectors.groupingBy(MovieEntity::getMovieCode));
Naman
  • 27,789
  • 26
  • 218
  • 353

1 Answers1

0

You should use a Date type for scanDateTime with implements Comparable.

Map<String, List<MovieEntity>> movieResponseEntityMap =
                        movieEntities
                         .stream()
                         .sorted((x,y) -> x.getScanDateTime.compareTo(y.getScanDateTime()))
                         .collect(Collectors.groupingBy(MovieEntity::getMovieCode));
Hans
  • 121
  • 2
  • 7