2

I need to store key-value pairs inside key-value pairs and so on as required by the data.

I tried to achieve this using Hash-Map in java but it didn't work as I expected it to. Since, my data has multiple name->value pairs, but the Hash-Map keeps overwriting the name key's value with the new one.

Say for example, I need to store a list of products and their details for an online store. Which data structure would help me achieve this and how ?

TIA.

3
  • How about redis server? tutorialspoint.com/redis/index.htm It is often referred to as a data structure server since keys can contain strings, hashes, lists, sets and sorted sets Jun 5, 2015 at 9:05
  • you do have json library for java provided by json.org. you can use that.
    – David Haim
    Jun 5, 2015 at 9:06
  • The problem with using a json library is the performance overhead introduced due to serializing and de-serializing the json string.
    – Anjul Garg
    Jun 5, 2015 at 9:07

3 Answers 3

5

If you want a 'generic' collection, why not use a Guava MultiMap ?

A collection that maps keys to values, similar to Map, but in which each key may be associated with multiple values. You can visualize the contents of a multimap either as a map from keys to nonempty collections of values ... or as a single "flattened" collection of key-value pairs

However, it sounds like you may really want to model your entities as proper objects, with the appropriate attributes etc. Storing collections of collections of collections will work, but you'll lose some of the benefits of defining objects with behaviour/polymorphism etc.

e.g.

List<Product> products = ...
products.add(new Book(...));
products.add(new Dvd(...));

(or perhaps a map of stock item id to object instance, or, or, or...)

I would treat JSON as a representation of your object (e.g. like XML). If you need to transport or store these objects, translate them to a format such as JSON or XML. But manage them within your program using objects with type and behaviour.

4
  • Thanks Brian, I looked into it. Can I use this MultiMap without a model class ?
    – Anjul Garg
    Jun 5, 2015 at 9:09
  • You can use it with any object (subject to equals/hashCode being defined correctly) Jun 5, 2015 at 9:10
  • Thanks a lot. I gave its documentation a quick read and I think this would be perfect. One more thing, can I convert an sql resultset to a Guava MultiMap ?
    – Anjul Garg
    Jun 5, 2015 at 9:23
  • I should think so. Perhaps simply iterate through the resultset, convert each row to an object and write it in given a suitable key Jun 5, 2015 at 9:25
2

You need a MultiMap, not present by default in Java but you can find it in libraries like Guava or Apache Commons.

0

As @maskacovnik wrote- use your own class :)

Btw you can also work with JSON in java - API docs there

If you need some container you can use also a hashMap - with all values saved as string eg. (for multi-valued keys you can use value in format eg. csv ..)

Example of Hashmap data structure:

name - name
surname - surname
functions - func1;funct2,etc..

And while you will need to read values of keys, you can parse them into array by string.split(regex)

Not the answer you're looking for? Browse other questions tagged or ask your own question.