Possible Duplicate:
how to search like LIKe operator in sql in hash map in java
Hi
How to check equalignorecase with startsWith in hashmap in j开发者_开发技巧ava give example
Thanks
I'm having trouble understanding your question. However, from what I can deduce:
Keys in a HashMap
are compared for equality using the equals(Object o) operation. You cannot change this. However, you can implement your own key, for example to perform a case-insensitive comparison:
public class Key {
private final String s;
private final int hashCode;
public Key(String s) {
this.s = s;
this.hashCode = s.toUpperCase().hashCode();
}
public int hashCode() { return hashCode; }
public boolean equals(Object o) {
if (this == o) {
return true;
} else if (o instanceof Key) {
return this.s.equalsIgnoreCase(((Key)o).s);
} else {
return false;
}
}
}
Sure:
Given:
Map<String,String> map = new HashMap<String,String>();
You may call:
map.get( someKey ).equalsIgnoreCase( someValue )
and
map.get( someKey ).startsWith( someOtherValue );
精彩评论