我有一个
列表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
的条目,如果它包含同一个zoneId
和tempId
的2个条目
(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)
在equals()
中实施TempVO
方法。
然后使用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);
答案 1 :(得分: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);
答案 2 :(得分:0)
创建一个帮助程序类,如下所示
class ZoneTempHelper{
private long zoneId;
private long tempId;
//implement equals() & hashCode() + accessor
}
现在遍历列表,将条目按tempId
,zoneId
分组到tempValues
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
&amp;的条目是否有多个值zoneId
的{{1}},如果已移除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"))
}
}
&amp; hashcode()
equals()
答案 3 :(得分: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);
}
}
}