如何从Java中的ArrayList中删除特定的bean对象?

时间:2014-07-05 19:58:18

标签: java collections arraylist

我想从ArrayList中删除特定的bean对象。 我使用删除 removeAll 方法从ArrayList中删除对象元素,但不删除元素。

例如,假设下面的代码,

ArrayList<SystemDetailData> systemDetails = new ArrayList<SystemDetailData>();
SystemDetailData data = new SystemDetailData();
data.setId("1");
data.setName("abc");
data.setHost("192.168.1.2");
systemDetails.add(data);

data = new SystemDetailData();
data.setId("2");
data.setName("asd");
data.setHost("192.168.1.45");
systemDetails.add(data);
System.out.println("Before remove : " + systemDetails);

ArrayList<SystemDetailData> systemDetail = new ArrayList<SystemDetailData>();
SystemDetailData data = new SystemDetailData();
data.setId("1");
data.setName("abc");
data.setHost("192.168.1.2");
systemDetail.add(data);
System.out.println("Old data :" + systemDetail);

//Remove object from arraylist - method1
systemDetails.removeAll(systemDetail);


//Remove object from arraylist - method2
systemDetails.removeAll(systemDetail.getId());
systemDetails.removeAll(systemDetail.getName());
systemDetails.removeAll(systemDetail.getHost());
System.out.println("After remove : "+systemDetails);

Bean类:

public class SystemDetailData extends BusinessData {

/**
 * 
 */
private static final long serialVersionUID = 1L;
private static final String DOMAIN_NAME = "domainName";
private static final String HOST_NAME = "hostName";
private static final String USER_NAME = "userName";
private static final String PASSWORD = "password";
private static final String INDEX = "index";

private BigInteger index;
private String domainName;
private String hostName;
private String userName;
private String password;


public BigInteger getIndex() {
    return (BigInteger) get (INDEX);
}

public void setIndex(BigInteger index) {
    set (INDEX, index);
    this.index = index;
}

public String getDomainName() {
    return (String) get(DOMAIN_NAME).toString();
}

public void setDomainName(String domainName) {
    set (DOMAIN_NAME, domainName);
    this.domainName = domainName;
}

public String getHostName() {
    return (String) get (HOST_NAME);
}

public void setHostName(String hostName) {
    set (HOST_NAME, hostName);
    this.hostName = hostName;
}

public String getUserName() {
    return (String) get (USER_NAME);
}

public void setUserName(String userName) {
    set (USER_NAME, userName);
    this.userName = userName;
}

public String getPassword() {
    return (String) get (PASSWORD);
}

public void setPassword(String password) {
    set (PASSWORD, password);
    this.password = password;
}

@Override
public String toString() {
    return "SystemDetailData [index=" + index + ", domainName="
            + domainName + ", hostName=" + hostName + ", userName="
            + userName + ", password=" + password + "]";
}

@Override
public String getKeyValue() {
    String value = "";
    if (index != null) {
        value = value + "INDEX =" + index + ";";
    }
    if (domainName != null) {
        value = value + "DOMAIN_NAME =" + domainName + ";";
    }
    if (userName != null) {
        value = value + "USER_NAME =" + userName + ";";
    }
    if (hostName != null) {
        value = value + "HOST_NAME =" + hostName + ";";
    }
    if (password != null) {
        value = value + "PASSWORD =" + password + ";";
    }
    return value;
}


}

我得到了以下输出:

Before remove : [SystemDetailData [index=1, Name=abc, host=192.168.1.2], SystemDetailData [index=2, Name=asd, host=192.168.1.45]]
Old data : [SystemDetailData [index=1, Name=abc, host=192.168.1.2]]

After remove : [SystemDetailData [index=1, Name=abc, host=192.168.1.2], SystemDetailData [index=2, Name=asd, host=192.168.1.45]]

我想要低于输出:

After remove : [SystemDetailData [index=2, Name=asd, host=192.168.1.45]]

2 个答案:

答案 0 :(得分:3)

如果SystemDetailData类必须实现hashcode和equals方法。要扩展我的答案,在java中要从集合中删除Object。 Java检查您要删除的对象是否在此集合中(如果Collection包含一个等于我们要删除的Object的对象)。它使用等于的方法。所以我们必须告诉(解释)Java对我们来说是什么相同的对象:它可以具有相同的名称或相同的id或另一个属性(属性)。这就是我们必须实现equals(和hashcode)

的原因

答案 1 :(得分:0)

您的SystemDetailData类需要实现equals方法。当您在ArrayList上调用remove时,代码执行如下操作:

ArrayList<SystemDetailData> items;
void remove(SystemDetailData itemToRemove) {
    for ( int i = 0; i < items.size() ++i ) {
        if ( items.get(i).equals(itemToRemove) ) {
            items.remove(i);
            break;
        }
    }
}

他 因此,除非equals方法为您传递给remove方法的项目和集合中的项目返回true,否则不会删除任何内容。

你需要确切地确定equals方法应该是什么样子,但是如果例如,如果id是相同的两个项是相同的那么你可以只添加一个方法到SystemDetailData:

public boolean equals(Object other) {
    SystemDetailData otherData = (SystemDetailData)other;
    return otherData.getId() == this.getId();
}

显然,您需要添加null的检查,其他类型的等等,但这应该让您了解该方法需要的样子。