Search code examples
javahashmaplinkedhashmapshallow-copy

Get values from HashMap as reference


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);
}

Output

**************** 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

  1. I want list of values from HashMap because I want to access items using their position
  2. I want to preserve order of values
  3. If I modify anything in the extracted list then it should reflect in HashMap too

Please do tell, if any third party library provide such data structure, or what would be best approach to handle this situation


Solution

  • 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.