无法从Java中的列表中移除null。

huangapple go评论58阅读模式
英文:

Not able to remove null from a list in Java

问题

我有一个名为newSRMAPIResponseBeanList的豆类列表,在这个列表中,我总是得到一个空对象,我试图删除它,但如果我处理异常,它会导致空指针异常,它不会删除那个空值。以下是我的代码。

for (int i = 0; i < newSRMAPIResponseBeanList.size(); i++) {
    try {
        if (newSRMAPIResponseBeanList.get(i).getCompanyId().equals(null)) {
            newSRMAPIResponseBeanList.remove(i);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

在if条件中它就失败了。我想要删除company ID 的空值。实际上,newSRMAPIResponseBeanList是一个List<NewSRMAPIResponseBean> newSRMAPIResponseBeanList = new ArrayList<>();的列表,而bean类如下所示。

public class NewSRMAPIResponseBean {
    private String companyId;
    private String investmentId;
    private String performanceId;
    private List<String> values;
}

有没有办法我可以删除那个空值?我还尝试使用Java流,如下所示。

List<NewSRMAPIResponseBean> finalList = newSRMAPIResponseBeanList.parallelStream()
    .filter(Objects::nonNull)
    .collect(Collectors.toList());

这也没有起作用。

英文:

I have a list of a bean class newSRMAPIResponseBeanList wherein I am always getting a null object at last which I am trying to remove but it is resulting in null pointer exception if I handle the exception, it is not removing that null value. Below is my code.

for (int i = 0; i < newSRMAPIResponseBeanList.size(); i++) {
				try {
					if (newSRMAPIResponseBeanList.get(i).getCompanyId().equals(null)) {
						newSRMAPIResponseBeanList.remove(i);
					}
				}catch(Exception e) {
					e.printStackTrace();
				}
			}

In the if condition itself it is failing. I want to remove the null value of company ID. Actually newSRMAPIResponseBeanList is a list of List<NewSRMAPIResponseBean> newSRMAPIResponseBeanList = new ArrayList<>(); and the bean class is as follows.

public class NewSRMAPIResponseBean {
	private String companyId;
	private String investmentId;
	private String performanceId;
	private List<String> values;
}

Is there any way I can remove that null value? I also tried using Java streams as follows.

List<NewSRMAPIResponseBean> finalList=newSRMAPIResponseBeanList.parallelStream()
			      .filter(Objects::nonNull)
			      .collect(Collectors.toList());

This too did not work.

答案1

得分: 0

I want to remove the null value of company ID.
我想移除公司ID的空值。

I presume you mean remove the list element if the Id is null. So It's not the bean that is null, it is the companyId. Assuming you have getters, try it like this. If the Id is not null, let it pass thru.
我猜你是指如果ID为null,则移除列表元素。所以它不是bean为空,而是companyId为空。假设你有getter方法,可以尝试像这样操作。如果ID不为null,就让它通过。

List<NewSRMAPIResponseBean> finalList=newSRMAPIResponseBeanList.parallelStream()
        .filter(bean->bean.getCompanyId() != null)
        .collect(Collectors.toList());

You can also do it in a simple loop. To avoid a ConurrentModificationException, use an iterator.
你也可以在一个简单的循环中执行。为了避免ConurrentModificationException,使用迭代器。

Iterator<NewSRMAPIResponseBean> iter =  newSRMAPIResponseBeanList.iterator();
while (iter.hasNext()) {
    if (iter.next().getCompanyId() == null) {
        iter.remove();
    }
}
英文:

> I want to remove the null value of company ID.

I presume you mean remove the list element if the Id is null. So It's not the bean that is null, it is the companyId. Assuming you have getters, try it like this. If the Id is not null, let it pass thru.


List&lt;NewSRMAPIResponseBean&gt; finalList=newSRMAPIResponseBeanList.parallelStream()
        .filter(bean-&gt;bean.getCompanyId() != null)
        .collect(Collectors.toList());

You can also do it in a simple loop.
To avoid a ConurrentModificationException, use an iterator.

Iterator&lt;NewSRMAPIResponseBean&gt; iter =  newSRMAPIResponseBeanList.iterator();
while (iter.hasNext()) {
    if (iter.next().getCompanyId() == null) {
        iter.remove();
    }
}

</details>



huangapple
  • 本文由 发表于 2023年2月18日 03:12:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75488400.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定