Number of String Entries in Java

Is there a good class that can count occurrences of specific lines in java? I would like to keep a list of names and then create unique email addresses for each name. For each occurrence of the last name, I would like to increase the number by one.

Ex: If I have 3 people with the last name Smith, I would like their address to be smith1 @ (Address), smith2 @ (Address) and smith3 @ (Address). I saw the Map class, but I cannot initialize it correctly. Is there a class that I can use to store a list of strings and their occurrences?

+2
source share
2 answers

Map , , (), ( ).

:

Map<String, Integer> nameOccurrences = new HashMap<String, Integer>();

:

nameOccurrences.put("Smith", 1);

, :

if (nameOccurrences.containsKey("Smith")) { ... }

:

Integer occurrences = nameOccurrences.get("Smith");

, . , - , , .

+2
+1

Source: https://habr.com/ru/post/1787345/


All Articles