Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags: , , , , , ,

Sponsored Links



Problem in accessing content of HashMap

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 13-02-2010
Member
 
Join Date: Dec 2009
Posts: 204
Problem in accessing content of HashMap

Hello,
I program a mini dictionary, where words and definitions are stored in a hashmap. The words match (as String) to keys and definitions (also a String) to values. To view the content of my HashMap, I use a method display (), which runs the hashmap using the entrySet method () that I travel with an iterator.
Here is my code:
Code:
Public class Dictionary{
	/ *
* Field class
* /
	protected HashMap hm;

	/ *
* Default constructor
* /
	Public Dictionary(){
		hm =new HashMap();
	}
	
	Public Dictionary ajouterMot(Forgot m, Definition d){
		hm.could(m.getMot(), d);
System.out.System.out.println("The word"+ m.getMot()+"Which has deated"d + +"Has been added to hm");
		return this;
	}
	
	Public boolean contientMot(Forgot m){
		return hm.containsKey(m.getMot());
	}
	
	Public void view(){
		Set s = hm.entrySet();
Iterator iter = s.iterator();
System.out.System.out.println("The words contained in the dictionary are:");
		while(iter.hasNext()) {
    			System.out.System.out.println(iter.next());
    	        }
		
    	
        }
	
	
}
Reply With Quote
  #2  
Old 13-02-2010
Member
 
Join Date: Nov 2009
Posts: 335
Re: Problem in accessing content of HashMap

Hello,
This is what appears when the toString () has been redefined, that is , have a look at the following statement in your code
Code:
getClass().getName() + '@' + Integer.toHexString(hashCode())
If you are not understanding what I am trying to say here , then you can refer to the java document at the sun official web site. And even there you can find the API for java which will provide you with all the methods and the functions.
Reply With Quote
  #3  
Old 13-02-2010
Member
 
Join Date: Dec 2009
Posts: 192
Re: Problem in accessing content of HashMap

Hello,
Even I was trying for the same kind of a problem but I have not successfully programmed it yet. I can not use the LinkedHashMap, because the specification for this project requires the use of a HashMap. I tried some changes, change the order of additions of words in the dict, which modify the definitions, but the display order does not change. I do not understand, because the display order is neither added nor sorted Alphabetically or sort alphabetically definitions, although these last two, I do not see how this sort could intervene as it is scheduled.
Reply With Quote
  #4  
Old 13-02-2010
Member
 
Join Date: Dec 2009
Posts: 204
Re: Problem in accessing content of HashMap

Hello,
Well, it works, the problem is corrected with a LinkedHashMap. Thank you for that and so now I want to change the order of the contents of my LinkedHashMap, regardless of the insertion order? if I add my definitions associated with the word order in a rash, and I want to sort the words (and therefore the definitions at the same time), knowing that words are key to the hashmap?
Reply With Quote
  #5  
Old 13-02-2010
Member
 
Join Date: Nov 2009
Posts: 583
Re: Problem in accessing content of HashMap

Hello,
If the project requires that you should use a HashMap and keep the order then he will have to change technique. Use an integer for the order. And put in your HashMap object type foods that you must define. With all the attributes you need.
Code:
HashMap h = new Hashtable ();
h.put (integer (converted to string), food);
So when you trail:
Code:
for (int i = 0; i <h.size (); i + +) (
A = Food (Food) h.get (i);
a.toString ();
}
Reply With Quote
  #6  
Old 13-02-2010
Member
 
Join Date: Nov 2009
Posts: 333
Re: Problem in accessing content of HashMap

Hello,
I have a code with me, if you want it you can refer to it. Here is the code
Code:
Public class TestCode {
 
	/ **
* @ Param args
* /
	Public static void hand(String[] args) {
	 
	 try {
		 	HashMap m = new HashMap();
m.could("cle3","val3");
m.could("key2","val2");
m.could("key1","val1");
TreeMap tm = new TreeMap(m);
		 	/ / On the trail all keys 
		 	Iterator it = tm.keySet().iterator(); 
		 	while(it.hasNext()){
		 		String key = it.next().function toString() {
    [native code]
}();
System.out.System.out.println("key:>"key + +"- Value:>"+ tm.get(cle));
		 	}
		 	
		 	
	} catch (Exception e) {
		/ / TODO Auto-generated catch block
		e.printStackTrace();
	}
	  
 
	}
 
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development


Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Problem in accessing content of HashMap"
Thread Thread Starter Forum Replies Last Post
Problem accessing certain websites Edward Lee Small Business Server 6 21-07-2010 05:22 PM
Problem Accessing WU: 0x800C0002 noone Windows Update 7 11-01-2010 05:38 AM
Problem in accessing Outlook NetworkeR Windows Software 5 24-12-2009 11:59 AM
Accessing content restricted to a specific country Gluteus Tips & Tweaks 3 03-10-2008 02:33 PM
ActiveX problem when accessing PWA asadim Microsoft Project 1 30-03-2007 09:05 PM


All times are GMT +5.5. The time now is 03:53 AM.