4

i got two String type arraylist ..one list containing “book1”, “book2”, “book3” and “book4”. And another arrayList contains “book1”, “book2”, “book3”. So, size of first list is 4 and second is 3. And I created another arrayList equal to the size of first list

List<Integer> comparingList = new ArrayList<Integer>();
                    //adding default values as one
                    for(int a=0;a<firstList.size();a++){
                        comparingList.add(0);

                    }

And if any content is equal between two lists, I’m setting 1 instead of 0.

so the new arrayList(comparingList) should have 1,1,1,0 elements

for(int counter = 0;counter < firstList.size();counter++){
for(int counter1 = 0;counter1 < secondList.size();counter1++){
if(firstList.get(counter).equals(secondList.get(counter1))){
    comparingList.set(counter,1);
    break;
}
}

}

but when i do this, i’m not being able to set 1 as I can’t get into if condition, can anyone help me please

6
  • What exactly is happening that you think is wrong? Commented Nov 24, 2013 at 15:21
  • 1
    Try printing values inside to loop to see what values are you comparing. Commented Nov 24, 2013 at 15:24
  • See: stackoverflow.com/questions/15183982/… Commented Nov 24, 2013 at 15:38
  • @Alan Stokes I think I'm not able to get inside that if condition..seems like .equals is not working Commented Nov 24, 2013 at 15:58
  • @sunjcarkey It seems unlikely that Integer equals() is broken. Try debugging. Commented Nov 24, 2013 at 16:09

2 Answers 2

6

Only iterate on first arraylist with larger length and check for contains in second arraylist , if found set one else do nothing

for(int counter = 0; counter < firstList.size(); counter++) {
    if(secondList.contains(firstList.get(counter))) {
          comparingList.set(counter,1);
      }
  }

Whole java program

Just try to run the below program in http://www.compileonline.com/compile_java_online.php

import java.util.ArrayList;
import java.util.List;

public class CompareArrayListTest{

 public static void main(String[] args) {

    ArrayList<String> firstList = new ArrayList<String>();

    firstList.add("book1");
    firstList.add("book2");
    firstList.add("book3");
    firstList.add("book4");

    ArrayList<String> secondList = new ArrayList<String>();

    secondList.add("book1");
    secondList.add("book2");
    secondList.add("book3");

    List<Integer> comparingList = new ArrayList<Integer>();
    // adding default values as one
    for (int a = 0; a < firstList.size(); a++) {
        comparingList.add(0);

    }

    for (int counter = 0; counter < firstList.size(); counter++) {
        if (secondList.contains(firstList.get(counter))) {
            comparingList.set(counter, 1);
        }
    }

    System.out.println(comparingList);

}

BitSet bitset = new BitSet();
// adding default values as one
for (int a = 0; a < firstList.size(); a++) {
    comparingList.add(0);

}

for (int counter = 0; counter < firstList.size(); counter++) {
    for (int counter2 = 0; counter < secondList.size(); counter++) {
        if (secondList.get(counter2).equals(firstList.get(counter))) {
            bitset.set(counter, 1);
        }
    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

what's the problem with the answer ?
@sunjcarkey the condition is Ok, i have pasted the whole program with the same condition, just check the same in your favourite java editor or online at compileonline.com/compile_java_online.php
its bit different than you think..coz..if book2 in firstList and books2 in secondList are equal, 1 will be set in third list in 2nd index. So, that means I need to know the index of book2 in firstList.
@sunjcarkey then probably your question is not clear, you never mentioned that you want to preserve the order.
so, now instead of using a new Arraylist to store 0 and 1 for available not available elements. Please try for BitSet to set the corrosponting bit. Added code above in post
|
2

You can transform the lists to sets, and then use Set.retainAll method for intersection between the different sets. Once you intersect all sets, you are left with the common elements, and you can transform the resulting set back to a list.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.