问题
I have List of log events that I shoud check, that they are the same as should be. There is structure of Event object:
public class Event {
public String type;
public List<Item> items;
public Event(String type, List<Item> items) {
this.type = type;
this.items = items;
}
}
public class Item {
public String id;
public String value;
public Item(String id, String value) {
this.id = id;
this.value = value;
}
}
List<Event> should = asList(
new Event("1000", asList(new Item("server_id", "1"), new Item("user_id", "11"))),
new Event("1000", asList(new Item("server_id", "2"), new Item("user_id", "22"))));
List<Event> logged = asList(
new Event("1000", asList(new Item("server_id", "2"), new Item("user_id", "22"))),
new Event("1000", asList(new Item("server_id", "1"), new Item("user_id", "11"))));
I've got an answer that i can do this with such code:
List<String> actualValues = logged.stream().flatMap(l -> l.items.stream())
.map(i -> i.value).collect(Collectors.toList());
List<String> shouldValues = should.stream().flatMap(l -> l.items.stream())
.map(i -> i.value).collect(Collectors.toList());
boolean logMatch = actualValues.equals(shouldValues);
It's good, but i found problem, logged Event's could have any order (server sometimes mixes them), but List<Item>
in Event should have same order, so I' cant use Set<String>
.
How should I compare this Events in proper way?
来源:https://stackoverflow.com/questions/53536678/how-to-compare-list-of-objects-with-nested-lists