2015-11-02 38 views
0

时间戳我有填充有时间戳(高达毫秒)一个ArrayList从交易用java

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,其中第一?

+0

什么格式的时间戳?数据类型? –

+0

你不是要求代码,是吗? – Seelenvirtuose

+0

@TimB他们是字符串。我将稍后将它们转换为jfreechart Regulartimeperiod二级 –

回答

1

你必须使用一个ArrayList的包含所有时间戳和一个String作为重点和Integer为值一个新的HashMap,其中包含String时间戳和Integer是一个计数器。喜欢这个;

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

然后,你必须使用一个for循环之前的值与ArrayList的当前值进行比较之后插入在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()); 
    } 
+1

谢谢!这工作完美。它不适用于没有排序的时间戳,但可以通过Collections.sort(al) –

+0

哦!我没有想过,但无论如何乐意帮助你。大!!! – Shivam

1

创建一个类看起来像:通过输入数据

public class TransactionsPerSecond { 
    long time; 
    int transactions=1; //Start at 1 to count the initial one 
} 

循环。如果时间与当前的TransactionsPerSecond对象不匹配,则创建一个新的对象,否则为当前的事务计数加1。

// For you to do, create results arraylist. 

TransactionsPerSecond current = null; 

for (String str: inputData) { 

    // for you to do - parse str into a Date d. 
    Date d = ???; 

    if (current == null || d.getTime() != current.time) { 
     current = new TransactionsPerSecond(); 
     current.time = d.getTime(); 
     results.add(current); 
    } else { 
     current.transactions++; 
    } 
}