Reputation: 678
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:
A Company object has a symbol, company name, sector, and financial data fields.
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: http://data.okfn.org/data/core/s-and-p-500-companies/r/constituents.csv ), 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).
Upvotes: 0
Views: 119
Reputation: 2957
If you can use a database, then it would be much easier. No need to create a map to hold all the Company objects in memory. Persist all the Company objects as rows into the database, with may be symbol as the primary key. This makes searching with symbols straight. Then you can index the database table for company name column, which will make querying with company name also straight. Though, it will take more space and time for insert operations, read operations will be much faster. Similarly, you can create index for other fields/columns like market cap, according to which the companies are often queried. Rather than writing the logic to query the company objects as per different fields, you can leave this work to the database and just query the database for what you want.
Upvotes: 1
Reputation: 2401
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.
Sidenote:
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.
Upvotes: 2
Reputation: 43728
If you have the symbol or the company name given exactly when you search, you could use a Map
where you enter each company twice. Once with the symbol as key and once with the name as key.
Upvotes: 1