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 つある場合zoneIdtempId (i,e) 新しいリストに含まれている必要があります。

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

次のようにヘルパー クラスを作成します。

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

}

リストを反復処理して、エントリをグループ化しtempIdzoneIdtempValues

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複数のエントリがあるかどうかを確認するために繰り返します。ある場合は、から削除しますtempIdzoneIdchecklist

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()を実装していることを確認してくださいTempVo

于 2012-06-15T05:10:16.530 に答える
0

私はあなたのために何かを書きました。クラス TempVO を実装し、入れ子になったループを使用してメイン関数 (プログラムのエントリ ポイント) のリストを反復処理しました。tempValue が「check」の場合、すべての 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 に答える
0

equals()でメソッドを実装しTempVOます。

次に、 のSet代わりにa を使用し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 を実装することは、1 つの解決策です。

別の配列リストが必要な場合は、これを使用します。

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 に答える