gpt4 book ai didi

java - 使用java根据时间戳计算每秒事务数

转载 作者:行者123 更新时间:2023-12-02 04:14:17 24 4
gpt4 key购买 nike

我有一个充满时间戳的数组列表(最多毫秒)

2015/11/01 12.12.12.990
2015/11/01 12.12.12.992
2015/11/01 12.12.12.999

2015/11/01 12.12.15.135
2015/11/01 12.12.15.995

2015/11/01 12.12.20.135
2015/11/01 12.12.20.200
2015/11/01 12.12.20.300
2015/11/01 12.12.20.900

每个时间戳都是一个事务,我需要计算 tps。我如何获得一个列表列表,最终它会是这样的

2015/11/01 12.12.12, 3
2015/11/01 12.12.12, 2
2015/11/01 12.12.20, 4

其中第一个是第二级上一秒发生的时间戳3、2、4 等是 tps?

最佳答案

您必须使用一个 ArrayList 来包含所有时间戳和一个新的 HashMap,其中 String 作为键,Integer 作为值,其中 String 包含时间戳,Integer 是一个计数器。像这样;

HashMap<String, Integer> hash = new HashMap<>();

然后,在将 ArrayList 的先前值与当前值进行比较后,必须使用 for 循环在 HashMap 中插入时间戳和计数值,如下所示:

if(i>0 && al.get(i).substring(0, 19).equalsIgnoreCase(al.get(i-1).substring(0, 19)))
hash.put(al.get(i).substring(0, 19),count);

那么 HashMap 中的键值就是结果。代码是:

ArrayList<String> al = new ArrayList<String>();
al.add("2015/11/01 12.12.12.990");
al.add("2015/11/01 12.12.12.992");
al.add("2015/11/01 12.12.12.999");
al.add("2015/11/01 12.12.15.135");
al.add("2015/11/01 12.12.15.995");
al.add("2015/11/01 12.12.20.135");
al.add("2015/11/01 12.12.20.200");
al.add("2015/11/01 12.12.20.300");
al.add("2015/11/01 12.12.20.900");

HashMap<String, Integer> hash = new HashMap<>();
int count = 0;
for(int i=0;i<al.size();i++){
if(i>0 && al.get(i).substring(0, 19).equalsIgnoreCase(al.get(i-1).substring(0, 19)))
hash.put(al.get(i).substring(0, 19),++count);
else
hash.put(al.get(i).substring(0, 19),count=1);
}
for (Entry<String, Integer> entry : hash.entrySet()) {
System.out.println(entry.getKey()+","+entry.getValue());
}

关于java - 使用java根据时间戳计算每秒事务数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33477827/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com