如何比较java中的两个Arraylist值? [英] How to compare two Arraylist values in java?
问题描述
我有两个 Arraylist RunningProcessList
AllProcessList
它包含以下值
I have Two Arraylist RunningProcessList
AllProcessList
its contains following values are
RunningProcessList:
Receiver.jar
AllProcessList:
Receiver.jar
Sender.jar
Timeout.jar
TimeourServer.jar
AllProcessList arraylist 包含所有 java 进程,RunningProcessList arraylist 包含当前正在运行的进程.我想比较这两个数组列表,如果进程没有运行,我想显示.例如比较两个列表并希望显示以下进程未运行.
AllProcessList arraylist contains the all java processes , RunningProcessList arraylist contains currently running process. I want to compare these two arraylist and I want to display If the process is not running. For Example compare two list and want to display following process is not running.
Result:
Sender.jar
Timeout.jar
TimeourServer.jar
我使用了以下代码,但它不起作用.
I used the following code but its not working.
Object Result = null;
for (int i = 0; i <AllProcessList.size(); i++) {
for (int j = 0; j < RunningProcessList.size(); j++) {
if( AllProcessList.get(i) != ( RunningProcessList.get(j))) {
System.out.println( RunningProcessList.get(j)));
Result =RunningProcessList.get(j);
}
if(AllProcessList.get(i) != ( RunningProcessList.get(j))) {
list3.add(Result);
}
}
}
推荐答案
查看文档 List,特别是 removeAll() 方法.
Take a look at the documentation for List, ecpecially the removeAll() method.
List result = new ArrayList(AllProcessList);
result.removeAll(RunningProcessList);
然后您可以遍历该列表并根据需要调用 System.out.println
,就像您在上面所做的那样......但这就是您想要做的吗?
You could then iterate over that list and call System.out.println
if you wanted, as you've done above... but is that what you want to do?
这篇关于如何比较java中的两个Arraylist值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!