Thread: Bidirectional map out of two hashmaps

Page 1 of 3 123 LastLast
Results 1 to 10 of 21
  1. #1 Bidirectional map out of two hashmaps 
    Respected Member


    kLeptO's Avatar
    Join Date
    Dec 2006
    Age
    28
    Posts
    2,955
    Thanks given
    1,183
    Thanks received
    754
    Rep Power
    3084
    Well I needed this and I didin't want to take a whole different lib just to use one class.
    So I made this one, it's quite basic and simple.
    Since I'm not so good in java, I need to know how this should be done correctly - all feedback is welcome.

    Code:
    import java.util.HashMap;
    import java.util.Map;
    
    /**
     * BidirectionalMap - The bidirectional hash-map.
     * 
     * @author wOok
     */
    public class BidiMap<K, V> {
    
    	/**
    	 * The regular type hash-map.
    	 */
    	private Map<K, V> map = new HashMap<K, V>();
    
    	/**
    	 * The inverted type hash-map.
    	 */
    	private Map<V, K> invertedMap = new HashMap<V, K>();
    
    	/**
    	 * Puts data into bidirectional map.
    	 * 
    	 * @param key
    	 *            the key
    	 * @param value
    	 *            the value
    	 */
    	public void put(K key, V value) {
    		map.put(key, value);
    		invertedMap.put(value, key);
    	}
    
    	/**
    	 * Gets data from the bidirectional map.
    	 * 
    	 * @param key
    	 *            the key
    	 * 
    	 * @return the value
    	 */
    	public Object get(Object key) {
    		Object entry = map.get(key);
    		if (entry == null) {
    			entry = invertedMap.get(key);
    		}
    		return entry;
    	}
    
    	/**
    	 * Removes the key from bidirectional map.
    	 * 
    	 * @param key
    	 *            the key
    	 */
    	public void remove(Object key) {
    		boolean first = false;
    		Object entry = map.get(key);
    		if (entry == null) {
    			first = true;
    			entry = invertedMap.get(key);
    		}
    		if (first) {
    			map.remove(entry);
    			invertedMap.remove(key);
    		} else {
    			invertedMap.remove(entry);
    			map.remove(key);
    		}
    	}
    
    	/**
    	 * Gets the bidirectional map size.
    	 * 
    	 * @return the bidirectional map size
    	 */
    	public int size() {
    		return map.size();
    	}
    
    }
    Example how it works:
    Code:
    BidiMap<Integer, Integer> map = new BidiMap<Integer, Integer>();
    map.put(10, 50);
    
    map.get(10); // this will return 50
    map.get(50); // this will return 10
    Well it can be used in many cases, as Graham mentioned before a good example is to convert normal item id to noted one, and from note back to item.
    Reply With Quote  
     

  2. #2  
    Renown Programmer
    veer's Avatar
    Join Date
    Nov 2007
    Posts
    3,746
    Thanks given
    354
    Thanks received
    1,370
    Rep Power
    3032
    bidirectional map sorry
    may want to differentiate between keys/values
    Reply With Quote  
     

  3. #3  
    Registered Member
    Mister Maggot's Avatar
    Join Date
    Dec 2008
    Posts
    7,227
    Thanks given
    3,283
    Thanks received
    2,875
    Rep Power
    5000
    Well it can be used in many cases, as Graham mentioned before a good example is to convert normal item id to noted one, and from note back to item.
    Code:
    import java.util.HashMap;
    
    public class Items {
        private static HashMap<Integer, ItemDefinition> items = new HashMap<Integer, ItemDefinitions>();
    
        public static class ItemDefinition {
            private int id;
            private int notedId;
    
            private ItemDefinition(id, notedId) {
                this.id = id;
                this.notedId = notedId;
            }
        }
    
        public static synchronized(?) ItemDefinition getItem(int i) {
            ItemDefinition d = items.get(i);
            d = d == null? items.put(Cache.loadItemDefinition(i)) : d;
            return d;
        }
    }
    A bit overkill for something like that.

    Wrote in browser; fuck off with errors if there are any.
    Reply With Quote  
     

  4. #4  
    Respected Member


    kLeptO's Avatar
    Join Date
    Dec 2006
    Age
    28
    Posts
    2,955
    Thanks given
    1,183
    Thanks received
    754
    Rep Power
    3084
    Quote Originally Posted by filth View Post
    synchronized keyword with wildcard in parentheses??? must be new feature of java
    I think he didin't knew if he should synchronize it or not. So he put I question mark there for someone like you to answer.
    Reply With Quote  
     

  5. Thankful user:


  6. #5  
    Renown Programmer
    veer's Avatar
    Join Date
    Nov 2007
    Posts
    3,746
    Thanks given
    354
    Thanks received
    1,370
    Rep Power
    3032
    maggot's doesn't go from noted to normal though...
    Reply With Quote  
     

  7. Thankful users:


  8. #6  
    Registered Member
    Mister Maggot's Avatar
    Join Date
    Dec 2008
    Posts
    7,227
    Thanks given
    3,283
    Thanks received
    2,875
    Rep Power
    5000
    The noted id would return the definition for both the noted and the unnoted?
    Reply With Quote  
     

  9. #7  
    Renown Programmer
    veer's Avatar
    Join Date
    Nov 2007
    Posts
    3,746
    Thanks given
    354
    Thanks received
    1,370
    Rep Power
    3032
    It's called a bidi map damnit stfu
    Reply With Quote  
     

  10. #8  
    Respected Member


    kLeptO's Avatar
    Join Date
    Dec 2006
    Age
    28
    Posts
    2,955
    Thanks given
    1,183
    Thanks received
    754
    Rep Power
    3084
    Quote Originally Posted by super_ View Post
    It's called a bidi map damnit stfu
    How am I supposed to know that. Thanks.
    Reply With Quote  
     

  11. #9  
    Community Veteran


    Join Date
    Jan 2008
    Posts
    2,659
    Thanks given
    494
    Thanks received
    627
    Rep Power
    980
    Quote Originally Posted by wOok View Post
    Well it can be used in many cases, as Graham mentioned before a good example is to convert normal item id to noted one, and from note back to item.
    Simpler solution:
    Code:
    public class Item {
    	private final int index; // >Initialised elsewhere
    	private final int noteIndex; // >Initialised elsewhere
    
    	private boolean noted = false;
    
    	public int getIndex() {
    		return index;
    	}
    
    	public int getNoteIndex() {
    		return noteIndex;
    	}
    
    	public boolean isNoted() {
    		return noted;
    	}
    
    	public void setNoted(boolean noted) {
    		this.noted = noted;
    	}
    }
    Code:
    int index = item.isNoted() ? item.getNoteIndex() : item.getIndex();
    I just set it out this way for the sake of example.
    ~iKilem
    Reply With Quote  
     

  12. #10  
    Renown Programmer
    veer's Avatar
    Join Date
    Nov 2007
    Posts
    3,746
    Thanks given
    354
    Thanks received
    1,370
    Rep Power
    3032
    1) the first reply...
    2) make it less bad plz
    3) shut up; the idea is to have note() and denote() not Item.noteId() Item.normalId()
    Reply With Quote  
     

Page 1 of 3 123 LastLast

Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. HashMaps
    By Arco iris Mágico in forum Website Development
    Replies: 41
    Last Post: 04-24-2010, 03:55 AM
  2. Your guide to: Using HashMaps
    By Ultimate in forum Tutorials
    Replies: 20
    Last Post: 04-16-2009, 12:17 PM
  3. HashMaps
    By Vastiko in forum Tutorials
    Replies: 7
    Last Post: 12-25-2008, 05:31 PM
  4. Hashmaps?
    By Mrquarterx in forum Application Development
    Replies: 3
    Last Post: 12-15-2008, 04:12 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
  •