Get values from HashMap as reference

user4774371

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

Thomas

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Get values from HashMap as reference

From Dev

Get one of the values from Hashmap with multiple values

From Dev

How to get 5 highest values from a hashmap?

From Dev

HashMap not updating its values by reference

From Dev

How to get the values from the Hashmap without using Iterator?

From Dev

How to get multiple values for multiple keys from Hashmap in a single operation?

From Dev

How to get particular hashmap keys and values from json objects

From Dev

How to get the values from the Hashmap without using Iterator?

From Dev

How to get values from a Java HashMap in javascript function using Rhino

From Dev

Delete duplicated values from Map/HashMap and get the most frequent value

From Dev

Get 5 highest values from a hashmap while preserving the insertion order

From Dev

Averaging values from a hashmap

From Dev

Removal of Values from a HashMap

From Dev

Firebase database how to get some value from 1 reference and equals to 2 reference to get its keys and values

From Dev

Storing and retrieving values from hashmap

From Dev

How can I use java 8 streams to get values from my hashmap

From Dev

How to get values from hashmap in struts2 and compare it in <s:if test>?

From Dev

Traverse Set elements and get values using them as keys from a hashmap using Scala

From Dev

How can I use java 8 streams to get values from my hashmap

From Dev

Cant get int from Hashmap

From Dev

get data from hashmap into highcharts

From Dev

Why are the keys and values of a borrowed HashMap accessed by reference, not value?

From Java

How to get the 3 highest values in a HashMap?

From Dev

Get ArrayList<HashMap<String, String>> values

From Dev

How to get the 3 highest values in a HashMap?

From Dev

ArrayList<HashMap<String,String>> get values

From Dev

Store multiple values in HashMap and get it later

From Dev

java Hashmap : Get the key with maximum number of values

From Dev

selecting values from a reference table

Related Related

  1. 1

    Get values from HashMap as reference

  2. 2

    Get one of the values from Hashmap with multiple values

  3. 3

    How to get 5 highest values from a hashmap?

  4. 4

    HashMap not updating its values by reference

  5. 5

    How to get the values from the Hashmap without using Iterator?

  6. 6

    How to get multiple values for multiple keys from Hashmap in a single operation?

  7. 7

    How to get particular hashmap keys and values from json objects

  8. 8

    How to get the values from the Hashmap without using Iterator?

  9. 9

    How to get values from a Java HashMap in javascript function using Rhino

  10. 10

    Delete duplicated values from Map/HashMap and get the most frequent value

  11. 11

    Get 5 highest values from a hashmap while preserving the insertion order

  12. 12

    Averaging values from a hashmap

  13. 13

    Removal of Values from a HashMap

  14. 14

    Firebase database how to get some value from 1 reference and equals to 2 reference to get its keys and values

  15. 15

    Storing and retrieving values from hashmap

  16. 16

    How can I use java 8 streams to get values from my hashmap

  17. 17

    How to get values from hashmap in struts2 and compare it in <s:if test>?

  18. 18

    Traverse Set elements and get values using them as keys from a hashmap using Scala

  19. 19

    How can I use java 8 streams to get values from my hashmap

  20. 20

    Cant get int from Hashmap

  21. 21

    get data from hashmap into highcharts

  22. 22

    Why are the keys and values of a borrowed HashMap accessed by reference, not value?

  23. 23

    How to get the 3 highest values in a HashMap?

  24. 24

    Get ArrayList<HashMap<String, String>> values

  25. 25

    How to get the 3 highest values in a HashMap?

  26. 26

    ArrayList<HashMap<String,String>> get values

  27. 27

    Store multiple values in HashMap and get it later

  28. 28

    java Hashmap : Get the key with maximum number of values

  29. 29

    selecting values from a reference table

HotTag

Archive