あなたはマップの魔女の中にあなたの言葉を記入することができますキーを単語と値の発生数。
マップループでデータをもう一度入力し終わったら、値が大きいか等しいかを再度確認し、大きい場合はマップから削除します。 そして、あなたはあなただけabc
またはABC
を使用したくない場合は、挿入したとき、あなたはLowerCase
やUpperCase
を使用したCA ユニークワード
の数を知ることが、最終的にこのマップのサイズを返すことができますあなたの地図で。ここで
あなたが投げる行くことができ、簡単な例は:
public static void main(String[] args) {
//i consider you know how to learn from a file, i use a simple array just to explain
String[] listWords = {"hello", "word", "hello", "stack", "HELLO", "WORD", "123", "what?"};
//fill your words in your map
Map<String, Integer> map = new HashMap<>();
for (String word : listWords) {
if (!map.containsKey(word.toLowerCase())) {
map.put(word.toLowerCase(), 1);
} else {
map.put(word.toLowerCase(), map.get(word.toLowerCase()) + 1);
}
}
//print your map
map.entrySet().forEach((entry) -> {
System.out.println("word : " + entry.getKey() + " occurence : " + entry.getValue());
});
System.out.println("**************************************************");
//loop throw your map and remove the words which occurrence > 1
for (Iterator<Map.Entry<String, Integer>> it = map.entrySet().iterator(); it.hasNext();) {
Map.Entry<String, Integer> entry = it.next();
if (entry.getValue() > 1) {
it.remove();
}
}
//print your map again
map.entrySet().forEach((entry) -> {
System.out.println("word : " + entry.getKey() + " occurence : " + entry.getValue());
});
//size of your end map
System.out.println("Size of map = " + map.size());
}
あなたがマップでプレイするものをベースにすることができますいくつかの良い参考文献:
How to update a value, given a key in a java hashmap?
iterating over and removing from a map
・ホープ、このあなたにアイデアを与えることができます。
ありがとうございました。 –