开发者

Readable way of knowing if there is at least one difference between two Map<Long, Integer> in Java?

I have two maps of type Map<Long, Integer>, one named "oldValues" representing the old state of an object, and the other named "newValues" representing the new开发者_如何学Go state of the same object.

Is there a simple & readable way of knowing if the old state and the new state are different (ie. if the state has changed)?

Ideally, I'd like some "java.utils" or "Apache Commons" library function like:

hasChanged = !MapUtils.diff(oldValues, newValues).isEmpty();

Note: I looked for, but didn't find one.

Thanks.


Doese'nt the equals method work?


Equals seems like a bad idea because examining the entire map for equality sounds expensive. You also would need to lock both maps to do the check to make it threadsafe.

There's an idea used in ConcurrentHashMap where the map uses a counter to detect changes. If this were public you could just compare the counter in the 2 maps for equality. It's not, but you could accomplish the same thing with a dirty flag or possibly a class representing the object state that wraps the map and simplifies the equality checking.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜