-2

I have an embeddable class with entity

@Embeddable
public class EventId implements Serializable {
    private Long id;
    private Date updateDateTime;
}

@Entity
public class Event {
    @EmbeddedId
    private EventId id;
    private String name;

}

How to get below map of EventId.id(Long),Event from steam for given event list

List<Event> events;
Map<Long, Event> eventMap = events.stream... ?
Dev Dev
  • 55
  • 1
  • 7
  • 2
    What values shall the key in the map have? Did you try anything yourself? What approach would you take if you would use "traditional" loops? – cyberbrain Mar 26 '23 at 16:58
  • https://mkyong.com/java8/java-8-collectors-groupingby-and-mapping-example/ – xerx593 Mar 26 '23 at 17:21
  • What exactly is `@Embeddable`? – Basil Bourque Mar 26 '23 at 18:16
  • @cyberbrain Updated question for key, it is id of embeddable class – Dev Dev Mar 26 '23 at 19:55
  • @BasilBourque definition of emeddable https://stackoverflow.com/questions/21143707/what-is-difference-between-entity-and-embeddable – Dev Dev Mar 26 '23 at 19:57
  • Add tags for the frameworks in question. – Basil Bourque Mar 26 '23 at 20:15
  • 2
    The annotations are really superfluous to the question. I would take them out of the example. – Tim Moore Mar 26 '23 at 21:41
  • I think that you want to process data in memory that should rather be processed by the database. Try to write a query that provides you with that data, especially when you have to process multiple events this will probably lead to better performance and save you some Java heap space. – cyberbrain Mar 27 '23 at 08:29

1 Answers1

1

I guess your purpose is to sort List<Event> events by EventId.id so you can use Collectors.groupingBy like this:

Map<Long, List<Event>> collect = events.stream().collect(Collectors.groupingBy(Event -> Event.getId().getId()));