pnValue.clear();のためにこのコードを書きました。私が得ていた出力はキーのためのヌル値でした。そこであるマップの値をもう一方のマップに追加することは、元のマップへの単なる参照であり、clone()メソッドを使用して2つのマップが別々であることを保証する必要があります。今私のマップをクローンした後に直面している問題は、特定のキーに複数の値があると、それらが上書きされているということです。例えば。LinkedHashMapの値が上書きされるベクトル
{PERSON = [James Fisher],ORGANIZATION=[American League, Chicago Bulls]}
しかし、私が取得することである:私はgoldSentenceを処理することから期待していた出力があり、私は間違っている、私はVector<String>
{PERSON = [James Fisher],ORGANIZATION=[Chicago Bulls]}
for(WSDSentence goldSentence : goldSentences)
{
for (WSDElement word : goldSentence.getWsdElements()){
if (word.getPN()!=null){
if (word.getPN().equals("group")){
String newPNTag = word.getPN().replace("group", "organization");
pnValue.add(word.getToken().replaceAll("_", " "));
newPNValue = (Vector<String>) pnValue.clone();
annotationMap.put(newPNTag.toUpperCase(),newPNValue);
}
else{
pnValue.add(word.getToken().replaceAll("_", " "));
newPNValue = (Vector<String>) pnValue.clone();
annotationMap.put(word.getPN().toUpperCase(),newPNValue);
}
}
sentenceAnnotationMap = (LinkedHashMap<String, Vector<String>>) annotationMap.clone();
pnValue.clear();
}
改変コード ベクターをリストに置き換え、クローンを除去した。しかし、これはまだ私の問題を解決しません。 {PERSON=[], ORGANIZATION=[]}
for(WSDSentence goldSentence : goldSentences)
{
for (WSDElement word : goldSentence.getWsdElements()){
if (word.getPN()!=null){
if (word.getPN().equals("group")){
String newPNTag = word.getPN().replace("group", "organization");
pnValue.add(word.getToken().replaceAll("_", " "));
newPNValue = (List<String>) pnValue;
annotationMap.put(newPNTag.toUpperCase(),newPNValue);
}
else{
pnValue.add(word.getToken().replaceAll("_", " "));
newPNValue = pnValue;
annotationMap.put(word.getPN().toUpperCase(),newPNValue);
}
}
sentenceAnnotationMap = annotationMap;
}
pnValue.clear();
) '。そこには1つ以上のアイテムがありません。 – shmosel
なぜVectorを変更するためにクローンを作成するのですか?変更する前にコピーを取るのが普通です。それはおそらく無意味です。また、1997年以降のコレクションを使用することをお勧めします。配列リスト。 –
私はpnValue.clear()を呼び出さないと、{PERSON = [James Fisher、American League、Chicago Bulls]、ORGANIZATION = [James Fisher、American League、Chicago Bulls]} – serendipity