0

我有一个清单

List < TempVO> lstTemp = new ArrayList < TempVO> ();

TempVO是一个包含

zoneId,tempId and tempValue

现在列表将包含如下所述的条目

zoneId tempId tempValue
-----------------------
 2 /     1    /  check    
 2   /    3     /check  
 2    /   4   /  check  
 2     /  4   /  entered1  
 3    /   5   /  check  
 3     /  8    / primary  
 3    /   6  /   check  
 3   /    8   /  check  

我的要求是tempValue= check从列表中删除该条目,如果它有 2 个相同的条目,zoneId并且tempId (即)我的新列表应该包含

zoneId tempId tempValue
-----------------------
 2 /     1    /  check    
 2   /    3     /check    
 2     /  4   /  entered1  
 3    /   5   /  check  
 3     /  8    / primary  
 3    /   6  /   check  

我该怎么做呢?

4

4 回答 4

0

实现一个equals()方法TempVO

然后使用 aSet而不是List.

public class TempVO {
    private int zoneId;
    private int tempId;
    private String tempValue;

    // getters & setters

    public boolean equals(Object obj) {
        TempVO t = (TempVO) obj;
        if (zoneId != t.zoneId) {
            return false;
        }
        if (tempId != t.tempId) {
            return false;
        }
        if (tempValue == null) {
            return t.tempValue == null
        }
        return tempValue.equals(t.tempValue);
    }
}

Set<TempVO> tempSet = new HashSet<TempVO>();
// now add to the set; the set will automatically remove duplicates
tempSet.add(myTemp);
于 2012-06-15T05:03:02.197 回答
0

实现 equals 和 set 是一种解决方案。

如果您只想要另一个数组列表,请使用以下命令:

List < TempVO> lstTemp = new ArrayList < TempVO> ();
List < TempVO> toBeRemoved = new ArrayList < TempVO> ();
for(TempVO temp : lstTemp) {
    if(condition) {
         toBeRemoved.add(temp);
    }
}
lstTemp.removeAll(toBeRemoved);
于 2012-06-15T05:06:50.620 回答
0

创建一个辅助类如下

class ZoneTempHelper{
  private long zoneId;
  private long tempId;
  //implement equals() & hashCode() + accessor

}

现在遍历列表,将条目按,tempId分组zoneIdtempValues

Map<ZoneTempHelper, List<String>> map = new HashMap<ZoneTempHelper, List<String>>();
for(TempVO tempVO: list){
   ZomeTempHelper helper = new ZoneTempHelper(tempVo.getTempID(), tempVO.getZoneId());
   List<String> tempValues = map.get(helper);
   if(tempValues == null){
        tempValues = new ArrayList<String>();
        map.put(helper, tempValues);
   }
   tempValues.add(tempVo.getTempVAlue());

}

现在遍历Map以检查是否有多个相同tempId&zoneId具有值的条目check,如果是,则从list

for(ZoneTempHelper helper: map.keySet()){
   List<String> values = map.get(helper);
   if(values.size() == 2 && values .contains("check")){ 
      list.remove(new TempVo(helper.getTempId(), helper.getZoneId(), "check"))
   }
}

确保您实施正确的hashcode()& equals()inTempVo

于 2012-06-15T05:10:16.530 回答
0

我为你写了一些东西。我实现了 TempVO 类,并使用嵌套循环遍历了主函数(我的程序入口点)中的列表。如果 tempValue 是“检查”,我会检查每个 TempVO。如果是这样,我检查列表中是否有另一个具有相同 zoneId 和 tempId 的 TempVO。如果是这样,我不打印 TempVO。您可以在我的代码中关注它:

import java.util.ArrayList;
import java.util.List;
class TempVO {
    private int zoneId;
    private int tempId;
    private String tempValue;
    public TempVO(int zoneId, int tempId, String tempValue) {
        super();
        this.zoneId = zoneId;
        this.tempId = tempId;
        this.tempValue = tempValue;
    }
    public int getZoneId() {
        return zoneId;
    }
    public void setZoneId(int zoneId) {
        this.zoneId = zoneId;
    }
    public int getTempId() {
        return tempId;
    }
    public void setTempId(int tempId) {
        this.tempId = tempId;
    }
    public String getTempValue() {
        return tempValue;
    }
    public void setTempValue(String tempValue) {
        this.tempValue = tempValue;
    }
    @Override
    public String toString() {
    return "zonedId: " + this.zoneId + " | tempId: " + this.tempId + " | tempValue: " + this.tempValue;
    }
}
public class Main { 
    public static void main(String[] args) {
        // initialize list
        List<TempVO> tempVOs = new ArrayList<>();
        tempVOs.add(new TempVO(2, 1, "check"));
        tempVOs.add(new TempVO(2, 3, "check"));
        tempVOs.add(new TempVO(2, 4, "check"));
        tempVOs.add(new TempVO(2, 4, "enterd1"));
        tempVOs.add(new TempVO(3, 5, "check"));
        tempVOs.add(new TempVO(3, 8, "primary"));
        tempVOs.add(new TempVO(3, 6, "check"));
        tempVOs.add(new TempVO(3, 8, "check"));
        // only print values wherein interested
        for(int i = 0; i < tempVOs.size(); i++) {
            TempVO outerTempVO = tempVOs.get(i);
            boolean keep = true;
            if("check".equals(outerTempVO.getTempValue())) {
                for(int j = 0; j < tempVOs.size(); j++) {
                    if(i != j) {
                        TempVO innerTempVO = tempVOs.get(j);
                        if(outerTempVO.getTempId() == innerTempVO.getTempId() && outerTempVO.getZoneId() == innerTempVO.getZoneId()) {
                            keep = false;
                            break;
                        }
                    }
                }
            }
            if(keep)
                System.out.println(outerTempVO);
        }
    }
}
于 2012-06-15T05:42:03.223 回答