javacodex.com
Java Examples
Java Examples
memu home questions

Cosequential Processing

Cosequential Processing is the procesing of two or more data sources sequentially and in parallel.

The algorithm in this example:

1) Get item1 from list1 and item2 from list2.
2) If the items are the same, put item in results list and get the next items from both lists.
3) If item1 is less than item2, then get the next item from list1
4) If item1 is greater than item2, then get the next item from list2. Go to step 2.

Source: (CosequentialProcessing.java)

import java.util.*;
 
public class CosequentialProcessing {
   public static void main(String[] args) {
 
      // list must be sorted
      List<String> list1 = Arrays.asList("AA","BB","CC","DD","EE","FF","GG");
      List<String> list2 = Arrays.asList("BB","DD","EE","HH","II");
      Collections.sort(list1);
      Collections.sort(list2);
 
      // results go in here
      List<String> results = new ArrayList<String>();
 
      int count1=0;
      int count2=0;
      String str1 = list1.get(count1);
      String str2 = list2.get(count2);
 
      while(count1 <= (list1.size()-1) && count2 <= (list2.size()-1)) {
 
         int x = str1.compareTo(str2);
 
         if (x==0) {
            results.add(str1);
            count1=count1+1;
            count2=count2+1;
            if(count1<=list1.size()-1){
               str1 = list1.get(count1);
            }
            if(count2<=list2.size()-1){
               str2 = list2.get(count2);
            }
         } else if (x<0){
            count1=count1+1;
            if(count1<=list1.size()-1){
               str1 = list1.get(count1);
            }
         } else {
            count2=count2+1;
            if(count2<=list2.size()-1){
               str2 = list2.get(count2);
            }
         }
      }
 
      System.out.println("list1: " + list1);
      System.out.println("list2: " + list2);
      System.out.println("results:" + results);
   }
}
 

Output:

$ java CosequentialProcessing
list1: [AA, BB, CC, DD, EE, FF, GG]
list2: [BB, DD, EE, HH, II]
results:[BB, DD, EE]

Contact: javacodex@yahoo.com