如何检查两个 ArrayList 是否不同,我不在乎发生了什么变化 [英] How can I check if two ArrayList differ, I don't care what's changed

查看:23
本文介绍了如何检查两个 ArrayList 是否不同,我不在乎发生了什么变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查两个 ArrayList 是否彼此不同?我不在乎有什么不同,我只想知道它们是否相同.

How can I check if two ArrayLists differ from one another? I don't care what's the difference, I just want to know if they're not the same.

我每分钟都从数据库中获取分数列表,并且只有当我获取的分数列表与我一分钟前获取的分数列表不同时,我才会将其发送给客户端.

I'm fetching scores list from a database every minute, and only if the scores list that I fetched is different from the one I fetched a minute ago I want to send it to the client.

现在 ArrayList 的值实际上是我创建的一个类(包含名称、等级、等级、分数).

Now the value of the ArrayList is actually a class that I created (that contains name, lvl, rank, score).

我需要在上面实现 equals() 吗?

Do I need to implement equals() on it?

推荐答案

这里有一个简单的方法来检查 2 个数组列表是否包含相同的值,而不管它们的顺序.

Here's a simple method that checks if 2 Array Lists contain the same values regardless their order.

 //the name of the method explains it well...
    public boolean isTwoArrayListsWithSameValues(ArrayList<Object> list1, ArrayList<Object> list2)
    {
        //null checking
        if(list1==null && list2==null)
            return true;
        if((list1 == null && list2 != null) || (list1 != null && list2 == null))
            return false;

        if(list1.size()!=list2.size())
            return false;
        for(Object itemList1: list1)
        {
            if(!list2.contains(itemList1))
                return false;
        }

        return true;
    }

这篇关于如何检查两个 ArrayList 是否不同,我不在乎发生了什么变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆