匹配两个List值,最后得到一个具有不同值的最终List

时间:2011-11-20 11:11:52

标签: 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 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;
import java.util.Iterator;
import java.util.List;

public class Tester {
    private Player player = new Player();

    public static void main(String args[]){
        new Tester().showValue();
    }

    private void showValue(){
        List<Player> singlePlayerScore = new ArrayList<Player>();
        List<Player> doublePlayerScore = new ArrayList<Player>();
        singlePlayerScore = player.getSinglePlayerscoreList();
        doublePlayerScore = player.getDoublePlayerscoreList();
        List<Player> allScoreList = new  ArrayList<Player>();
        allScoreList.addAll(singlePlayerScore);
        allScoreList.addAll(doublePlayerScore);
            How do i iterate here, and print my data as:


 Name         singlePlayerScore  Double Player Score  TotalScore     
 A              100                   200                300     
 B              300                   400                700


     }
    }

}

当我迭代时,我得到A两次,其值和B相同。

是否有一种有效的方式可以按要求执行。

1 个答案:

答案 0 :(得分:1)

有一个名为retainAll()的方法可以完全相反的行动。

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

// 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);