更新:我猜HashSet.add(Object obj)
不调用contains
. 有没有办法实现我想要的(删除 dup 字符串忽略大小写使用Set
)?
原始问题:试图从java中的String列表中删除dups,但是在下面的代码CaseInsensitiveSet.contains(Object ob)
中没有被调用,为什么?
public static List<String> removeDupList(List<String>list, boolean ignoreCase){
Set<String> set = (ignoreCase?new CaseInsensitiveSet():new LinkedHashSet<String>());
set.addAll(list);
List<String> res = new Vector<String>(set);
return res;
}
public class CaseInsensitiveSet extends LinkedHashSet<String>{
@Override
public boolean contains(Object obj){
//this not getting called.
if(obj instanceof String){
return super.contains(((String)obj).toLowerCase());
}
return super.contains(obj);
}
}