Q:

Comparing two ArrayLists

I have two arrayList of the same Type and I want to compare the two based on a particular attribute in the ValueList.
ValueList
ValueList A contains
1,10,5,
2,20,3
3,40,5,
4,60,8

ValueList B contains
2,20,3
3,40,5

I want to compare both the list based on line_num and create another arraylist Result and if the line_num is present in ValueList A but not in ValueList B, then the value field in the Result  has to have a -1. Result should be like;
Result
10,-1
20,3
40,5,
60,-1

I am not able to write the 'Not Found' Condition. Could someone please help me?
My code 
List<Result> result= new ArrayList<Result>();
for(ValueList data1: valueListA) {
    for (ValueList data2: valueListB) {
        Result inter = new Result();
        if(data1.getLine_num==data2.getLine_num) {
            inter.setKey(data1.getKey());
            inter.setValue(data1.getValue());
            result.add(inter);
        }
    }
}

Updated Code which works:
public static  List<Result> result;= new ArrayList<Result>();
    ....

  int i1 = 0,int i2 = 0;
  Result inter = new Result();
  while (i1 < valueListA.size() && i2 < valueListB.size()) {
  ValueList data1 = valueListA.get(i1);
  ValueList data2 = valueListB.get(i2);
  if (data1.getLine_num == data2.getLine_num) {
  // Add the result.      
  result= new ArrayList<Result>();

  inter.setValue(data1.getValue());
  inter.setKey(data1-getKey())
  result.add(inter);
  i1++;
  i2++;
 } else if (data1.getLine_num < data2.getLine_num) {
 result= new ArrayList<Result>();
  // Add -1 because the data was not in valueListB.
   inter.setValue(data1.getValue());
  inter.setKey(-1);
  result.add(inter);
  i1++;
  } else {
   i2++;
 }      

}

A:

From an algorithm point of view:
Add a boolean variable found that equals to false before the start of the inner loop. Then when you found one, you set it to true.
After the loop, you test the variable found and if it's false, you add -1.
List<Result> result= new ArrayList<Result>();
for(ValueList data1: valueListA){
    boolean found = false;
    for (ValueList data2: valueListB){
        Result inter= new Result();
        if(data1.getLine_num==data2.getLine_num){
           inter.setKey(data1.getKey());
           inter.setValue(data1.getValue());
           result.add(inter);
           found = true;
           break;
        }
    }
    if (!found) {
       result.add(...)
    }
}

However, Java allows better solutions, see the other answers for that.
But, if the lists are sorted like in your example, you have better algorithms. You can use a single while loop and 2 indexes (one per list). The complexity will drop from O(N*M) to O(N+M).
int i1 = 0;
int i2 = 0;
while (i1 < valueListA.size() && i2 < valueListB.size()) {
   ValueList data1 = valueListA[i1];
   ValueList data2 = valueListB[i2];
   if (data1.getLine_num == data2.getLine_num) {
      // Add the result.
      i1++;
      i2++;
   } else if (data1.getLine_num < data2.getLine_num) {
      // Add -1 because the data was not in valueListB.
      i1++;
   } else {
      i2++;
   }      
}

