Niminim Niminim - 2 months ago 6x
Java Question

Which data structure should I use to handle multi value data?

I started working on an investing project in java and I need a good data-structure to meet its requirements. I've seen some solutions here to a bit similar questions, but with different requirements.

My requirements are as follow:

  1. A Company object has a symbol, company name, sector, and financial data fields.

  2. Searching for a specific company requires a symbol or a company name.

First, I thought about creating a map with symbol as the key and company name as the value (for simplicity assume I harvest the data from here: ), but then I had a second thought because symbol and company name are both Company object data fields, so maybe it's not the best solution OO-wise. Plus, remember it also requires something like a vlookup capability so companies can be found not just by using their symbol but also their name.

What's the best DS to meet my requirements ?
map<String Symbol, Company company>
is good for this purpose?

A few notes:

Assume I also want to have all the data in my program and not just searching for it on the web.

There're 500 objects in this DS, though companies can be added or removed.

The solution should be so searching for all the companies from a specific sector (or other data - like all companies with a market cap higher than 100 B$, etc) will be easy to implement, and efficient.

I don't know if the user enter a symbol or a company name.

The data will also be saved to a file.

The solution should use java core only (not commons, guava etc).


If you really want to use Java classes only, using two Maps is the best option here. While it does need two operations to store a single new company, you will probably read data way more often than you store it. And as long as it's stored as references the additional space required by a second Map is negligible.

However, in the unlikely case that there is a deteministic way to create a company's symbol out of it's name or vice versa, it would be possible to create a hash function which calculates identical hashes for a company's symbol and name. In that case a HashTable/HashMap would be slighlty more efficient.


Since you already save your data to a file, which is a kind of persistent storage, I recommend using a database. The sole purpose of a DBMS is to store, manage and query large amounts of data efficiently making them the best option in most situations. And with Java's SQL libraries java.sql.* it's really easy to integrate as well.