user1792440
user1792440

Reputation: 91

Compare strings in two different arraylist (JAVA)

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

Upvotes: 4

Views: 43060

Answers (10)

Deepak gupta
Deepak gupta

Reputation: 947

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
                }
            }
        }

Upvotes: 0

Raj
Raj

Reputation: 65

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);
 }

Upvotes: 0

Denys S&#233;guret
Denys S&#233;guret

Reputation: 382092

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));
    }
}

Upvotes: 11

user3415060
user3415060

Reputation: 1

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"));
    }

}

Upvotes: -2

stones333
stones333

Reputation: 8958

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;
}

Upvotes: 0

jlordo
jlordo

Reputation: 37813

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.

Upvotes: 0

ct_
ct_

Reputation: 2347

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);

Upvotes: 0

jlordo
jlordo

Reputation: 37813

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.

Upvotes: 0

Srinivas
Srinivas

Reputation: 1790

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

Upvotes: 0

Frank
Frank

Reputation: 15631

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

Upvotes: 1

Related Questions