我有以下格式的JSON响应,我将解析该响应以获取每个键的键(key_as_String)和值(Expected_Usage和Actual_Usage)。
"aggregations": {
"Inner_aggregation": {
"doc_count": 366,
"Hours_aggregation": {
"doc_count": 366,
"by_day": {
"buckets": [
{
"key_as_string": "2016-01-11",
"key": 1452556800000,
"doc_count": 1,
"Expected_Usage": {
"value": 5
},
"Actual_Usage": {
"value": 3
}
},
{
"key_as_string": "2016-01-12",
"key": 1452556800000,
"doc_count": 1,
"Expected_Usage": {
"value": 43
},
"Actual_Usage": {
"value": 2
}
},
.........,
.........
]
}
}
}
}
}我想保持插入顺序,因为弹性搜索返回的键已经排序。我还想维护每个键的值顺序。
为此考虑使用LinkedHashmap和LinkedHashSet。
LinkedHashMap<String, LinkedHashSet<Integer>> LinkedMap =
new LinkedHashMap<String,LinkedHashSet<Integer>>();
LinkedHashSet<Integer> LinkedSet =
new LinkedHashSet<Integer>();
LinkedSet.add(3);
LinkedSet.add(4);
LinkedSet.add(2);
LinkedMap.put("2016/03/11",LinkedSet);
for(Map.Entry m:LinkedMap.entrySet()){
System.out.println("Key is : " + m.getKey() + " Values: " + m.getValue());
}在内存和性能方面有什么更好的选择吗?
发布于 2016-10-09 22:59:23
有许多选择,主要取决于您将如何使用获取的数据--例如,如果您不关心调用代码修改数据,使用公共成员设计结构将产生最小的CPU压力。
响应中的最大可能键值为365,每个键只有2个值。
我明白了。我建议删除LinkedHashSet并编写一个自定义类来保存这两个整数--取消装箱的(不要浪费CPU将int转换为Integer和back):
public class ExpectedVsActual {
// if you don't care too much of your data integrity
// along other lines of coding, make those public
// and forget about getters
protected int expected;
protected int actual;
public ExpectedVsActual(int exp, int act) {
this.expected=exp;
this.actual=act;
}
public int getExpected() {
return this.expected;
}
public int getActual() {
return this.actual;
}
}然后
LinkedHashMap<String, ArrayList<ExpectedVsActual>> myMap=...; // etc当然,如果你不需要搜索钥匙,那么你就不需要地图了。
如果您想要每个键的各个条目,也许最好将每个条目包装成一个结构:
public class MyEntryRepresentation {
protected String dateStr;
// ArrayList: faster iteration by position
// LinkedList: memory conservative - doesn't allocate more than necessary
protected List<ExpectedVsActual> data;
public MyEntryRepresentation(String date)
this.dateStr=date;
this.data=new ArrayList<ExpectedVsActual>();
}
public void addEntry(int expected, int actual) {
this.data.add(new ExpectedVsActual(expected, actual));
}
public List<ExpectedVsActual> getValues() {
// if you don't care what the caller will do with your List
return this.data;
// If you want to forbid them to modify the returned list
// return Collections.unmodifiableList(this.data);
}
public String getDateStr() {
return this.date;
}
} 然后
LinkedHashMap<String, MyEntryRepresentation> map=... etc;
map.add(entry.getDateStr(), entry);发布于 2016-10-09 22:42:31
linkedhashmap使用内部的entry Array,无论您将新值放入映射中,都会创建一个新条目,并且新条目具有引用前一个条目的意义。此外,我不建议使用set,因为如果在Expected_Usage和Actual_Usage之间有相同的值,则在集合中只有一个值。我创建了一个简单的数据结构来解决您的问题,希望这能有所帮助。
public static class DataStructure{
private final String key;
private List<Integer> values;
public DataStructure(String key){
this.key = key;
values = new ArrayList<Integer>();
}
public void addValue(Integer value){
values.add(value);
}
public List<Integer> getValues(){
return Collections.unmodifiableList(values);
}
public String getKey(){
return this.key;
}
}
List<DataStructure> datas = new ArrayList<DataStructure>();
DataStructure data = new DataStructure("2016/03/11");
data.addValue(1);
data.addValue(2);
datas.add(data);https://stackoverflow.com/questions/39948969
复制相似问题