Search code examples
javalistcollectionscomparison

Java compare two lists of String of same size where order is important


I am currently stuck while comparing two list of strings. Here are the inputs:

First list : three, two, ten, five.
Second list: three, ten, two, five.

The order is important in both list in such a way that: if one element index is not same in other list, then it should put an empty line.

I have attached a screen shot for better clarity.

enter image description here

Here is my code

public static void main(String[] args) {

 List<String> list1 = new ArrayList<String>();
 List<String> list2 = new ArrayList<String>();


    list1.add("three");
    list1.add("two");
    list1.add("ten");
    list1.add("five");

    list2.add("three");
    list2.add("ten");
    list2.add("two");
    list2.add("five");

    for(int iIndex = 0, jIndex = 0; iIndex < list1.size() && jIndex < list2.size(); iIndex ++, jIndex++) {
        if(!list1.get(iIndex).contentEquals(list2.get(jIndex))) {
            list1.add(jIndex, "");
        }
    }

Note: I have searched and checked each listed topics before posting this question. Thank you for your help


Solution

  • If I understood your question correctly you need something like this

    int size = list2.size();
    for(int i = 0; i < size ; i++) {
        if(!list1.get(i).contentEquals(list2.get(i))) {
          if(list2.size() <= list1.size()) {
            list2.add(i, "XXX");
            size +=1;
          } else {
            list1.add(i, "XXX");
          } 
        }
    }
    

    And the output will be:

    three   three
    two     XXX
    ten     ten
    XXX     two
    five    five
    

    I've added XXX to view them easier. You should also check the case when the lists are not equal in size since it might change the expected output of your program.

    UPDATE:

    You can try to do something like this.

    int size = list1.size();
    for(int i = 0; i < size; i++) {
      if(list1.size() == i){
        list1.add(i, "XXX");
        continue;
      }
      if(list2.size() == i){
        list2.add(i, "XXX");
        continue;
      }
      if(!list1.get(i).contentEquals(list2.get(i))) {
        int next_index1 = list2.subList(i, list2.size()).indexOf(list1.get(i));
        int next_index2 = list1.subList(i, list1.size()).indexOf(list2.get(i));
    
        if (next_index1 == -1){
          list2.add(i, "XXX");
        }
        else if(next_index2 == -1){
          list1.add(i, "XXX");
        } 
        else if(next_index1 < next_index2) {
          list1.add(i, "XXX");
        } else {
          list2.add(i, "XXX");
        } 
      }
      size = list1.size() < list2.size() ? list2.size() : list1.size();
    }
    for(int i = 0; i < size ; i++) {
      System.out.println(list1.get(i) + "   " + list2.get(i));
    }
    

    In brief, it will check when is the closest occurrence of a string in the other list. For example if your lists are:

    three   three
    two     ten
    ten     two
    five    five
    

    After the first elements in each list, because they are the same, it will find the distance between the current position in the first list and the index of the first occurrence of that element in the second list and the other way around. If the distances are equal then it will add space in the second list. So the result will be

    three   three
    two     XXX
    ten     ten
    XXX     two
    five    five
    

    But if your list are

    three   three
    two     ten
    five     two
    

    the output will be

    three   three
    XXX     ten
    two     two
    five    XXX