In the recent project, the report needs to be aggregated because all the items are dynamic and can only be added dynamically.
The idea is to use map, initialize all maps, and then aggregate and overwrite them in the map.
With HashMap, the order of all dynamic items after initialization is messed up.
map<string, double> totalmap = new hashmap<string, double> ();
For (map<string, object> map:itemlist) {
Itemsql + = "SUM (" +map.get ("item_no") + ") as" +map.get ("item_no") + ",";
Totalmap.put (Map.get ("Item_no"). ToString (), 0d);
}
TreeMap also can not meet my needs, treemap belong to the natural sort. Dynamic items are definitely manually adjusted, and I just need to sort by the put order.
Google, found that the HashMap subclass Linkedhashmap can implement the iteration order (both the insertion Order) code as follows:
map<string, double> totalmap = new linkedhashmap<string, double> (); For statistical dynamic items
For (map<string, object> map:itemlist) {
Itemsql + = "SUM (" +map.get ("item_no") + ") as" +map.get ("item_no") + ",";
Totalmap.put (Map.get ("Item_no"). ToString (), 0d);
}
All right, this is done.
Linkedhashmap sorting map According to put order