2012-06-15 102 views
0

迭代一個列表,我這是一個列表需要邏輯在Java

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 en嘗試對同一zoneIdtempId (I,E)我的新名單應該包含

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

我如何做到這一點?

+0

如前所述,這個問題似乎沒有解決您的問題的更廣泛的應用。你能否重新翻譯它,使它對更廣泛的受衆變得更有用? –

+0

假設你在你的arrayList中有** tempValue = primary **。那麼同樣的檢查也適用於它,或者你只需​​要** tempValue = check **。 –

回答

0

實施適當的hashcode() & equals()我寫的東西了爲你。我實現了TempVO類,並使用嵌套循環遍歷主函數(我的程序入口點)中的那些列表。如果tempValue是「check」,我檢查每個TempVO。如果是這樣,我檢查列表中是否有另一個TempVO具有相同的zoneId和tempId。如果是這樣,我不打印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); 
     } 
    } 
} 
0

TempVO中實施equals()方法。然後用Set代替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); 
+0

對不起。我是java的新手..請你詳細說明一下嗎? – user1372469

0

實現equals和set是一個解決方案。

如果你想只是一個ArrayList中,使用此:

List < TempVO> lstTemp = new ArrayList < TempVO>(); 
List < TempVO> toBeRemoved = new ArrayList < TempVO>(); 
for(TempVO temp : lstTemp) { 
    if(condition) { 
     toBeRemoved.add(temp); 
    } 
} 
lstTemp.removeAll(toBeRemoved); 
+0

這個解決方案的問題在於,'condition'很難實現。它會像'lstTemp.contains(temp)'一樣。然而,這已經在'Set'中完成了,並且在那裏表現更好。因此,儘管TO詢問了「List」,但我不會在這種情況下使用List。 –

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

} 

現在迭代thr ough Map檢查是否存在與價值check多個條目相同tempId & zoneId,如果是從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")) 
    } 
} 

刪除確保在TempVo