Results 1 to 6 of 6

Thread: Problem in accessing content of HashMap

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

  2. #2
    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.

  3. #3
    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.

  4. #4
    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?

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

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

Similar Threads

  1. Problem Accessing WU: 0x800C0002
    By noone in forum Windows Update
    Replies: 7
    Last Post: 11-01-2010, 05:38 AM
  2. Problem in accessing Outlook
    By NetworkeR in forum Windows Software
    Replies: 5
    Last Post: 24-12-2009, 11:59 AM
  3. Problem accessing the D drive in the Windows 7
    By Ekpah in forum Operating Systems
    Replies: 3
    Last Post: 15-12-2009, 10:39 PM
  4. Accessing content restricted to a specific country
    By Gluteus in forum Tips & Tweaks
    Replies: 3
    Last Post: 03-10-2008, 01:33 PM
  5. ActiveX problem when accessing PWA
    By Lima Jaric in forum Microsoft Project
    Replies: 1
    Last Post: 30-03-2007, 08:05 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,702,568.39568 seconds with 16 queries