Results 1 to 5 of 5
Thread: Java Map class
- 04-20-2009, 06:35 PM #1
Member
- Join Date
- Apr 2009
- Posts
- 12
- Rep Power
- 0
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
- 04-20-2009, 06:42 PM #2
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
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.
- 04-20-2009, 06:50 PM #3
Member
- Join Date
- Apr 2009
- Posts
- 12
- Rep Power
- 0
thanks for your answer - any other handy tibbets?
- 04-20-2009, 07:28 PM #4
Senior Member
- Join Date
- Nov 2008
- Posts
- 286
- Rep Power
- 5
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:
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.Java Code:Map<Integer,List<String>> idToStrings = new HashMap<Integer,List<String>>(100);
Last edited by neilcoffey; 04-20-2009 at 07:31 PM. Reason: added note about access time scaling
Neil Coffey
Javamex - Java tutorials and performance info
- 04-21-2009, 10:03 AM #5
Member
- Join Date
- Apr 2009
- Posts
- 12
- Rep Power
- 0
Hi,
so i went for this interface
so my various implementations can change.Java Code:public interface LookupMap { public Map<Number,Number[]> getLookupMap(); public void setLookupMap(Map<Number,Number[]> lookupMap); public Number[] getLookupMapResult(Number lookupValue); }
One such is
Java 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
the implementation of getLookupMapResult in this instance is fine but i am having trouble finding an efficient way to provide other lookup methods.Java 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); } }
Any help much appreciated.
Similar Threads
-
mapping java class with xml
By k2kcall in forum XMLReplies: 0Last Post: 03-27-2009, 09:11 AM -
Need a Help with Java Class
By yasir17 in forum New To JavaReplies: 5Last Post: 09-28-2008, 02:38 PM -
class.java to class.exe
By f_the_cook in forum New To JavaReplies: 11Last Post: 07-16-2008, 02:24 PM -
Java Calendar Class
By vasu18 in forum New To JavaReplies: 1Last Post: 12-22-2007, 02:51 AM -
Im new to java. how do i fix the problem with class
By lexlukkia in forum New To JavaReplies: 2Last Post: 11-18-2007, 04:47 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks