比较两种不同类型的对象列表

时间:2015-01-19 15:34:04

标签: java

我有两个不同类型的对象但持有相似的值。我想从列表中找到添加和删除的值。 代码是:

List<SelectItem> sourceList = new ArrayList<SelectItem>(TypeA);
List<SelectListItems> destinaitonList = new ArrayList<SelectListItems>(TypeB);

该列表包含相同的字段,但包含不同的对象。

sourceList.getItemValue();
sourceList.getItemLabel();


destinaitonList.getItemValue();
destinaitonList.getItemLabel();

我想比较标签值,想要知道添加到目的地列表的内容以及从源列表中删除的内容,与源列表进行比较。

说:

SourceList (A, B , D, E)
DestinationList (A, G, H, K )

Result : Added G, H, K
Removed : B, D, E

到目前为止,我已经尝试过这个:

List < SelectItem > tempList1 = new ArrayList < SelectItem > ();
List < SelectListItems > tempList2 = new ArrayList < SelectListItems > ();
for (SelectItem s1: sourceList) {
    for (SelectListItems d1: destinaitonList) {
        if (!s1.getItemLabel().equalsIgnoreCase(d1.getItemLabel())) {
            tempList1.add(s1);
        } else {
            tempList2.add(d1);
        }
    }
}

3 个答案:

答案 0 :(得分:0)

只需使用contains()方法:

ArrayList<Character> l1 = new ArrayList<>(Arrays.asList('A', 'B', 'D', 'E'));
ArrayList<Character> l2 = new ArrayList<>(Arrays.asList('A', 'G', 'H', 'K'));

ArrayList<Character> added = new ArrayList<>();
ArrayList<Character> removed = new ArrayList<>();

for (int i = 0; i < l2.size(); i++)
    if (!l1.contains(l2.get(i)))
        added.add(l2.get(i));

for (int i = 0; i < l1.size(); i++)
    if (!l2.contains(l1.get(i)))
        removed.add(l1.get(i));

即使两个列表包含不同类型的对象,此解决方案仍然有效,只需对Arraylist<?>added列表使用removed即可。以下是我使用ArrayList<?>作为两个开始列表的示例:

ArrayList<?> l1 = new ArrayList<>(Arrays.asList('A', 1, 'B', 2));
ArrayList<?> l2 = new ArrayList<>(Arrays.asList('A', 3, 'C', 1));

for (int i = 0; i < l2.size(); i++)
    if (l1.contains(l2.get(i)))
        System.out.print(l2.get(i));

输出将是:"A1"

如果两个列表不同要比较的一个公共字段,请使用两个嵌套循环:

import java.util.ArrayList;
import java.util.Arrays;


public class Main
{
    static class A
    {
        private int number;
        private char character;

        public A (int number, char character) {
            this.number = number;
            this.character = character;
        }

        public int getNumber() {
            return number;
        }

        public char getCharacter() {
            return character;
        }
    }


    static class B
    {
        private int number;
        private String string;

        public B (int number, String string) {
            this.number = number;
            this.string = string;
        }

        public int getNumber() {
            return number;
        }

        public String getString() {
            return string;
        }
    }


    public static void main(String[] args) {
        ArrayList<A> l1 = new ArrayList<>(Arrays.asList(new A(1, 'a'), new A(2, 'b'), new A(3, 'c'), new A(4, 'd')));
        ArrayList<B> l2 = new ArrayList<>(Arrays.asList(new B(1, "a"), new B(-2, "b"), new B(-3, "c"), new B(4, "d")));

        for (int i = 0; i < l1.size(); i++)
            for (int j = 0; j < l2.size(); j++)
                if (l1.get(i).getNumber() == l2.get(j).getNumber())
                    // l1.get(i) and l2.get(j) are equals
    }
}

答案 1 :(得分:0)

你可以做这样的事情

Set < SelectItem > addedList = new HashSet < SelectItem > ();
Set < SelectListItems > removedList = new HashSet < SelectListItems > ();

for (SelectItem s1: sourceList) {
        if(!destinaitonList.contains(s1)){
            removedList.add(s1)
        }
   }
}
 for (SelectListItems d1: destinaitonList) {
        if(!sourceList.contains(d1)){
            addedList.add(d1);
        }
}

为了适应您的设计问题,您可以执行类似的操作(在一种情况下更改了for循环的顺序)

for(int j=0; i < sourceList.size(); i++) {
    for(int i=0; i< destinationList.size(); i++){ 
        boolean found=false;
            if(sourceList.get(j).getItemValue().equals(destinaitonList.get(i).getItemVa‌​lue()))
                found=true;
        if(!found){
            System.out.println(sourceList.get(j).getItemValue()+" not found in destination list so it is removed")
        }   
    }
}

for(int i=0; i< destinationList.size(); i++){ 
    for(int j=0; i < sourceList.size(); i++) {
        boolean found=false;
            if(destinaitonList.get(i).getItemVa‌​lue().equals(sourceList.get(j).getItemValue()))
                found=true;
        if(!found){
            System.out.println(destinaitonList.get(i).getItemVa‌​lue()+" not found in source list list so it is added")
        }   
    }
}

这样就可以了,在它成功运行后尝试在一个双循环中进行,为你做一些功课:)

希望这有帮助!

祝你好运!

答案 2 :(得分:0)

Thi is what I did and it worked smooth for add and delete. List<String> sourList = new ArrayList<String>();
        sourList.add("BANK");
        sourList.add("BENE");
        sourList.add("ADVICE");

        List<String> destList = new ArrayList<String>();
        destList.add("BANK");
        destList.add("ADVICE");
        destList.add("ROLL");
        destList.add("MOBILE");

        for(String sList : sourList){
            boolean present = false;
            for(String dList : destList){
                if(sList.equalsIgnoreCase(dList)){
                    present = true;
                    break;
                }
            }
            if(!present)
                System.out.println(sList);          
        }


    }