首页 > 编程语言 > 详细

java实现一个简单的计数器

时间:2018-10-19 17:13:07      阅读:341      评论:0      收藏:0      [点我收藏+]
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);


}
}

java实现一个简单的计数器

原文:https://www.cnblogs.com/coderdxj/p/9817477.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!