首页 > 其他分享 >给定一个字符串列表List<String> strList, 统计里面每一个字符串的出现次数。 如: {

给定一个字符串列表List<String> strList, 统计里面每一个字符串的出现次数。 如: {

时间:2022-10-26 11:07:45浏览次数:42  
标签:aa Map reult List count 字符串 strList public

给定一个字符串列表List<String> strList, 统计里面每一个字符串的出现次数。

如:

{"aa", "aa", "b"}

输出:

{"aa", 2}, {"b", 1}

补充完整下面的方法:
public Map<String, Integer> calStringArray(List<String> strList) {
}

 

package baseTest;
import java.util.*;
public class Test {
public static Map<String,Integer> calStringArray(List<String> strList){
Map<String,Integer> reult=null;
if(strList!=null){
reult=new HashMap<>();
for (String s : strList) {
Integer count = reult.get(s);
if(count==null){
count=1;
}else{
count++;
}
reult.put(s,count);
}
}
return reult;
}
public static void main(String[] args) {
String[] strings = {"aa", "aa", "b"};
Map<String,Integer> map = Test.calStringArray(Arrays.asList(strings));
for (Map.Entry<String, Integer> entry:map.entrySet()){
System.out.println(entry.getKey()+":"+entry.getValue()+"\t");
}
}
}

 



标签:aa,Map,reult,List,count,字符串,strList,public
From: https://blog.51cto.com/u_15846941/5797399

相关文章