Map内部无法排序,简单实现一下。
package com.hp.service.urlparam;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class FIFOMap {
private Map<String, Object> map;
private List<String> list;
public FIFOMap() {
map = new HashMap<String, Object>();
list = new ArrayList<String>();
}
public Object get(String key) {
return map.get(key);
}
public List<String> getKeyList() {
return list;
}
public void put(String key, Object value) {
list.add(key);
map.put(key, value);
}
public void putAll(FIFOMap subMap) {
list.addAll(subMap.getKeyList());
map.putAll(subMap.getMap(subMap));
}
private Map<String, Object> getMap(FIFOMap map) {
Map<String, Object> result = new HashMap<String, Object>();
Iterator<?> it = map.getKeyList().iterator();
while (it.hasNext()) {
String key = it.next().toString();
Object value = map.get(key);
result.put(key, value);
}
return result;
}
public boolean containsKey(String key) {
return list.contains(key);
}
public String toString() {
String result = "{";
Iterator<?> it = this.getKeyList().iterator();
while (it.hasNext()) {
String key = it.next().toString();
Object value = this.get(key);
result += "[" + key + "] : " + value + "\n";
}
result += "}";
return result;
}
}