-
Java Map class
Hi all,
Im using java 5 and am writing a java object to hold a table extracted from a database.
I was told that Maps would be the way to go here as access is 'cheap' and iterators arent needed.
My db table contains a 'lookup value' and two return values in each row.
So my question is - what is the best implementation of the map interface to use for holding this data?
Also is it possible to tell the map to get the nearest value to x, the value lower than x, etc? (Where x is the key value and 'value are the return values for that key)
On the same note i assume (although i havent found it yet) that it is possible to hold 2/more values for each key?
Thanks in advance
-
in a map, there believe that there is no < or >, just equal to. you will need to implement your own methods to perform this type of search.
anyways, look up the api for map/hashmap. you'll see that there are key and value classes where key is whatever type your 'lookup value' is, and the value can be an array or your own class to hold the other data. depends on how you wanna implement it.
and no more than one value per key for hashmap. i personally don't know anything that will do more than a 1:1 ratio, but maybe someone else can help you find something to your liking.
-
thanks for your answer - any other handy tibbets?
-
A plain HashMap gives you 1:1 mapping, on average constant lookup however big the map grows, and no you can't navigate it in order (though you can iterate).
If you need to be able to be able to do lookups of the type "tell me the value lower than x", then look at TreeMap (or anything that implements NavigableMap-- if you need concurrent access, use ConcurrentSkipListMap). This has methods such as higherEntry(), lowerEntry().. These maps give you on-average-constant access for a given fixed number of elements, but the actual access time does increase as the map grows (but it increases at a uniform rate as the size doubles-- it's not so bad in the end).
To perform a one-to-many mapping, with the standard collections you need to map to a list. So you declare for example:
Code:
Map<Integer,List<String>> idToStrings = new HashMap<Integer,List<String>>(100);
As I recall, the Google collections API has a multimap, although it's not terribly much code to just manage things yourself as a map of key to list as mentioned.
-
Hi,
so i went for this interface
Code:
public interface LookupMap
{
public Map<Number,Number[]> getLookupMap();
public void setLookupMap(Map<Number,Number[]> lookupMap);
public Number[] getLookupMapResult(Number lookupValue);
}
so my various implementations can change.
One such is
Code:
public abstract class GenericLookupMap implements LookupMap
{
private Map<Number, Number[]> lookupMap = null;
public GenericLookupMap() {
this.setLookupMap(new HashMap<Number, Number[]>());
}
public Map<Number, Number[]> getLookupMap() {
return this.lookupMap;
}
public void setLookupMap(Map<Number, Number[]> lookupMap) {
this.lookupMap = lookupMap;
}
}
which is inherited by
Code:
public class FractionLookupMap extends GenericLookupMap
{
public Number[] getLookupMapResult(Number lookupValue) {
//TODO: may have to implement way of getting value nearest lookup value
return this.getLookupMap().get(lookupValue);
}
}
the implementation of getLookupMapResult in this instance is fine but i am having trouble finding an efficient way to provide other lookup methods.
Any help much appreciated.