Java 从HashMap获取值作为引用
是否可以从Java 从HashMap获取值作为引用,java,hashmap,linkedhashmap,shallow-copy,Java,Hashmap,Linkedhashmap,Shallow Copy,是否可以从HashMap获取值列表作为参考 类MyCustomObject{ 字符串名; 整数id; MyCustomObject(字符串名称,整数id){ this.name=名称; this.id=id; } } HashMap=newlinkedhashmap(); map.put(1,新的MyCustomObject(“abc”,1)); map.put(2,新的MyCustomObject(“xyz”,2)); 列表=新的ArrayList(map.values()); Log.i(
HashMap
获取值列表作为参考
类MyCustomObject{
字符串名;
整数id;
MyCustomObject(字符串名称,整数id){
this.name=名称;
this.id=id;
}
}
HashMap=newlinkedhashmap();
map.put(1,新的MyCustomObject(“abc”,1));
map.put(2,新的MyCustomObject(“xyz”,2));
列表=新的ArrayList(map.values());
Log.i(标签,“************来自HashMap的列表****************”;
用于(MyCustomObject s:列表){
Log.i(标签,“name=“+s.name”);
}
list.set(0,新的MyCustomObject(“temp”,3));
Log.i(标记,“*************更新后来自HashMap的列表****************”;
用于(MyCustomObject s:列表){
Log.i(标签,“name=“+s.name”);
}
Log.i(标签,“************来自HashMap的列表****************”;
List list2=新的ArrayList(map.values());
对于(MyCustomObject s:list2){
Log.i(标签,“name=“+s.name”);
}
输出 在这里,如果从
HashMap
获取值列表,它将返回deepcopy
更新
我的要求
请务必说明,如果任何第三方库提供了此类数据结构,或者处理这种情况的最佳方法是什么您正在根据
地图的值创建一个新的列表
:
List<MyCustomObject> list = new ArrayList<>(map.values());
List List=newarraylist(map.values());
这就是创建值集合
副本的原因,而该列表
中的更改无法反映在原始的映射
中
如果直接修改map.values()
返回的集合(例如,map.values().remove(新的MyCustomObject(“abc”,1))
),它将反映在原始映射的内容中。但是,您无法在集合
上调用集合
,因为集合
没有该方法
集合值()
返回此映射中包含的值的集合视图。这个
集合由映射支持,因此映射的更改会反映出来
在集合中,反之亦然
因此,使用集合并将values()
分配给它。或者entrySet()
尝试使用映射支持的映射项,这些映射项是通过调用entrySet()
获得的。这些功能的列表几乎可以像您希望的那样工作(尽管我仍然主张您直接使用map.put(key,updatedValue)
)
例如:
Map<String, Integer> map = new HashMap<>();
map.put( "a", 1 );
map.put( "b", 2 );
//you create a list that's not backed by the map here but that isn't a problem
//since the list elements, i.e. the entries, are backed by the map
List<Entry<String, Integer>> entryList = new ArrayList<>(map.entrySet());
entryList.get(0).setValue( 5 );
System.out.println( map ); //prints: {a=5, b=2} (note that order is a coincidence here)
Map Map=newhashmap();
地图放置(“a”,1);
地图放置(“b”,2);
//您可以创建一个没有地图支持的列表,但这不是问题
//因为列表元素(即条目)由映射支持
List entryList=newarraylist(map.entrySet());
entryList.get(0.setValue)(5);
System.out.println(map);//prints:{a=5,b=2}(注意这里的顺序是一致的)
不过最后一点要注意的是:正如我在评论中所说,处理映射顺序并不总是确定的(除非您知道您正在处理的是像TreeMap
这样的有序映射)因此,使用索引可能会引入错误或不希望出现的行为。这就是为什么在大多数情况下,您至少要检查密钥,因此您需要使用Map.Entry
(顺便说一句,出于良好的原因,不能更改其密钥)或者直接使用该键,在这种情况下,无论如何都不需要值或条目的列表/集合。list list list=new ArrayList(map.values())
你认为这行是做什么的?它是浅拷贝,你到底想要什么?扰流板:问题是hashmap不知道索引,所以你不能得到一个由映射支持的列表。即使有可能list.set(0,随便什么)
对映射没有多大意义,因为您需要以任何方式访问/检查键,在这种情况下,您可以调用map.put(key,任意)
。我想要的是,如果我从列表中更新MyCustomObject,它应该在HashMap中反映出我的建议,即使用entrySet()
以及entry.setValue(…)
更新该条目。@Thomas+1从我这里说,entrySet比值更常用,因为键经常起作用。这与我需要的最接近。如果我使用LinkedHashMap而不是HashMap,顺序是否确定?将映射。entrySet()按相同顺序提供数据always@penguinLinkedHashMap
具有确定的顺序,但用于处理列表索引的算法可能仍然不知道它。此外,LinkedHashMap
更像是一种折衷/混合,就地图部分而言,索引没有任何意义。这意味着我发现它很危险依靠不打算以这种方式使用的折衷方案或集合财产。
Map<String, Integer> map = new HashMap<>();
map.put( "a", 1 );
map.put( "b", 2 );
//you create a list that's not backed by the map here but that isn't a problem
//since the list elements, i.e. the entries, are backed by the map
List<Entry<String, Integer>> entryList = new ArrayList<>(map.entrySet());
entryList.get(0).setValue( 5 );
System.out.println( map ); //prints: {a=5, b=2} (note that order is a coincidence here)