Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare two Arraylist values in java?

Tags:

java

arraylist

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);
       }
    }
}
like image 679
lakshmi Avatar asked Feb 18 '26 06:02

lakshmi


1 Answers

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?

like image 146
Noel M Avatar answered Feb 19 '26 19:02

Noel M



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!