简体   繁体   English

如何比较java中的两个Arraylist值?

[英]How to compare two Arraylist values in java?

I have Two Arraylist RunningProcessList AllProcessList its contains following values are我有两个 Arraylist RunningProcessList 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. AllProcessList arraylist 包含所有 java 进程, RunningProcessList arraylist 包含当前正在运行的进程。 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);
       }
    }
}

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 ,就像您在上面所做的那样……但这就是您想要做的吗?

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英语不是我的第一(也不是第二)语言,欢迎任何更正

Hi lakshmi,嗨拉克希米,

I upvoted noelmarkham's answer as I think it's the best code wise and suits Your needs.我赞成noelmarkham 的回答,因为我认为它是最好的代码,适合您的需求。 So I'm not going to add another code snippet to this already long list, I just wanted to point You towards two things:所以我不打算在这个已经很长的列表中添加另一个代码片段,我只是想向您指出两件事:

  1. 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.如果您的进程是唯一的(无论它们的名称/id 是什么),您可能会考虑使用 (Hash)Sets 来存储它们,以便更好地执行所需的操作。 This should only be a concern when Your lists are large.只有当您的列表很大时,这才应该是一个问题。
  2. What about using ActiveProcesses and InactiveProccesses instead of Your current two lists?使用ActiveProcessesInactiveProccesses而不是您当前的两个列表怎么样? 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.这将导致整体设计更简洁,您可以立即访问未运行的进程。

Greetings你好

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.为此,您需要覆盖 equals 和 hashCode 方法。 Here is an article on that.这是 一篇关于此的 文章

If the class is a built in Java library one then odds are equals and hashCode are done.如果该类是一个内置的 Java 库,那么几率是相等的并且 hashCode 已经完成。

For sorted lists, the following is O(n).对于排序列表,以下是 O(n)。 If a sort is needed, this method becomes O(nlogn).如果需要排序,则此方法变为 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());
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM