java list集合属性值比较

比较两个list,如何找出相同属性id,其他属性,如name值不相同的数据?需要怎样重写equals吗?我用两次for循环,效率太低了!
public static List needupdatetdxxlist() {
List getoratdxxlist=getoratdxx();
List getresttdxxlist = getrestgctdxx();
List needupdatetdxx = new ArrayList();

            try {           

                for (int i = 0; i < getresttdxxlist.size(); i++) {
                    gctd gctd =new gctd();

                    for (int j = 0; j < getoratdxxlist.size(); j++) {



                        String DevChnId = getresttdxxlist.get(i).getDevChnId();
                        String DevChnName = getresttdxxlist.get(i).getDevChnName(); 
                        String Manufacturer = getresttdxxlist.get(i).getManufacturer();
                        double GpsX=getresttdxxlist.get(i).getGpsX();
                        double GpsY=getresttdxxlist.get(i).getGpsY();
                        int DevChnType=getresttdxxlist.get(i).getDevChnType();
                        int CameraType=getresttdxxlist.get(i).getCameraType();
                        String DevChnSn =getresttdxxlist.get(i).getDevChnSn();
                        int Direct=getresttdxxlist.get(i).getDirect();
                        int LaneNo=getresttdxxlist.get(i).getLaneNo();
                        String OrgName = getresttdxxlist.get(i).getOrgName();
                        String OrgCode =getresttdxxlist.get(i).getOrgCode();
                    if (DevChnId.equals(getoratdxxlist.get(j).getDevChnId()) && !DevChnName.equals(getoratdxxlist.get(j).getDevChnName()))
                    {
                        gctd.setDevChnId(DevChnId);
                        gctd.setDevChnName(DevChnName);
                        gctd.setManufacturer(Manufacturer);
                        gctd.setGpsX(GpsX);
                        gctd.setGpsY(GpsY);
                        gctd.setDevChnType(DevChnType);
                        gctd.setCameraType(CameraType);
                        gctd.setDevChnSn(DevChnSn);
                        gctd.setDirect(Direct);
                        gctd.setLaneNo(LaneNo);
                        gctd.setOrgName(OrgName);
                        gctd.setOrgCode(OrgCode);                           
                        needupdatetdxx.add(gctd);
                        LOGGER.debug("需要更新的设备通道:"+DevChnId+"  "+DevChnName);  
                        break;
                    }               
                  }


                }

                        } catch (Exception e) {

                               e.printStackTrace();
                        } 
            return needupdatetdxx;
        }
 效率比较高的方式是使用HashMap<int, Pair>
其中
class Pair
{
public 第一个List类型 Item1;
public 第二个List类型 Item2;
}

首先,遍历第一个list,将所有条目添加到以id为key,Pair为值的HashMap里面,作为Item1
然后遍历第二个List,判断是否在Hashmap里有对应的id,如果有,把自身添加到对应的Pair对象,作为Item2
最后遍历一下HashMap,将结果取出来就是你要的。

对于list,你只能两次for循环。

这样算法效率是O(n)

你那个效率是O(n^2)

 import java.util.*;

class Untitled {
    public static void main(String[] args) {
        ArrayList<A> list1 = new ArrayList<A>();
        list1.add(new A(1, "北京"));
        list1.add(new A(2, "上海"));
        list1.add(new A(3, "武汉"));
        list1.add(new A(4, "长沙"));
        list1.add(new A(6, "广州"));
        ArrayList<B> list2 = new ArrayList<B>();
        list2.add(new B(1, "Beijing"));
        list2.add(new B(2, "Shanghai"));
        list2.add(new B(5, "Changchun"));
        list2.add(new B(6, "Guangzhou"));
        list2.add(new B(7, "Hangzhou"));

        //方法1,两个循环
        System.out.println("method 1:");
        for (A a : list1)
        {
            for (B b: list2)
            {
                if (a.id == b.id) System.out.println(a.chn + "=" + b.eng);
            }
        }

        //方法2,用hashmap
        System.out.println("method 2:");
        HashMap<Integer, Pair> map = new HashMap<Integer, Pair>();
        for (A a : list1)
        {
            map.put(new Integer(a.id), new Pair(a.chn, null));
        }
        for (B b : list2)
        {
            if (map.containsKey(new Integer(b.id)))
            {
                Pair p = map.get(new Integer(b.id));
                p.eng = b.eng;
                System.out.println(p.chn + "=" + p.eng);
            }
        }

    }
}

class A
{
    public int id;
    public String chn;
    public A(int id, String name) { this.id = id; this.chn = name; }
}
class B
{
    public int id;
    public String eng;
    public B(int id, String name) { this.id = id; this.eng = name; }
}
class Pair
{
    public String chn;
    public String eng;
    public Pair(String a, String b) { chn = a; eng = b; }
}