Results 1 to 5 of 5

Thread: Java Map class

  1. #1
    thorne_ is offline Member
    Join Date
    Apr 2009
    Posts
    12
    Rep Power
    0

    Default 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

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    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.

  3. #3
    thorne_ is offline Member
    Join Date
    Apr 2009
    Posts
    12
    Rep Power
    0

    Default

    thanks for your answer - any other handy tibbets?

  4. #4
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default

    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:

    Java 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.
    Last edited by neilcoffey; 04-20-2009 at 07:31 PM. Reason: added note about access time scaling

  5. #5
    thorne_ is offline Member
    Join Date
    Apr 2009
    Posts
    12
    Rep Power
    0

    Default

    Hi,

    so i went for this interface

    Java 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

    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

    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);
    	
    	}
    	
    	
    }
    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.

Similar Threads

  1. mapping java class with xml
    By k2kcall in forum XML
    Replies: 0
    Last Post: 03-27-2009, 09:11 AM
  2. Need a Help with Java Class
    By yasir17 in forum New To Java
    Replies: 5
    Last Post: 09-28-2008, 02:38 PM
  3. class.java to class.exe
    By f_the_cook in forum New To Java
    Replies: 11
    Last Post: 07-16-2008, 02:24 PM
  4. Java Calendar Class
    By vasu18 in forum New To Java
    Replies: 1
    Last Post: 12-22-2007, 02:51 AM
  5. Im new to java. how do i fix the problem with class
    By lexlukkia in forum New To Java
    Replies: 2
    Last Post: 11-18-2007, 04:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •