problem with hashtable

inserting 2 values with same key

Page 1 of 1

5 Replies - 5125 Views - Last Post: 15 December 2008 - 07:45 AM Rate Topic: -----

#1 IvicaEdge  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-December 08

problem with hashtable

Posted 14 December 2008 - 07:57 PM

Hi, i have a problem with hashtable, when i put 2 values with same key i get some address, but when i put more than 2 values with same keys it works well, can someone help me?

thanks

import java.util.*;
public class Main {
	boolean c;
	
	Hashtable table;
	Main(){
		table = new Hashtable();}

	void insert(Object K, Object V){
		Object existing = null;
		 c = table.containsKey(K);
		 if(c == true){
		 existing = K;}
	   if(existing == null)
	   table.put(K, V);
	   else
		   if(table.get(K) instanceof Object[]){
			  
			   ArrayList a = new ArrayList();
			   a.addAll(Arrays.asList((Object[]) table.get(K)));
			   a.add(V);
			   table.put(K, a);
		   }
		   else
		   {
			   table.put(K, new Object[]{table.get(K),V});
		   }
	}
	Object find(Object K){
		Object str;
		if(table.get(K) != null)
			return table.get(K);
		else
			str = "That key doesn't exist";
			return str;
	}

	public void pecati(){
		String key = new String();
	   Enumeration keys = table.keys();
			while ( keys.hasMoreElements() )
   {
				key = (String)keys.nextElement();
				System.out.println(key+": "+table.get(key));

   }
   
	}
	public static void main(String[] args) {

		Main book = new Main();
		book.insert( "WA" , "Washington" );
		book.insert( "NY" , "New York" );
		book.insert( "RI" , "Rhode Island" );
		book.insert( "RI" , "2nd Rhode Island" );
		book.insert( "BC" , "British Columbia" );
		book.insert("RI", "3th Rhode Island");

		book.pecati();
		System.out.println("=========");
		System.out.println("Looking for RI: "+ book.find("RI"));


	}

  

}



Is This A Good Question/Topic? 0
  • +

Replies To: problem with hashtable

#2 Programmist  Icon User is offline

  • Refactorer in Chief
  • member icon

Reputation: 255
  • View blog
  • Posts: 1,843
  • Joined: 02-January 06

Re: problem with hashtable

Posted 15 December 2008 - 04:07 AM

HashTable implements the Map<K,V> interface. Read the second sentence in the Map Javadoc (link provided). Also read the description of the put method and you'll understand the behavior.
Was This Post Helpful? 1
  • +
  • -

#3 IvicaEdge  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-December 08

Re: problem with hashtable

Posted 15 December 2008 - 05:42 AM

"If the map previously contained a mapping for the key, the old value is replaced by the specified value"
if it's like this, so how can i put more values with same key... and get the value i need ?, i mean i don't want you to write me code..if you can just to tell me what do i need to use.....array of LinkedLists ?

This post has been edited by IvicaEdge: 15 December 2008 - 05:43 AM

Was This Post Helpful? 0
  • +
  • -

#4 IvicaEdge  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-December 08

Re: problem with hashtable

Posted 15 December 2008 - 06:58 AM

i think i got the point, thanks

This post has been edited by IvicaEdge: 15 December 2008 - 06:58 AM

Was This Post Helpful? 0
  • +
  • -

#5 Programmist  Icon User is offline

  • Refactorer in Chief
  • member icon

Reputation: 255
  • View blog
  • Posts: 1,843
  • Joined: 02-January 06

Re: problem with hashtable

Posted 15 December 2008 - 07:17 AM

I'm a little hesitant to suggest this lest I get myself entangled in a thread on a Monday morning because I know that I'm probably going to be too busy today to really follow up. However, I figure some help is better than no help. The behavior you are looking for (multiple values per key) is not available using Java Map<k,V>. In the past when I needed to map one key to multiple values I have created a Map<K, List<V>>. Basically, a map that maps one key to a list of values. This approach was clunky and required lots of code that inserted values to check for a null list and then create one if necessary. This class was a part of my personal library that I took with me from company to company. Lucky for you (and me) Google Collections was released a few years ago and it includes just such a data structure, called a Multimap<K,V>. The Multimap<K,V> behaves just like a Map<K,V> except that you can enter more than one value per key and when you call MultiMap.get(K key) it returns a List<V> of the values for that key. Have a look at Google collections. It's a very useful library. It can also save you some keystrokes if you use the static methods to create your collections (e.g. Maps.newHashMap(), Lists.newArrayList(), etc).
Was This Post Helpful? 0
  • +
  • -

#6 IvicaEdge  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-December 08

Re: problem with hashtable

Posted 15 December 2008 - 07:45 AM

I checked in "Thinking in Java" book, which it's really good book for Java, and i saw that Map<K, List<V>>, and thanks for the info, now i see some things better :)

hf

This post has been edited by IvicaEdge: 15 December 2008 - 07:47 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1