我有以下事件对象定义:
@Data
class Event{
private int referenceId;
private int messageId;
private String comment;
public Event(int referenceId, int messageId, String comment) {
this.referenceId = referenceId;
this.messageId = messageId;
this.comment = comment;
}
}
我得到以下输入:
EventDTO event = new Event(1, 1, "comment");
EventDTO event1 = new Event(1, 2, "comment");
EventDTO event3 = new Event(1, 3, "comment");
EventDTO event4 = new Event(1, 4, "comment");
List<EventDTO> events = List.of(event, event1, event3, event4);
除了 messageId 属性之外,我拥有的所有输入都将具有相同的数据。
由于我需要将这些数据转换为另一个名为 EventMessages 的对象:
@Data
class ChangeReason{
private int id;
private String description;
public ChangeReason(int id, String description) {
this.id = id;
this.description = description;
}
}
@Data
class EventMessages{
private int referenceId;
private String comment;
private List<ChangeReason> reasons;
public EventMessages(int referenceId, String comment, List<ChangeReason> reasons) {
this.referenceId = referenceId;
this.comment = comment;
this.reasons = reasons;
}
}
ChangeReason id 是 Event 类的 messageId
问题是 Event 的 N 个对象需要映射到只是1 个 EventMessages 对象。我确信传入事件中的所有属性都与 messageId 的预期相同。
我不太喜欢像这样获取第一个对象的解决方案:
List<ChangeReason> reasons = events.stream().map(event -> new ChangeReason(event.getMessageId(), "")).collect(Collectors.toList());
EventMessages eventMessages = new EventMessages(events.get(0).getReferenceId(), events.get(0).getComment(), reasons);
我不太喜欢这个实现,我想知道是否有更好的方法来实现这一点。
我试图获得以下数据结构场景:
地图<活动、列表<整数> > eventsWithMessages
因此,关键事件将是来自的对象,我将检索所有对象中常见的注释和referenceId信息,该值将是收集的事件列表的不同messageId列表我正在尝试使用lambdas来获取它和流
这样做是行不通的:
Map<Event, List<Integer>> eventMessages = events.stream().collect
(Collectors.groupingBy(e -> e, Collectors.mapping(e -> e.getMessageId(), Collectors.toList())));
由于 groupingBy 键比较是由对象完成的,并且 messageId 的属性不同......
I have the following Event object definition:
@Data
class Event{
private int referenceId;
private int messageId;
private String comment;
public Event(int referenceId, int messageId, String comment) {
this.referenceId = referenceId;
this.messageId = messageId;
this.comment = comment;
}
}
I am given the following input:
EventDTO event = new Event(1, 1, "comment");
EventDTO event1 = new Event(1, 2, "comment");
EventDTO event3 = new Event(1, 3, "comment");
EventDTO event4 = new Event(1, 4, "comment");
List<EventDTO> events = List.of(event, event1, event3, event4);
All the input I have will have the same data except the messageId property.
Since I need to transform this data into another object called EventMessages:
@Data
class ChangeReason{
private int id;
private String description;
public ChangeReason(int id, String description) {
this.id = id;
this.description = description;
}
}
@Data
class EventMessages{
private int referenceId;
private String comment;
private List<ChangeReason> reasons;
public EventMessages(int referenceId, String comment, List<ChangeReason> reasons) {
this.referenceId = referenceId;
this.comment = comment;
this.reasons = reasons;
}
}
The ChangeReason id is the messageId of Event class
The problem is that N objects of Event need to be mapped into just 1 object of EventMessages. I am sure that all properties among incoming Events are going to be the same expect for the messageId.
I do not really like the solution of getting the first object like this:
List<ChangeReason> reasons = events.stream().map(event -> new ChangeReason(event.getMessageId(), "")).collect(Collectors.toList());
EventMessages eventMessages = new EventMessages(events.get(0).getReferenceId(), events.get(0).getComment(), reasons);
I dont really like this implementation and i was wondering if there is a better way of achiveing this.
I am trying to get the following scenario of data structure:
Map<Event,List< Integer> > eventsWithMessages
So the key Event will be the object from I will retreive the comment and referenceId information which is common among all objects and the value would be the Collected different List of messageIds of events List I am trying to get this using lambdas and streams
Doing this wouldn't work:
Map<Event, List<Integer>> eventMessages = events.stream().collect
(Collectors.groupingBy(e -> e, Collectors.mapping(e -> e.getMessageId(), Collectors.toList())));
Since the groupingBy key comparison is done by the object and the property of messageId is different...
发布评论
评论(1)
找到了解决方案。我开始使用 mergeOperators,我认为这可能是我过去遇到的这个问题的解决方案。我留下代码:
首先我需要为 Event 类实现 EqualHashCode:
然后我在 MergeFunction toMap() 上使用了此代码
Found a solution. I started working with mergeOperators and I thought this could be the solution for this problem I had in the past. I leave the code:
First I needed to implement an EqualHashCode for the Event class:
Then i used this code on MergeFunction toMap()