在報表的開發上,剛好遇到一個問題,我同時要餵資料給兩張iReport template(i.e. P1, P2),然後後端用for loop將裡頭的資料抓出來,原本的方式:
1: Map dataMap = new HashMap ();
2: dataMap.put("NIG071P1", p1Data);
3: dataMap.put("NIG071P2", p2Data);
但是此方式,並沒有如預期的依照順序產生P1, P2,反而是先印出P2、再印出P1,這是因為HashMap並不會根據你insert的順序來將資料抓出來
Solution
改用LinkedHashMap (Hash table and linked list implementation of the Map interface, with predictable iteration order. This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order). )。
這是一個會根據我們insert的順序來把資料抓出來的Map,改寫如下,即可解決上述的問題:
1: //LinkedHashMap defines the iteration ordering,
2: //which is normally the order in which keys were
3: //inserted into the map (insertion-order)
4: Map dataMap = new LinkedHashMap ();
5: dataMap.put("NIG071P1", p1Data);
6: dataMap.put("NIG071P2", p2Data);
No comments:
Post a Comment