gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 22:52:17 25 4
gpt4 key购买 nike

我想从 ArrayList 中删除特定的 bean 对象。我正在使用 removeremoveAll 方法从 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]]

最佳答案

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

关于java - 如何从Java中的ArrayList中删除特定的bean对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24590098/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com