4

This is a pice of my code :

  ArrayList<String> Alist= new ArrayList<String>();
  ArrayList<String> Blist= new ArrayList<String>(); 

  Alist.add("gsm");
  Alist.add("tablet");
  Alist.add("pc");
  Alist.add("mouse");

  Blist.add("gsm");
  Blist.add("something");
  Blist.add("pc");
  Blist.add("something");

so i have two array list i want to compare all items and check if they are not equal and if they are to print out only the items that are not equal.

so i make something like this:

http://postimage.org/image/adxix2i13/ sorry for the image but i have somekind of bug when i post here a for looop.

and the result is :

not equals..:tablet
not equals..:pc
not equals..:mouse
not equals..:gsm
not equals..:tablet
not equals..:pc
not equals..:mouse
not equals..:gsm
not equals..:tablet
not equals..:pc
not equals..:mouse
not equals..:gsm
not equals..:tablet

i want to print only the 2 that are not equal in the example they are gsm and pc

not equals..:gsm
not equals..:pc
2
  • 1
    Well I can't see the image. And what bug can you get while posting a code? I think you should try posting it again. Commented Nov 3, 2012 at 8:58
  • 1
    I don't get why "gsm" and "pc" are the items "that are not equal". Commented Nov 3, 2012 at 9:08

10 Answers 10

11

Don't use != to compare strings. Use the equals method :

if (! Blist.get(i).equals(Alist.get(j))

But this wouldn't probably fix your algorithmic problem (which isn't clear at all).

If what you want is know what items are the same at the same position, you could use a simple loop :

int sizeOfTheShortestList = Math.min(Alist.size(), Blist.size());
for (int i=0; i<sizeOfTheShortestList; i++) {
    if (Blist.get(i).equals(Alist.get(i))) {
        System.out.println("Equals..: " + Blist.get(i));
    }
}

If you want to get items that are in both lists, use

for (int i = 0; i < Alist.size(); i++) {
    if (Blist.contains(Alist.get(i))) {
        System.out.println("Equals..: " + Alist.get(i));
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks :) but this check only the one by one how can i make it many to many check
1

You can use the RemoveAll(Collection c) on one of the lists, if you happen to know if one list always contains them all.

Comments

0

You could use the following code:

    ArrayList<String> Alist = new ArrayList<String>();
    ArrayList<String> Blist = new ArrayList<String>();

    Alist.add("gsm");
    Alist.add("tablet");
    Alist.add("pc");
    Alist.add("mouse");

    Blist.add("gsm");
    Blist.add("something");
    Blist.add("pc");
    Blist.add("something");
    for (String a : Alist)
    {
        for (String b : Blist)
        {
            if (a.equals(b))
            {
                System.out.println("Equals " + a);
                break;
            }
        }
    }

Output is:
Equals gsm
Equals pc

2 Comments

Ok but if i want ot check the first item of Alist with the last item or Blist
@user1792440 I don't seem to understand your query above. Can you explain by modifying your example?
0

right now your comparing each element to all of the other ones. Do something like

for (int i = 0; i < Alist.size(); i++) {
    if (!Alist.get(i).equals(Blist.get(i)) {
        // print what you want
    }
}

Thats of course assuming both lists have the same length.

3 Comments

Ok but we dont know that every time the item will be same on the same line how can i check many to many
Sort AList and BList before checking? But then you have a problem if the corresponding items are not aligned.
give me a minute, i'll come up with a clever new answer
0

Rather than writing code to manually compare list elements you might consider using Apache Commons Collections.

import org.apache.commons.collections.CollectionUtils;

List listA = ...;
List listB = ...;

Collection intersection = CollectionUtils.intersection(listA, listB);

1 Comment

Check the hashCode and equals methods of the objects in the lists.
0
import java.util.HashSet;

public class CheckSet<T> extends HashSet<T>{

    @Override
    public boolean add(T e) {
        if (contains(e)) {
            remove(e);
            return true;
        } else {
            return super.add(e);
        }
    }   
}

Add all elements of both of your lists to a CheckSet intance, and at the end it will only contain the ones not equal.

Comments

0

Here is one way:

    public static boolean compare(List<String> first, List<String> second) {
    if (first==null && second==null) return true;
    if (first!=null && second==null) return false;
    if (first==null && second!=null) return false;

    if ( first.size()!=second.size() ) return false;

    HashMap<String, String> map = new HashMap<String, String>();
    for (String str : first) {
        map.put(str, str);
    }
    for (String str : second) {
        if ( ! map.containsKey(str) ) {
            return false;
        }
    }
    return true;
}

Comments

0

public static void main(String args[] ) throws Exception {

     List<String> arrayList1 = new ArrayList<String>();
     arrayList1.add("a");
     arrayList1.add("b");
     arrayList1.add("c");
     arrayList1.add("d");

     List<String> arrayList2 = new ArrayList<String>();
     arrayList2.add("a");
     arrayList2.add("b");
     arrayList2.add("c");
     arrayList2.add("d");

     boolean isEqual = false;
     if(arrayList1.size() == arrayList2.size()){
        List<String> arrayListTemp = new ArrayList<String>();
        arrayListTemp.addAll(arrayList1);
        arrayListTemp.addAll(arrayList2);
        HashSet<Object> hashSet = new HashSet<Object>();
        hashSet.addAll(arrayListTemp);
        if(hashSet.size() == arrayList1.size() &&
            hashSet.size() == arrayList2.size()){
            isEqual = true;
        }
     }
     System.out.println(isEqual);
 }

1 Comment

PLease provide some context for your answer. See here: stackoverflow.com/help/how-to-answer
0

we can compare two different size arrayList in java or Android as follow.

ArrayList<String> array1 = new ArrayList<String>();
ArrayList<String> array2 = new ArrayList<String>();

array1.add("1");
array1.add("2");
array1.add("3");
array1.add("4");
array1.add("5");
array1.add("6");
array1.add("7");
array1.add("8");

array2.add("1");
array2.add("2");
array2.add("3");
array2.add("4");

 for (int i = 0; i < array1.size(); i++) {
            for (int j=0;j<array2.size();j++) {
                if (array1.get(i) == array2.get(j)) {
                    //if match do the needful
                } else {
                    // if not match
                }
            }
        }

Comments

-2
import java.util.Arrays;
public class ExampleContains {
    public static boolean EligibleState(String state){
        String[] cities = new String[]{"Washington", "London", "Paris", "NewYork"};
        boolean test = Arrays.asList(cities).contains(state)?true:false;
        return test;
     }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println(EligibleState("London"));
    }

}

1 Comment

It doesn't compare 2 Lists.

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.