Gorgochef:
だから私は、キーは単語の長さのオフに基づいて2つのマップを持っています。長さの同じ量を持つ単語があるたびに、私はそれがキーに追加します。
私が使用してこれをしたいmap.merge()
機能を、しかし、ドキュメンテーションを見た後、私はそうするかどうかはわからないと思います。私は他のリソースを見てみましたが、多くはないが役立っています。
Map<Integer, String> map = new HashMap<Integer, String>();
Map<Integer, String> map = new HashMap<Integer, String>();
String[] wordsSplit = doc.split(" ");
for(int i = 0; i < wordsSplit.length; i++) {
int key = wordsSplit[i].length();
Map<Integer, String> tempMap = new HashMap<Integer, String>();
tempMap.put(key, wordsSplit[i]);
//merge here
map.merge(key, map.keySet(), map.get(key) + ", " + wordsSplit[i]);
}
編集:ここで私は古いマップ上に一時的なマップをマージのコンテキストでマッピングする方法を把握しようとしていますので、この質問は異なっています。
キー:たとえば、この手段で同じキーを共有するいくつかの項目がある場合は、それはそれ以上のものとして判明するでしょう: "Car, bar, Tar"
Hulya:
まず、あなたが区切ら同じ長さの連結ワードにマージ機能を必要とします", "
。
BiFunction<String, String, String> mergeFunction = (i, j) -> {
return i + ", " + j;
};
その後map.merge(key, x, mergeFunction);
の要素マージwordsSplit
それらの長さに応じてアレイ。以下はkey
、単語の長さx
の略wordsSplit[i]
Stream.of(wordsSplit).forEach(x -> {
int key = x.length();
map.merge(key, x, mergeFunction);
});
それとも、できるだけのマージ機能のボディ過去(i, j) -> { return i + ", " + j; }
にmap.merge()
代わりに定義するのmergeFunction
別々 :
Stream.of(wordsSplit).forEach(x -> {
int key = x.length();
map.merge(key, x, (i, j) -> { return i + ", " + j; });
});