开发者

How to compare two Arraylist values in java?

开发者 https://www.devze.com 2023-01-05 16:47 出处:网络
I have Two Arraylist RunningProcessListAllProcessList its contains following values are RunningProcessList:

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 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);
       }
    }
}


Take a look at the documentation for List, ecpecially the removeAll() method.

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?


Assuming your lists are not too long, you can just collect all elements of AllProcessList that are not in the RunningProceesList

    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).


it would be better if your list contains instances of Process (or some other dedicated class).

    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. 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. This should only be a concern when Your lists are large.
  2. What about using ActiveProcesses and InactiveProccesses 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.

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:

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"?

To do that you will need to override the equals and hashCode methods. Here is an article on that.

If the class is a built in Java library one then odds are equals and hashCode are done.


For sorted lists, the following is O(n). If a sort is needed, this method becomes 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());
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消