ListOrderedMap位于commons-collections的jar包里,与普通的map相比,ListOrderedMap的key可保持原有顺序,在某些情况比如做报表的时候,非常实用。
代码例子如下:
public class TestListOrderedMap { public static void main(String[] args) { Map<String, String> map = new HashMap<String, String>(); map.put("1", "Test1"); map.put("2", "Test2"); map.put("3", "Test3"); map.put("4", "Test4"); Set<Entry<String, String>> entrySet1 = map.entrySet(); for (Entry<String, String> entry : entrySet1) { System.out.println(entry.getKey() + ":" + entry.getValue()); } Map<String, String> listOrderedMap = ListOrderedMap.decorate(new HashMap()); listOrderedMap.put("1", "Test1"); listOrderedMap.put("2", "Test2"); listOrderedMap.put("3", "Test3"); listOrderedMap.put("4", "Test4"); Set<Entry<String, String>> entrySet2 = listOrderedMap.entrySet(); for (Entry<String, String> entry : entrySet2) { System.out.println(entry.getKey() + ":" + entry.getValue()); } } }
时间: 2024-10-31 00:06:03