2015-11-02 67 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++; 
    } 
}