java 字符串出现次数,计算Java中字符串的出现次数

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

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

解决方案

Map would be a viable data structure for this, if you're just looking to count the number of emails with given last names. The key would be a String (the last name), and the value would be an Integer (number of occurrences).

You instantiate it as follows:

Map nameOccurrences = new HashMap();

To add a value to the map:

nameOccurrences.put("Smith", 1);

To check if a name is in the map:

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

To get a value from the map:

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

Note that names with different capitalization would be considered different keys. If you need to ignore capitalization, you'd have to do something like make the keys all uppercase before adding them to the Map.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值