【问题标题】:Compare two List<String> values in android比较 android 中的两个 List<String> 值
【发布时间】:2013-06-08 06:20:48
【问题描述】:

亲爱的,如果两个值相同,我正在比较 android 中的两个 List 并且返回 true 很好,但我想知道列表字符串之间有多少值是正确的,如果它不匹配如何实现此帮助赞赏。下面是我的代码。

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

【问题讨论】:

  • 如果它们不相等,则循环遍历它们自己比较项目,并保留一个简单的整数作为计数,每次项目匹配时递增。
  • 在同一个问题上遇到了困难,你得到答案了吗?如果是这样,请发布正确的答案。我需要从两个列表中获取共同值。我关注了一些帖子,但那不起作用。

标签: android string list equals


【解决方案1】:

使用Collection#retainAll()。喜欢

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

因此您可以检查大小是否大于 0,这意味着某些元素是常见的。哪些是常见的元素common 会告诉你。

【讨论】:

    【解决方案2】:

    您可以在 Java 集合上使用 containsAll 方法。这是一个例子。

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

    否则,您可以使用 apache CollectionUtils 库来提高性能。根据提供的 Collection 类型,此方法将比调用 Collection.containsAll(Collection) 快得多。 请参阅 apache 的文档 containsAll here

    【讨论】:

      【解决方案3】:
      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++;
              }
          }
      }
      

      它的时间复杂度很高,但可以解决问题。

      【讨论】:

      • 这是检查我列表中的所有字符串并返回我想要多少是正确的,我的列表中有多少是错误的,而不是作为一个整体
      【解决方案4】:

      您可以自己遍历列表检查是否相等,同时保留一个计数器来指示它们在哪个位置上不同。

      示例(伪)代码:

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

      另外请记住,您必须检查列表的大小是否相同,并决定如果它们不一样该怎么办(例如,返回 -1 表示这一点)。

      【讨论】:

        【解决方案5】:
        list1.containsAll(acct_Rte_Cdes_A)
        
        boolean containsAll(Collection<?> c)
        

        如果此列表包含所有元素,则返回 true 指定的集合。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-12-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-12-05
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多