2014-08-29 89 views
0

我从MasterList表查询获取以下记录:迭代和比较Java列表

id listName   listValue 
1 Type of Repository Animal Specimen 
2 Type of Repository Human Specimen 
3 Type of Repository Environmental Specimen 
4 Type of Repository Microorganism Culture Collection 
5 Type of Repository Vegetal Specimen 
6 Type of Repository Other 

每个记录/行是一个对象,并存储为对象的列表下面的列表

List<MasterList> typeOfRepositoryMasterList 

同样,每个对象都有对应的getter方法可用列,如obj.getListValue();

然后从其他查询到biobankList表我有如下记录:

biobankId listName   listValue 
1  Type of Repository Animal Specimen 
1  Type of Repository Human Specimen 
1  Type of Repository Microorganism Culture Collection 
1  Type of Repository Vegetal Specimen 

同样这些记录也可以作为对象的列表作为

List<biobankList> typeOfRepositoryBiobankList 

每个对象在这里也有相应的getter方法。

我想要做的是,对于第一个记录集中的所有listValue,如果在第二个记录集中有相同的listValue,则将其添加到新列表中,如selectedList。在第二个记录集中不存在的两个listValue应该被添加到availableList。图片可能更好地解释。 enter image description here

for(MasterList attributeMaster: typeOfRepositoryMasterList){ 
     boolean selected = false; 
     for(biobankList attribute: typeOfRepositoryBiobankList){ 
      if(attributeMaster.getListValue().equals(attribute.getListValue())){ 
       System.out.println("equal"); 
       selected = true; 
       selectedList.add(new KeyValuePair(
         attribute.getListName()+"_"+attribute.getListValue(), attribute.getListValue())); 
       break; 
      } 
      if(!selected){ 
       System.out.println("not equal"); 
       availableList.add(new KeyValuePair(
          attributeMaster.getListName()+"_"+attributeMaster.getListValue(), attributeMaster.getListValue())); 

      } 
     } 


    } 

的查询工作正常,但这种嵌套的for循环代码显然是不能工作,因为它增加了相同的值多次。

编辑:我试着添加boolean但仍然存在问题。我在selectedList中获得了正确的值,但显然availableList包含重复的值。这是截图。 enter image description here

+0

添加列表。包含(对象o)你的if – user3487063 2014-08-29 13:38:47

+0

我也试过,但它仍然不起作用,因为我在availableList中获得重复的值。 – SASM 2014-08-29 13:44:52

+0

看看我的答案我认为你有问题与availableList不是吗? – 2014-08-29 13:50:04

回答

1

我相信你的问题是与else语句。每次调用内部循环并且没有匹配时(属性和attributeMaster都有不同的listValue),代码会将该listValue添加到availableList。

你必须改变你的逻辑是这样的:

定义布尔第二循环外,并将其设置为false。每当发现匹配(在break;之前)行将其设置为true时。

在第二个循环之外检查该布尔值是否为真(意味着当前'attributeMaster'已匹配)不要将其添加到availableList。这里是正确的代码:

for(MasterList attributeMaster: typeOfRepositoryMasterList){ 
    boolean found = false; 
    for(biobankList attribute: typeOfRepositoryBiobankList){ 
     if(attributeMaster.getListValue().equals(attribute.getListValue())){ 
      System.out.println("equal"); 
      selectedList.add(new KeyValuePair(attribute.getListName()+"_"+attribute.getListValue(), attribute.getListValue())); 
      found = true; 
      break; 
     } 
    } 
    if (!found) { 
     System.out.println("not equal"); 
     availableList.add(new KeyValuePair(attributeMaster.getListName()+"_"+attributeMaster.getListValue(),attributeMaster.getListValue())); 

    } 
} 
1

它更好地重新思考整个方法来解决问题。

我会创建两套,每种类型:

Set<String> typeOfRepositoryMasterList 
Set<String> typeOfRepositoryBiobankList 

之后,您可以通过轻松创建两个期望集:

//Selected list is an intersection of the two sets 
selectedList = new HashSet<String>(typeOfRepositoryMasterList); 
selectedList.retainAll(typeOfRepositoryBiobankList); 

//Available list can be constructed in a similar fashion 
availableList = new HashSet<String>(typeOfRepositoryMasterList); 
availableList.addAll(typeOfRepositoryBiobankList); 
availableList.removeAll(selectedList);