|
| |||||||||
| Tags: accessing, content, data, format, hashmap, java, programming language |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| |||
| |||
| 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
| |||
| |||
| 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()) |
|
#3
| |||
| |||
| 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
| |||
| |||
| 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
| |||
| |||
| 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); Code: for (int i = 0; i <h.size (); i + +) ( A = Food (Food) h.get (i); a.toString (); } |
|
#6
| |||
| |||
| 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();
}
}
} |
![]() |
|
| Thread Tools | Search this Thread |
| |
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 |