package com.fengunion.sf;
import org.junit.platform.commons.util.StringUtils;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
public class CountUtil {
private static Map<String, Integer> map = null;
static {
map = new HashMap<String, Integer>();
}
/**
*
* 实现一个简单的计数器
* @param str
* @return
*/
public static int countNum(String str) {
if (StringUtils.isNotBlank(str)) {
Integer count = map.get(str);
if (count == null) {
count = 1;
} else {
count++;
}
map.put(str, count);
}
return map.get(str);
}
public static void main(String[] args) {
for(int i = 0; i<new Random().nextInt(50); i++){
countNum("a");
}
countNum("a");
countNum("b");
countNum("b");
countNum("c");
int counta = countNum("a");
int countb = countNum("b");
countNum("c");
countNum("c");
int countc = countNum("c");
System.out.println(" countaNum: " + counta + " countbNum: " + countb + " countcNum: " + countc);
}
}
【推荐】博客园的心动:当一群程序员决定开源共建一个真诚相亲平台
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】Flutter适配HarmonyOS 5知识地图,实战解析+高频避坑指南
【推荐】开源 Linux 服务器运维管理面板 1Panel V2 版本正式发布
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步