开发者

How to check equalignorecase with startsWith in hashmap in java [duplicate]

This question already has answers here: Closed 12 years ago.

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 );
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜