java - 使用集合比较字符串数组

标签 java collections

我有两个字符串数组 a,b。

String a [] = {"one","two","three"};
String b [] = {"one","Two","Three","four"};

我需要检查两个数组是否相同,不区分大小写。 我知道,以下代码非常适合区分大小写。

List <String> l1 = Arrays.asList(a);
List <String> l2 = Arrays.asList(b);
System.out.println(l2.containsAll(l1));  

有没有其他方法可以使用集合比较两个字符串数组(不区分大小写)?

最佳答案

最后,我使用了 TreeSet 和不区分大小写的比较器。

示例:

 String [] oldVal = {"one","two","three","Four"};
 String [] newVal = {"one","Two","Three","four"};

 Set <String> set1 = new TreeSet <String> (String.CASE_INSENSITIVE_ORDER);
 Set <String> set2 = new TreeSet <String> (String.CASE_INSENSITIVE_ORDER);

 set1.addAll(Arrays.asList(oldVal));
 set2.addAll(Arrays.asList(newVal));

 System.out.println("--Using Tree Set --- "+ set1.containsAll(set2));  // Return True

谢谢大家..

关于java - 使用集合比较字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2419061/

相关文章:

java - 在 JSP 页面中获取 maxInactiveInterval 值

java - 如何在 Android 中渲染 PDF 文件

java - 在 groovy 中将 LinkedHashMap 转换为 HashMap

collections - 在没有 "For Each"枚举的情况下从 VBScript 访问 WMI 集合对象

java - 如何初始化静态 List<T>?

java - 如何使用宏函数计算数组的长度?

java - JKS保护

java - 有没有框架?

java - 没有额外空间的 O(n) 中的反转堆栈不会发生

java - 为什么 iterator.remove 不抛出 ConcurrentModificationException