我有一个arrayList
里面有一串字符串。我还有另一个arrayList
用一串字符串。我想看看第一个数组列表是否包含第二个 arrayList 中的任何字符串,如果是,则将其打印出来。应该很简单,但似乎不起作用(并且肯定有共同的字符串)。
这是我的代码:
ArrayList<String> che=new ArrayList<String>();
for (int i = 1; i <= rsmd.getColumnCount(); i++) {
System.out.println("COL"+rsmd.getColumnName(i));
che.add(rsmd.getColumnName(i).trim());
}
String key = "";
String keys = "";
for (Entry<String, String> entry : map.entrySet()) {
key = entry.getKey().replace(":","")
.replace(">","MoreThan").replace("__","")
.replaceAll("\\." ,"").replaceAll("\\(s\\)" ,"")
.replaceAll(",$" ,"").replaceAll("%" ,"")
.replaceAll("\\(" ,"").replaceAll("\\)" ,"")
.replace("-" ,"").replace("-" ,"")
.replace("_" ,"").replace("#" ,"")
.replaceAll("\\/", "_").replaceAll("@", "AT")
.replaceAll("&", "AND").replaceAll("\\?", "AND")
.replace("≤","LessThan").replace("≥","MoreThan")
.replaceAll("\\s" ,"").replaceAll("^,","");
for (String n:che){
System.out.println("Che_String "+n);
System.out.println("KEY_String "+key);
if (n==key){
System.out.println("Detected the same: "+key.trim());
}
}
}
我知道有很多替换,我应该使用StringUtils.repaceEach
我会这样做,但需要先对此进行排序
System.out.println
输出显示有几个相似的字符串,但它们没有被拾取,例如
Che_String Gender
KEY_String Gender
但是我的Detected the same:
输出为空。
我也尝试过che.contains(key)
具有相同的结果。
最佳答案
String 应与 String.equals(String)
方法进行比较。使用 ==
可以比较它是否是相同的对象而不是相同的内容。 List.contains(Object)
使用 equals()
方法。所以这应该有效。也许您的文本末尾有一些空白,这些空白在测试打印中不可见。
关于java - ArrayList 包含未检测到它应该包含的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37135636/