Java 匹配两个列表值并最终得到具有不同值的最终列表

Java 匹配两个列表值并最终得到具有不同值的最终列表,java,list,iterator,arraylist,Java,List,Iterator,Arraylist,我这里有个问题。我有两个列表,这两个列表都有一些共同的元素。 这些公共元素和值必须放在另一个列表中。这是非常烦人的要求 我的测试课程如下: import java.util.ArrayList; import java.util.List; public class Player { private int singleModeVal; private int doubleModeVal; private String mode; private

我这里有个问题。我有两个列表,这两个列表都有一些共同的元素。 这些公共元素和值必须放在另一个列表中。这是非常烦人的要求

我的测试课程如下:

    import java.util.ArrayList;
    import java.util.List;

public class Player {
    private int singleModeVal;
    private int doubleModeVal;
    private String mode;
    private String name;
    public Player(){}

    public String getName(){
        return name;
    }
    public void setName(String name){
        this.name = name;
    }
    public int getSingleModeVal(){
        return singleModeVal;

    }
    public void setSingleModeVal(int val1){
        this.singleModeVal=val1;
    }
    public int getDoubleModeVal(){
        return doubleModeVal;
    }
    public void setDoubleModeVal(int val2){
        this.doubleModeVal=val2;
    }
    public String getMode(){
        return mode;
    }
    public void setMode(String mode){
        this.mode = mode;
    }
    public List<Player> getSinglePlayerscoreList(){
        List<Player> singlePlayerscoreList = new ArrayList<Player>();
        for(int i=0;i<2;i++){
            Player player = new Player();
            player.setName("A");
            player.setMode("singlePlayerMode");
            player.setSingleModeVal(100);
            player.setDoubleModeVal(200);
            singlePlayerscoreList.add(player);
        }
        return singlePlayerscoreList;
    }
    public List<Player> getDoublePlayerscoreList(){
        List<Player> doublePlayerscoreList = new ArrayList<Player>();
        for(int i=0;i<2;i++){
            Player player = new Player();
            player.setName("B");
            player.setMode("doublePlayerMode");
            player.setSingleModeVal(300);
            player.setDoubleModeVal(400);
            doublePlayerscoreList.add(player);
        }
        return doublePlayerscoreList;
    }
}
import java.util.ArrayList;
导入java.util.List;
公开课选手{
私有int-singleModeVal;
私有int-doubleModeVal;
私有字符串模式;
私有字符串名称;
公共播放器(){}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
public int getSingleModeVal(){
返回singleModeVal;
}
公共无效设置SingleModeval(int val1){
这个.singleModeVal=val1;
}
public int getDoubleModeVal(){
返回doubleModeVal;
}
公共无效setDoubleModeVal(int val2){
这个.doubleModeVal=val2;
}
公共字符串getMode(){
返回模式;
}
公共无效设置模式(字符串模式){
这个模式=模式;
}
公共列表GetSinglePlayerCoreList(){
List SinglePlayerCoreList=新建ArrayList();

对于(inti=0;i有一个名为
retainal()
的方法,它执行完全相反的操作

因此,您可以执行以下操作:

// create copies of source list because retainAll() works in place
List<T> copy1 = new ArrayList<T>(one);
List<T> copy2 = new ArrayList<T>(two);
copy1.retainAll(two);
copy2.retainAll(one);
// now copy1 and copy2 contain common elements

// create collection of retained elements
List<T> retained = new ArrayList<T>();
retained.addAll(copy1);
retained.addAll(copy2);

// refresh content of copy1 and copy2 (it is abuse but ok for the example)
copy1 = new ArrayList<T>(one);
copy2 = new ArrayList<T>(two);
// remove all retained elements, so now both collection contain elements unique for these collections only
copy1.removeAll(retained);
copy2.removeAll(retained);

// create collection that contains all distinct elements.
List<T> distinct = new ArrayList<T>();

distinct.addAll(copy1);
distinct.addAll(copy2);
//创建源列表的副本,因为retainAll()可以在适当的位置工作
List copy1=新阵列列表(一个);
List copy2=新阵列列表(两个);
副本1.保留(两份);
副本2.保留(一份);
//现在,copy1和copy2包含公共元素
//创建保留元素的集合
保留的列表=新的ArrayList();
保留。添加全部(副本1);
保留。添加全部(副本2);
//刷新copy1和copy2的内容(这是滥用,但对于示例来说是可以的)
copy1=新阵列列表(一个);
copy2=新阵列列表(两个);
//删除所有保留的元素,因此现在两个集合都只包含这些集合的唯一元素
副本1.移除所有(保留);
副本2.移除所有(保留);
//创建包含所有不同元素的集合。
List distinct=new ArrayList();
distinct.addAll(copy1);
distinct.addAll(copy2);

为什么要在GetSinglePlayerCoreList和GetDoublePlayerCoreList中迭代for循环两次?这至少是为什么在showValue中迭代两次得到A和B。您的
Player
类需要
equals()
hashCode()
--如果没有这些方法,您就无法真正使用Collection API。然后,请检查Player实现Comparable,将启用:Set list=new TreeSet();@DavidBurstrom for循环是故意的,因为在我的例子中,rs.next()就是这样做的。
// create copies of source list because retainAll() works in place
List<T> copy1 = new ArrayList<T>(one);
List<T> copy2 = new ArrayList<T>(two);
copy1.retainAll(two);
copy2.retainAll(one);
// now copy1 and copy2 contain common elements

// create collection of retained elements
List<T> retained = new ArrayList<T>();
retained.addAll(copy1);
retained.addAll(copy2);

// refresh content of copy1 and copy2 (it is abuse but ok for the example)
copy1 = new ArrayList<T>(one);
copy2 = new ArrayList<T>(two);
// remove all retained elements, so now both collection contain elements unique for these collections only
copy1.removeAll(retained);
copy2.removeAll(retained);

// create collection that contains all distinct elements.
List<T> distinct = new ArrayList<T>();

distinct.addAll(copy1);
distinct.addAll(copy2);