8

If I have a data structure

Stock
{
 String Symbol;
 LinkedHashMap<Date,Double> DateForPrice; 
}

I know in the LinkedHashMap, I can get the stock price of specific date without traversing the whole list.

However, if I want to iterate through the LinkedHashMap of DateForPrice starting from a specific date, are there any way to do it without traversing the whole list?

Mark Elliot
77.3k23 gold badges146 silver badges162 bronze badges
asked Jul 6, 2011 at 2:43
2
  • 2
    Even if you can jump to a particular place, the iteration order of a LinkedHashMap will be the insertion-order, not the natural ordering of the keys; is that what you want? Commented Jul 6, 2011 at 2:48
  • Yes, that's what I want. The the stock price will be inserted in the order of date. Commented Jul 6, 2011 at 2:52

2 Answers 2

11

LinkedHashMap doesn’t offer a way to start iterating in the middle of its ordered view of the map’s data. Supposing your use case is really that you want all dates after some Date d and to iterate those, then you should probably store your map as a TreeMap. An important distinction here is that LinkedHashMap’s ordering is the insertion-order, and our supposed use-case here is that you want the natural key-order. TreeMaps maintain such a view, sorting the contents of the map by the map’s key.

TreeMaps have the additional benefit of allowing you to create slices of the map based on the key, so you can call tailMap(K k), to return the map with all keys occurring after k. In this case, you can call tailMap with your starting point, d.

e.g.:

TreeMap<Date, Double> dateForPrice;
// load up dateForPrice
Date start = // the point to start your iteration
for(Entry<Date, Double> entry : dateForPrice.tailMap(start).entrySet()){
 // loop code
}

tailMap method returns SortedMap, which is not iterable. But it has entrySet method returning Set, which is subinterface of Iterable.

Conveniently, if you want to keep storing your data in a LinkedHashMap you can simply load up a TreeMap with your current instance (with some performance tradeoff, of course):

TreeMap<Date, Double> dateSortedDateForPrice = new TreeMap<Date, Double>(dateForPrice);
Palec
13.7k8 gold badges77 silver badges145 bronze badges
answered Jul 6, 2011 at 2:52
2
  • in addition to tailMap() you can use subMap(Object fromKey, Object toKey), if you want to bound the new map you are creating. download.oracle.com/javase/1.4.2/docs/api/java/util/… Commented Jul 6, 2011 at 2:58
  • 1
    +1: Worth mentioning that what you really want is any NavigableMap e.g. TreeMap. Commented Jul 6, 2011 at 5:28
0

I'd suggest to use TreeMap instead - it will be sorted by date and you can use tailMap to get the required portion

answered Jul 6, 2011 at 2:51

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.