How to compare list of objects with nested lists?

陌路散爱 提交于 2019-12-24 20:03:09

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!