Compare two List<String> values in android

15,577

Solution 1

int correctCount=0, incorrectCount = 0;
List<String> list1 = new ArrayList<String>(i1.values());

List<String> acct_Rte_Cdes_A = Arrays.asList(result)    

for(String tmp1: list1) {
    for(String tmp2: list2) {
        if(tmp1.compareTo(tmp2) == 0) {
            correctCount++;
        } else {
            incorrectCount++;
        }
    }
}

it's time complexity is high but it will do the trick.

Solution 2

Use Collection#retainAll(). like

List<Integer> common = new ArrayList<Integer>(listA);
common.retainAll(listB);
// common now contains only the elements which are contained in listA and listB.

So you can check for size if it is greater than 0 it meanse some elements are common. And which are common elements common will tell.

Solution 3

You can use containsAll method on Java collection. Here is an example.

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

public class MainClass {

  public static void main(String[] a) {
    String elements[] = { "A", "B", "C", "D", "E" };
    List<String> list = new ArrayList<String>(Arrays.asList(elements));

    elements = new String[] { "A", "B", "C" };
    List<String> list2 = new ArrayList<String>(Arrays.asList(elements));

    System.out.println(list.containsAll(list2)); // true
    System.out.println(list2.containsAll(list)); // false
  }
}

Otherwise, you can use apache CollectionUtils library to improve performances. Depending on the type of Collection provided, this method will be much faster than calling Collection.containsAll(Collection) instead. See the doc of apache containsAll here.

Solution 4

You can iterate through the lists yourself checking for equality and at the same time keeping a counter to indicate on which position they differ.

Sample (pseudo) code:

//if equal return 0, else position of difference
//keep in mind that item at index [0] is first, so return would be 1 if lists differ there
public int checkEquality(List list1, List list2) {
    for (int i = 0; i < list1.size(); ++i) {
        if (list1[i] != list2[i])
             return i + 1;
    }
    return 0;
}

Also keep in mind, that you'd have to check if the lists are of the same size, and decide what to do if they are not (for example, return -1 to indicated this).

Share:
15,577
user1048958
Author by

user1048958

Updated on June 25, 2022

Comments

  • user1048958
    user1048958 almost 2 years

    Dear all i am comparing two List in android if both values are same and it is returning true that is fine but i want to know how many values have been correct between the list string if it is not matching how to achieve this Help is appreciated. Below is my code.

                List<String> list1 = new ArrayList<String>(i1.values());
                ///list 1 = [-ful; to be full of; play; playful; full of play]
        List<String> acct_Rte_Cdes_A = Arrays.asList(result) ;
                ///acct_Rte_Cdes_A  = [-ful; to be full of; play; playful; full of play]
                 if (list1.equals(acct_Rte_Cdes_A)) { 
                    // do what you want to do if the string is there
                    //System.out.println("True");
         }  else {
            System.out.println("False");
             }