Java LinkedHashMap中的LinkedHashSet
我有一个以下格式的JSON响应,我将对其进行解析,以获取每个键的键(key_as_String)和值(预期的_用法和实际的_用法)Java LinkedHashMap中的LinkedHashSet,java,elasticsearch,Java,elasticsearch,我有一个以下格式的JSON响应,我将对其进行解析,以获取每个键的键(key_as_String)和值(预期的_用法和实际的_用法) "aggregations": { "Inner_aggregation": { "doc_count": 366, "Hours_aggregation": { "doc_count": 366, "by_day": { "buckets": [
"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
}
},
.........,
.........
]
}
}
}
}
}
我想保持插入顺序,因为弹性搜索返回的键已经排序。我还希望维护每个键的值顺序
"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());
}
LinkedHashMap LinkedMap=
新建LinkedHashMap();
LinkedHashSet LinkedSet=
新LinkedHashSet();
LinkedSet.add(3);
LinkedSet.add(4);
LinkedSet.add(2);
LinkedMap.put(“2016/03/11”,LinkedSet);
对于(Map.Entry m:LinkedMap.entrySet()){
System.out.println(“键为:+m.getKey()+”值:+m.getValue());
}
在内存和性能方面有更好的替代方案吗?linkedhashmap在内部使用一个条目数组,无论您在映射中添加什么新值,都会创建一个新条目,并且新条目可以引用以前的条目。此外,我不建议使用set,因为如果预期的_用法和实际的_用法之间有相同的值,那么集合中就有一个值。我创建了一个简单的数据结构来解决你的问题,希望能对你有所帮助
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);
公共静态类数据结构{
私有最终字符串密钥;
私有列表值;
公共数据结构(字符串键){
this.key=key;
值=新的ArrayList();
}
公共void addValue(整数值){
增加(价值);
}
公共列表getValues(){
返回集合。不可修改列表(值);
}
公共字符串getKey(){
返回此.key;
}
}
列表数据=新的ArrayList();
数据结构数据=新数据结构(“2016/03/11”);
数据。附加值(1);
数据。附加值(2);
添加(数据);
有很多选择,主要取决于您将如何使用所获取的数据-例如,如果您不关心调用代码修改数据,则使用公共成员设计结构将导致最小的CPU压力
响应中最大可能的键值为365,每个键只有2个值
我明白了。我建议删除LinkedHashSet
,并编写一个自定义类来保存这两个未装箱的整数(不要浪费CPU将int
转换为Integer
):
然后
LinkedHashMap myMap=…;//等
当然,如果你没有;如果你不需要搜索钥匙,那么你就不需要地图了
如果希望每个键都有单独的条目,那么最好将每个条目包装为一个结构:
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;
}
}
公共类MyEntryRepresentation{
受保护字符串dateStr;
//ArrayList:按位置加快迭代
//LinkedList:内存保守-分配的内存不会超过需要
受保护名单数据;
公共MyEntryRepresentation(字符串日期)
this.dateStr=日期;
this.data=new ArrayList();
}
公共无效补遗(预期整数,实际整数){
this.data.add(新的ExpectedVsActual(expected,actual));
}
公共列表getValues(){
//如果你不在乎打电话的人会怎么处理你的名单
返回此.data;
//如果您想禁止他们修改返回的列表
//返回集合.不可修改列表(this.data);
}
公共字符串getDateStr(){
返回此日期;
}
}
然后
LinkedHashMap<String, MyEntryRepresentation> map=... etc;
map.add(entry.getDateStr(), entry);
LinkedHashMap=。。。等
add(entry.getDateStr(),entry);
在什么意义上更好?性能、内存、代码可维护性?:)您在性能方面选择了两个对立的维度。通常,如果牺牲内存,你可以获得额外的速度,反之亦然。:)我懂了。这些是我的要求,按重要性排序。1.保留键顺序和键2内的值顺序。绩效3。内存您会建议任何其他数据结构吗?在您的示例中:您已经牺牲了整数的完整性,将它们存储在集合中,从而丢失了重复的值。因此,你得问问自己:秩序仍然重要吗?此外,您将看起来像日期的内容存储在键中:日期对象可能比字符串表示形式更紧凑(但如果需要人眼读取,您可能需要花费额外的CPU来解析和格式化它们)。“您会建议其他数据结构吗?”在一般情况下:不会。在特定情况下,有了特定于您需求的数据结构,我可能-这取决于这些数据结构以及获取和使用它们的具体情况。Eric,非常感谢!ArrayList是一个很好的选择,可以做我想做的事情。你的代码肯定能用。但我认为它不能防止重复的钥匙。我认为LinkedHashMap+ArrayList会解决这个问题。这正是我想要的。谢谢你,阿德里安!不客气。(毕竟,这就是问题所在)
LinkedHashMap<String, MyEntryRepresentation> map=... etc;
map.add(entry.getDateStr(), entry);