Der Collections.frequency
Ansatz ist ein nett. Allerdings ist die Behauptung, dass die map.values()
nicht funktioniert, ein bisschen seltsam, wie es funktionieren sollte. Ich würde hinzufügen, dass es besonders seltsam ist, da die Sammlung an die Collections.frequency
die map.values()
übergeben wurde.
// method to do the counting of a particular animal in a map
static Integer countAnimal(String animal, Map<Integer, String> map)
{
int cnt = 0;
for (String val : map.values()) {
if (val.equals(animal)) {
++cnt;
}
}
return new Integer(cnt);
}
public static void main(String[] args)
{
String[] animals = new String[] { "cat", "dog", "pig", "goat", "donkey", "horse", "cow" };
Map<Integer, String> map = new HashMap<>();
// load map for test
Random rnd = new Random();
for (int i = 0; i < 100; ++i) {
String animal = animals[rnd.nextInt(animals.length)];
map.put(new Integer(i), animal);
}
// count how many there are
Map<String, Integer> numAnimals = new HashMap<>();
for (String animal : animals) {
numAnimals.put(animal, countAnimal(animal, map));
}
System.out.println(numAnimals);
// show the cool Collections.frequency approach
Integer count = Collections.frequency(map.values(), "dog");
System.out.println("dog: " + count);
}
Beispiel Ausgang:
{horse = 18, cat = 13, Esel = 23, Kuh = 15, Ziege = 17, Hund = 3, pig = 11}
Hund: 3
EDIT: ein Update, das eine Zeichenfolge ermöglicht Aufspalten der Zählung zu finden. Grundsätzlich teilt countAnimal
die Zeichenfolge, die von der Karte abgerufen wird, und überprüft dann jedes Token, um festzustellen, ob es sich um ein Tier handelt. Auch der Testfall wurde leicht geändert. Es funktioniert basierend auf dem aktualisierten Kommentar. Es berücksichtigt jedoch nicht Plural. Der triviale Fall von "Katze" und "Katzen" ist leicht zu handhaben, aber "Maus" und "Mäuse" wären schwieriger und erfordern zusätzliche Arbeit.
public static void main(String[] args)
{
String[] animals = new String[] { "cat", "dog", "pig", "goat",
"donkey", "horse", "cow" };
Map<Integer, String> map = new HashMap<>();
// load map for test
map.put(1, "My friend has a horse");
map.put(2, "A bear can be big"); // will not be found
map.put(3, "A cat is a loner");
map.put(4, "A dog is man's best friend");
map.put(5, "The cat and the dog are peacefully sleeping");
// count how many there are
Map<String, Integer> numAnimals = new HashMap<>();
for (String animal : animals) {
numAnimals.put(animal, countAnimal(animal, map));
}
System.out.println(numAnimals);
}
static Integer countAnimal(String animal, Map<Integer, String> map)
{
int cnt = 0;
for (String val : map.values()) {
// split the val by space
String[] tokens = val.split("[\\s]+");
for (String token : tokens) {
if (token.equalsIgnoreCase(animal)) {
++cnt;
}
}
}
return new Integer(cnt);
}
Beispiel Ausgabe:
{Pferd = 1, cat = 2, Esel = 0, Kuh = 0, Ziege = 0, Hund = 2, Schwein = 0}
Wenn Sie eine Zählung mit "0" für nicht gefunden wollen, warum verwenden Sie ein Boxed 'Integer'-Objekt als Ergebnis, und nicht normalen' int'-Wert? – Andreas