我有两个字符串数组 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/