2011-12-09 10 views
1

私はHashMapで検索し、文字列を表すノードに特定の文字列を一致させようとしています。HashMap検索と一致する正規表現

 for (Entry<String, Component> entry : hashMap.entrySet()) { 
      if(entry.getValue().toString().matches("test1")){ 
       System.out.println(entry.getValue().toString()); 
      } 
      else 
      { 
       System.out.println("Nothing found"); 
      } 
     } 

しかし、私は異なる場合があります。それはこのように行われていたであろう正確に特定の値をマッチングするための

。ノードに長い文字列が含まれています"xxx .. test 1 .."

したがって、どのようにこれらのノード文字列に "test 1"を対応させることができますか?

答えて

2

あなただけのindexOfを使用することができます。

if (entry.getValue().toString().indexOf("test 1") != -1) { 
0

すべてを行う必要がtest 1が文字列である場合、あなたはString.contains()メソッドを使用することができます見ている場合。

if(entry.getValue().toString().contains("test1")){ 
    System.out.println(entry.getValue().toString()); 
} 
else 
{ 
    System.out.println("Nothing found"); 
}