public List<String> getMaxNameFromList(List<String> list){
String s1 = "";
int max_cnt = 0;
String max_str = "";
String tmp = "";
String regex;
Pattern p;
Matcher m;
String tot_str = list.toString();
for(String str : list) {
if (tmp.equals(str)) continue;
tmp = str;
regex = str;
p = Pattern.compile(regex);
m = p.matcher(tot_str);
int cnt = 0;
while(m.find()) {
cnt++;
}
//System.out.println(str + ":" + cnt);
if (cnt > max_cnt) {
max_cnt = cnt;
max_str = str;
}
}
List<String> list1 = new ArrayList<String>() ;
list1.add(max_cnt+"");//list1.get[0]即为出现最多的次数
list1.add(max_str);//list1.get[1]即为出现次数最多的元素
return list1;
}
🐞标题:java8 lambda 统计list中对象的重复次数
👽作者:ruige
🐾地址:https://jjdhhc.com/articles/2020/11/07/1604720915840.html
🙏感恩:谢谢您的打赏与支持!中间图片是我的微信公众号,扫码关注哦!