统计每个地区的人数
有List集合,集合中数据如下:
List list=new ArrayList();
list.add("张三,北京");
list.add("李四,上海");
list.add("王五,北京");
。。。。。。
统计每个地区的人数
package work;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class Work04_AddPeopleCount {
public static void main(String[] args) {
List<String> list=new ArrayList<String>();
list.add("张三,北京");
list.add("李四,上海");
list.add("王五,北京");
HashMap<String, Integer> hashMap = new HashMap<String, Integer>();
for(int i = 0; i < list.size();i++) {
String[] str = list.get(i).split(",");
String key = str[1];
Integer value = hashMap.get(key);
if(value == null) {
hashMap.put(key, 1);
}else {
value++;
hashMap.put(key, value);
}
}
System.out.println(hashMap);
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210224175829134.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM4MzMyNDUz,size_16,color_FFFFFF,t_70)