如何比较java中的两个Arraylist值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3163293/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to compare two Arraylist values in java?
提问by lakshmi
I have Two Arraylist RunningProcessList
AllProcessList
its contains following values are
我有两个 ArraylistRunningProcessList
AllProcessList
它包含以下值
RunningProcessList:
Receiver.jar
AllProcessList:
Receiver.jar
Sender.jar
Timeout.jar
TimeourServer.jar
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.
AllProcessList arraylist 包含所有 java 进程, RunningProcessList arraylist 包含当前正在运行的进程。我想比较这两个数组列表,如果进程没有运行,我想显示。例如比较两个列表并希望显示以下进程未运行。
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);
}
}
}
采纳答案by Noel M
Take a look at the documentation for List, ecpecially the removeAll()method.
查看List的文档,尤其是removeAll()方法。
List result = new ArrayList(AllProcessList);
result.removeAll(RunningProcessList);
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?
然后,您可以遍历该列表并System.out.println
根据需要调用,就像您在上面所做的那样……但这就是您想要做的吗?
回答by TofuBeer
Depending on the type on AllProcessList and RunningProcessList (whocu should be allProcessList and runningProcessList to follow the Java naming conventions) the following will not work:
根据 AllProcessList 和 RunningProcessList 上的类型(whocu 应该是 allProcessList 和 runningProcessList 以遵循 Java 命名约定)以下将不起作用:
if ( AllProcessList.get(i) != ( RunningProcessList.get(j))) {
you should replace it with
你应该用
if (!(AllProcessList.get(i).equals(RunningProcessList.get(j)))) {
!= compares physical equality, are the two things the exact same "new"ed object? .equals(Object) compared locaical equality, ate the two things the "same"?
!= 比较物理相等,这两个东西是完全相同的“新”对象吗?.equals(Object) 比较了localequality,吃的两个东西“一样”?
To do that you will need to override the equals and hashCode methods. Here is an articleon that.
为此,您需要覆盖 equals 和 hashCode 方法。这是一篇关于此的文章。
If the class is a built in Java library one then odds are equals and hashCode are done.
如果该类是一个内置的 Java 库,那么几率是相等的并且 hashCode 已经完成。
回答by Tim Bender
For sorted lists, the following is O(n). If a sort is needed, this method becomes O(nlogn).
对于排序列表,以下是 O(n)。如果需要排序,则此方法变为 O(nlogn)。
public void compareLists(final List<T> allProcesses, final List<T> runningProcesses) {
// Assume lists are sorted, if not call Collection.sort() on each list (making this O(nlogn))
final Iterator<T> allIter = allProcesses.iterator();
final Iterator<T> runningIter = runningProcesses.iterator();
T allEntry;
T runningEntry;
while (allIter.hasNext() && runningIter.hasNext()) {
allEntry = allIter.next();
runningEntry = runningIter.next();
while (!allEntry.equals(runningEntry) && allIter.hasNext()) {
System.out.println(allEntry);
allEntry = allIter.next();
}
// Now we know allEntry == runningEntry, so we can go through to the next iteration
}
// No more running processes, so just print the remaining entries in the all processes list
while (allIter.hasNext()) {
System.out.println(allIter.next());
}
}
回答by user85421
Assuming your lists are not too long, you can just collect all elements of AllProcessList that are not in the RunningProceesList
假设您的列表不太长,您可以收集所有不在 RunningProceesList 中的 AllProcessList 元素
for (Object process : AllProcessList) {
if (!RunningProcessList.contains(process)) {
list3.add(process);
}
}
it's important that the RunningProcessList contains the same instances as the AllProcessList (or the objects must implement a functional equals
method).
重要的是 RunningProcessList 包含与 AllProcessList 相同的实例(或者对象必须实现功能equals
方法)。
it would be better if your list contains instances of Process
(or some other dedicated class).
如果您的列表包含Process
(或其他一些专用类)的实例会更好。
List<Process> AllProcessList = new ArrayList<Process>();
List<Process> RunningProcessList = new ArrayList<Process>();
List<Process> list3 = new ArrayList<Process>();
...
for (Process process : AllProcessList) {
if (!RunningProcessList.contains(process)) {
list3.add(process);
}
}
English is not my first (neither second) language, any correction is welcome
英语不是我的第一(也不是第二)语言,欢迎任何更正
回答by Dave O.
Hi lakshmi,
嗨拉克希米,
I upvoted noelmarkham's answeras I think it's the best code wise and suits Your needs. So I'm not going to add another code snippet to this already long list, I just wanted to point You towards two things:
我赞成noelmarkham 的回答,因为我认为它是最好的代码,适合您的需求。所以我不打算在这个已经很长的列表中添加另一个代码片段,我只是想向您指出两件事:
- If Your processes are unique (their name/id whatever), You might consider to use (Hash)Sets in order to store them for better performance of Your desired operations. This should only be a concern when Your lists are large.
- What about using
ActiveProcesses
andInactiveProccesses
instead of Your current two lists? If a process changes its state You just have to remove it from one list and insert it into the other. This would lead to an overall cleaner design and You could access the not-running processes immediately.
- 如果您的进程是唯一的(无论它们的名称/id 是什么),您可能会考虑使用 (Hash)Sets 来存储它们,以便更好地执行所需的操作。只有当您的列表很大时,这才应该是一个问题。
- 使用
ActiveProcesses
andInactiveProccesses
而不是您当前的两个列表怎么样?如果进程更改其状态,您只需将其从一个列表中删除并将其插入另一个列表中。这将导致整体设计更简洁,您可以立即访问未运行的进程。
Greetings
你好