Reputation: 1591
I need a hashmap solution whereby the key is an Investor Object that is unique, and value is a Portfolio object.
The problem is that I need Investors to be sorted by their Portfolio Object, which has a total Value inside, which is changing constantly as he/she buys/sells stocks.
How can one solve this issue? thanks!
Upvotes: 0
Views: 441
Reputation: 719159
The problem is that I need Investors to be sorted by their Portfolio Object, which has a total Value inside, which is constantly as he/she buys/sells stocks.
The "constantly changing" is the tricky part here.
AFAIK, is no efficient (i.e. O(logN) or better performance) data structure that can keep something sorted on a constantly changing value. All general purpose map API's I've come across assume that keys don't change while a mapping is a member of the map.
So, what I think you will have to use an event mechanism to track "change of value" events for the Portfolio
object. The event handler would need to atomically remove the Portfolio from the ordered mapping, make the change, and then add it back in again at the new position.
To implement the data structures you need either a pair of maps or a bidirectional map.
Upvotes: 1
Reputation: 32969
Depending on your requirements, I think the best solution is to store the values in a standard Map
but provide a method to get the Portfolios in order ImmutableSortedSet<Portfolio> getSortedPortfolios()
. The method would get the list of values and put them in the Set
. This assumes that Portfolios implements Comparable
otherwise you would need to provide a Comparator
.
ImmutableSortedSet<Portfolio> getSortedPortfolios(){
return ImmutableSortedSet.builder()
.addAll(myMap.getValues())
.build();
}
If you really need thread-safety I would recommend using a copy-constructor to create snapshots and insert them. This would ensure that the returned sorted list remains sorted.
ImmutableSortedSet<Portfolio> getSortedPortfolios(){
ImmutableSortedSet.Builder<Portfolio> builder = ImmutableSortedSet.builder();
for (Portfolio p : myMap.getValues()){
builder.add(new Portfolio(p));
}
return builder.build();
}
I am using Guava's ImmutableSortedSet here, but you could just use a SortedSet
and treat it as immutable or use Collection.unmodifiable...
Upvotes: 1
Reputation: 5958
As far as I know, Java
collection library does not have a data structure to suit your requirement. But you can use TreeBidiMap
in the common collection library
Upvotes: 0