• java实现一个简单的计数器


    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);


    }
    }
  • 相关阅读:
    2021.6.2 Python网络编程
    2021.6.1 数据库
    2021.5.29 PHP大作业
    2021.5.28 bootstrap和vue
    2021.5.27 三个和尚
    2021.5.26 Python操作Mysql数据库
    2021.5.25 PHP作业
    2021.5.24 Python解析XML
    如何用 GPU 训练模型?
    44 内核中的中断处理(上)
  • 原文地址:https://www.cnblogs.com/coderdxj/p/9817477.html
Copyright © 2020-2023  润新知