0

I am storing a data in HashMap and getting the value in later stage.

HashMap<String, byte[]> hm = new HashMap<String, byte[]>();

Now, I want to store two more values into it. For example, I want to store info like below. Could someone please advise me, how can i modify the Hashmap to ahieve this way? I also require to read all these stored values and find some value from it in later stage.

Key 1
    IPAddress
    RandomNumber
    Byte data
Key 2
    IPAddress
    RandomNumber
    Byte data

Thank you!

Stella
  • 1,728
  • 5
  • 41
  • 95

3 Answers3

2

You have to create a class with these properties:

class MyData{
    private String IPAddress;
    private long RandomNumber;
    private byte[] data;

    //getters setters...
}

Map<String, MyData> hm = new HashMap<String, MyData>();

You can get the values as:

MyData dataObj = hm.get("Key 1");
dataObj.getRandomNumber();

or directly

  hm.get("Key 1").getData();
  hm.get("Key 1").getRandomNumber();

To iterate over the map:

    Iterator it = hm.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry myDataEntry = (Map.Entry)it.next();
        System.out.println(myDataEntry.getKey() + " = " + myDataEntry.getValue());
        it.remove(); // avoids a ConcurrentModificationException
    }

Taken from here: Iterate through a HashMap

Community
  • 1
  • 1
Evgeni Dimitrov
  • 21,976
  • 33
  • 120
  • 145
  • OK..How to retrieve the values back? Lets say I want to read all the values and pick a particular item back? Others doubts, 1.) HashMap stores the data permanently or temporarily? 2.) Is it possible to delete a particular record from the entire list? – Stella Feb 15 '14 at 20:12
  • I edited the answer for the access to the properties. For doubt 1 - the data will be stored as long as the `hm` object is in scope/is not collected by GC. Doubt 2 - you can use the hashmap's remove method, see http://www.tutorialspoint.com/java/util/hashmap_remove.htm – Evgeni Dimitrov Feb 15 '14 at 20:20
  • Hi, I couldn't get code to go through all data to retrieve. How can i go through all the data using for loop? – Stella Feb 16 '14 at 16:41
  • I updated the answer. To iterate with for loop see the second answer from the link. – Evgeni Dimitrov Feb 16 '14 at 16:54
  • Hi, How would i get RandomNumber() inside this loop? – Stella Feb 16 '14 at 17:46
  • I could able to get it. – Stella Feb 16 '14 at 18:32
  • entry.getValue().getRandomNumber(); – Evgeni Dimitrov Feb 16 '14 at 20:35
  • thank you so much! Need urgent help, Could you please help on this query as well-> http://stackoverflow.com/questions/21823938/hashmap-data-not-appending-new-data-into-arraylist – Stella Feb 17 '14 at 08:19
0

Create a class like:

public class Key{
    int randomNumber;
    byte[] data;
    String ipAddress;
}

And store it like a value of your Map.

Map<String, Key> map;

Hope it helps.

eltabo
  • 3,749
  • 1
  • 21
  • 33
0

I see two options for your issue:

1 - make a bean to wrap all the needed content (as advised by other posters)

2 - if you want to have more values for a single key and adding a new library to your project is not an issue you can use google guava library, more particularly the Multimap class. There you can have more values for a single key.

Nevertheless I would advise in writing a java bean that wraps the content you want in a single object.

Olimpiu POP
  • 5,001
  • 4
  • 34
  • 49