0

文字列パターンが存在しない場合にのみコレクションに追加する必要があり、正確なパターンを探す必要があります。

たとえば、特定のキーに対して、値が abc,bcd,aefg で、追加する新しいパターンが efg であると仮定すると、追加する必要があります (値が区切り文字としてカンマで区切られていると仮定します)

メソッドを使用する予定StringBuffer.indexOf(String Str)でしたが、検索するパターンが値の部分文字列として発生した場合にインデックスを返すため、使用していません (上記の場合、efg は既に部分文字列として発生しています)。Java 5 の Regex 機能を使用した解決策があることは知っていますが、そのトピックについて少しブラッシュアップする必要があります。今のところ、私は次のことをしています

  1. キーに対して取得した値を反復処理します (区切り記号で区切られた値リスト)
  2. 新しいパターン (追加される) と完全に一致し、存在しない場合にのみ追加します。

より良い解決策はありますか?

4

4 に答える 4

0

私があなたを誤解していない限り、contains メソッドはそのトリックを行うべきですか?

str1.toLowerCase().contains(",efg,")

于 2013-09-12T16:49:13.590 に答える
0

コレクション インターフェイス自体には、contains メソッドがあります。コレクションに正確な文字列がまだ含まれていない場合にのみ追加することを検討している場合は、それを使用して、内容を反復処理することから自分自身を救うことができます。コレクションの contains メソッドは、そのコンテンツに「equals」メソッドを使用します。

http://docs.oracle.com/javase/6/docs/api/java/util/Collection.html#contains(java.lang.Object)

于 2013-09-12T16:51:50.200 に答える
0

あなたはそのようなことを試すことができます:

@Test
public void addString() {
    String target = "abc,bcd,aefg";
    String stringToAdd = "efg";

    System.out.println(target);

    if(! new HashSet<String>(Arrays.asList(target.split(","))).contains(stringToAdd)) {
        target += "," + stringToAdd;
    }
    System.out.println(target);

    if(! new HashSet<String>(Arrays.asList(target.split(","))).contains(stringToAdd)) {
        target += "," + stringToAdd;
    }
    System.out.println(target);
}

出力

abc,bcd,aefg
abc,bcd,aefg,efg
abc,bcd,aefg,efg

コードは次のように進みます。

  1. を使用して分割,
  2. 配列をリストに変換し、それをHasSet のコンストラクターに渡すことにより、結果の配列のセットを作成します
  3. Set のメソッドcontainsを使用して、追加する文字列が存在するかどうかを確認します
  4. 存在しない場合は追加

編集

この正規表現を使用してみることができます^efg|^efg,.+|.+,efg,.+|.+,efg$:

@Test
public void addStringRegEx() {
    String target = "abc,bcd,aefg";
    String stringToAdd = "efg";

    String regex =
            // string is alone at first position and there is no comma
            "^" + stringToAdd + 
            "|" +  // OR
            // string is first and is followed by a comma and some other strings
            "^" + stringToAdd + ",.+" +   
            "|" +   // OR
            // string is enclosed within two commas i.e. in the middle
            ".+," + stringToAdd + ",.+" +
            "|" +   // OR
            // string is at the end and there are some strings and comma before it
            ".+," + stringToAdd + "$";

    Assert.assertFalse("aefg".matches(regex));
    Assert.assertFalse(",efg".matches(regex));
    Assert.assertFalse("efg,".matches(regex));
    Assert.assertFalse(",efg,".matches(regex));

    Assert.assertTrue("efg".matches(regex));
    Assert.assertTrue("foo,efg".matches(regex));
    Assert.assertTrue("foo,efg,bar".matches(regex));
    Assert.assertTrue("efg,bar".matches(regex));


    if(! target.matches(regex)) {
        target += "," + stringToAdd;
    }
    Assert.assertEquals("abc,bcd,aefg,efg", target);

    if(! target.matches(regex)) {
        target += "," + stringToAdd;
    }
    Assert.assertEquals("abc,bcd,aefg,efg", target);
}
于 2013-09-12T17:06:37.397 に答える
0

セットで使えますか?

String[] strings = myString.split(",");
Set<String> set;
//add strings to set

そうでない場合:

String[] strings = myString.split(",");
List<String> list;
for (String s : strings) {
   if(!list.contains(s)) {
       list.add(s);
   }
}
于 2013-09-12T16:51:12.993 に答える