Reputation:
Is it possible to get list of values from HashMap
as a reference
class MyCustomObject {
String name;
Integer id;
MyCustomObject(String name, Integer id){
this.name = name;
this.id = id;
}
}
HashMap<Integer, MyCustomObject> map = new LinkedHashMap<>();
map.put (1, new MyCustomObject("abc",1));
map.put (2, new MyCustomObject("xyz",2));
List<MyCustomObject> list = new ArrayList<>(map.values());
Log.i(TAG,"************ List from HashMap ************");
for (MyCustomObject s : list) {
Log.i(TAG,"name = "+s.name);
}
list.set(0,new MyCustomObject("temp",3));
Log.i(TAG,"************ List from HashMap after update ************");
for (MyCustomObject s : list) {
Log.i(TAG,"name = "+s.name);
}
Log.i(TAG,"************ List from HashMap ************");
List<MyCustomObject> list2 = new ArrayList<>(map.values());
for (MyCustomObject s : list2) {
Log.i(TAG,"name = "+s.name);
}
**************** List from HashMap ***************
name = abc
name = xyz
**************** List from HashMap after update ***************
name = temp
name = xyz
**************** List from HashMap ***************
name = abc
name = xyz
Here if get list of values from HashMap
it return deep-copy.
Update
My Requirement
Please do tell, if any third party library provide such data structure, or what would be best approach to handle this situation
Upvotes: 2
Views: 1796
Reputation: 88747
Try using the map entries which are backed by the map and which you get by calling entrySet()
. A list of those almost works like you want it to do (although I'd still advocate you directly use map.put( key, updatedValue )
.
Example:
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)
One final note though: as I already stated in my comment when dealing with a map order is not always deterministic (unless you know you're dealing with an ordered map like TreeMap
) and thus using indices may introduces bugs or undesired behavior. That's why you'll want to at least check the key in most cases and thus you either need to use Map.Entry
(which btw can't have its key altered, for good reasons) or use the key directly in which case you don't need a list/collection of values or entries anyways.
Upvotes: 1
Reputation: 393996
You are creating an new List
based on the values of the Map
:
List<MyCustomObject> list = new ArrayList<>(map.values());
That's what creates the copy of the values Collection
, and changes in that List
cannot be reflected in the original Map
.
If you modify the Collection returned by map.values()
directly (for example, map.values().remove(new MyCustomObject("abc",1))
), it will be reflected in the contents of the original Map
. You wouldn't be able to call set
on the Collection
, though, since Collection
doesn't have that method.
Upvotes: 5
Reputation: 109603
Collection values()
Returns a Collection view of the values contained in this map. The collection is backed by the map, so changes to the map are reflected in the collection, and vice-versa.
So use a Collection and assign values()
to it. Or the entrySet()
.
Upvotes: 3