0

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?

callmedope
  • 83
  • 1
  • 9
  • @ernest-k you are the best with this cases, can u give me an advice? – callmedope Nov 29 '18 at 10:19
  • implement a comparator for `Event` or is this no option for you? – UninformedUser Nov 29 '18 at 10:23
  • We'll let only ernest-k answer this then... – Nicholas K Nov 29 '18 at 10:24
  • Can you clarify, if you should check if the Item Values are same or the Event logs have the same Item Values, or everything needs to be the same, just the Events are stored in different manner ? – Nenad Vichentikj Nov 29 '18 at 10:26
  • @NicholasK sorry, if you can explain me right way - you're welcome :) coz i'm not good with difficult comparasions, my brain generates not good code for this example) – callmedope Nov 29 '18 at 10:27
  • You want to compare the 2 lists for equality right? First of all, you need to implement Comparator, adding a compareTo method to Event. Then when you run equals between the collections it will use the compareTo to compare them. In the compareTo method, you can use methods from ArrayList such as retainAll, as explained [here](https://stackoverflow.com/a/2762137) – fpezzini Nov 29 '18 at 10:31
  • @NenadVichentikj every event(by specs) contains exact items (type=1000: server_id=?, user_id=?, for example). I have "should" list, that has events and items like in specs. So i should check if logged events are ok: all of them logged (in any order) and every event has exact Items as in specs – callmedope Nov 29 '18 at 10:34
  • From the context of your previous question, I think in this case you should just override `equals` in both `Event` and `Item` (checking order of `Event.items`), then check that both lists have the same size, and that each `should.contains(event)` for each `event` in `logged` returns `true` – ernest_k Nov 29 '18 at 14:22
  • @ernest_k ok, thnx :) – callmedope Nov 30 '18 at 09:28

0 Answers0