1 2 Previous Next 25 Replies Latest reply: Apr 6, 2007 5:56 AM by 807599 RSS

    How to create a dicitionary using hash map.

    807599
      Hi All,

      I want to create a dictionary using hash map where in i can add new words and there meaning. I have written some code But I am not happy with this. Can you suggest me how to add the words and there meanings to the hash map.

      I am not able to add the words and there meaning to the hash map.
      for this i tried creating a dictionary class.
      // Using hashmap
      import java.util.*;
      
      public class Dictionary
      {
          public static void main(String[] args)
          {
              // Create a hash map
              HashMap hm = new HashMap();
      
              // put elements to the map
              hm.put("Abode: ", "Home");
              hm.put("Balance: ", "An intrument to measure or weigh");
              hm.put("Car: ", "Automobile");
              hm.put("Dinner: ","Last meal of the day generally had after evening and before sleeping");
              hm.put("Embossed: ", "Engraved kind");
                 
              Set set = hm.entrySet();
      
              Iterator i = set.iterator();
      
              while (i.hasNext())
              {
                  Map.Entry me = (Map.Entry) i.next();
                  System.out.print(me.getKey() + ": ");
                  System.out.println(me.getValue());
              }
              System.out.println();
          }
      
      }
      This is the other Dictionary class which i created.
      public class Main_Dictionary
      {
          public static void main(String[] args)
          {
              Content_Dictionary cd = new Content_Dictionary();
              cd.getContent_Dictionary("Abode", "Home");
              cd.displayValues();
          }
      
      }
      
      class Content_Dictionary
      {
          String word, meaning;
      
          Content_Dictionary()
          {
              word = "a";
              meaning = "b";
          }
      
          Content_Dictionary(String x, String y)
          {
              word = x;
              meaning = y;
          }
      
          void getContent_Dictionary(String w, String m)
          {
              word = w;
              meaning = m;
          }
      
          void displayValues()
          {
              System.out.print(word + ": ");
              System.out.println(meaning);
          }
      
      }
      If i create an interface containing all the words and there meaning
      public interface Words_Dictionary
      {
          String Abode = "Home";
          String Dark = "Lacking brightness";
          String Balance = "An intrument to measure or weigh";
          String Car = "Type of Automobile";
          String Dinner = "Last meal of the day";
          String Embossed  = "Engraved kind";
          String Adroit = "SkillFul";
          
      }
      and then create a another class which implements this interface, but how should i add these words and there meaning in the hashmap.
        • 1. Re: How to create a dicitionary using hash map.
          807599
          Since dictionaries typically contain quite a lot of words you might find it easiest
          if the words and their meansings are contained in a separate file. Your dictionary
          can then be a Map of some kind with a method to read the file adding word/meaning
          entries to the map.
          • 2. Re: How to create a dicitionary using hash map.
            807599
            That means i should have a Say a word document containing all the words and there meanings, But I am not good at reading input files.

            What I can gather, acoording to you I should read from a word document and then use readline method and then use " : ' as the string tokenizer and then take the key and value and then add it to the hash map. But I don't know how to do that, If I have to read from a word document, I get confused how to raed from the document and then separate the kay and the value part of it.
            • 3. Re: How to create a dicitionary using hash map.
              807599
              Keep it simple: use a text document and avoid any tricky tokenising by putting the
              word on one line, then the definition on the next.
              • 4. Re: How to create a dicitionary using hash map.
                807599
                I tried creating word document but i was unable to figure out how to do the operations on the word document and its content,

                So This is what i could come up with, i just created one class Dictionary , now I am able to search for the meaning of the word specified by me, and I am able to print all the words and there meanings added to the hashmap, but i am not able to figure out how to add a new word and the meaning in the hashmap and how to find is a word is there or not...

                This is the code i wrote:
                import java.util.*;
                
                public class Dictionary
                {
                
                    public static void main(String[] args)
                    {
                        // Create a hash map
                        HashMap<String, String> hm = new HashMap<String, String>();
                
                        // add the elements to the hash map
                        hm.put("Abode", "Home");
                        hm.put("Balance", "An intrument to measure or weigh");
                        hm.put("Car", "Automobile");
                        hm.put("Dinner", "Last meal of the day");
                        hm.put("Embossed", "Engraved kind");
                
                        // To Find The meaning of the word specified
                        String word = "Car";
                        System.out.println("\nMeaning of the Word Specified is: ");
                        System.out.printf("%s:\t%s\n", word, hm.get(word));
                        
                        //To Print all content of the Dicitonary
                        System.out.println("\nHere are the Contents of the dictionary: ");
                        for (Map.Entry<String, String> e : hm.entrySet())
                            System.out.printf("%s : %s\n", e.getKey(), e.getValue());
                    }
                
                }
                The output which I am getting is this:

                Output :

                Meaning of the Word Specified is:
                Car:     Automobile

                Here are the Contents of the dictionary:
                Car : Automobile
                Embossed : Engraved kind
                Dinner : Last meal of the day
                Abode : Home
                Balance : An intrument to measure or weigh
                • 5. Re: How to create a dicitionary using hash map.
                  800282
                  I tried creating word document but i was unable to
                  figure out how to do the operations on the word
                  document and its content,

                  So This is what i could come up with, i just created
                  one class Dictionary , now I am able to search for
                  the meaning of the word specified by me, and I am
                  able to print all the words and there meanings added
                  to the hashmap, but i am not able to figure out how
                  to add a new word and the meaning in the hashmap and
                  how to find is a word is there or not...
                  May I suggest a slightly different approach?
                  Do not create a String ==> String mapping, but a String ==> List<String> mapping. And do NOT place everything inside your main method.
                  Here's a small demo:
                  import java.util.ArrayList;
                  import java.util.Map;
                  import java.util.HashMap;
                  import java.util.List;
                  
                  public class Dictionary {
                  
                      private Map< String, List<String >> dictionary;
                      
                      public Dictionary() {
                          dictionary = new HashMap< String, List<String >>();
                      }
                      
                      public void addWord(String base, String meaning) {
                          List<String> allMeanings = dictionary.get(base);
                          if(allMeanings == null) { // if null, there is no entry mapped to 'base' yet: create a new one
                              List<String> newMeanings = new ArrayList<String>();
                              newMeanings.add(meaning);
                              dictionary.put(base, newMeanings);
                          } else {
                              allMeanings.add(meaning);
                          }
                      }
                      
                      public List<String> search(String base) {
                          // your code here
                          return null;
                      }
                      
                      // ... other methods ...
                      
                      public String toString() {
                          StringBuilder strb = new StringBuilder();
                          for(Map.Entry< String, List<String >> e : dictionary.entrySet()) {
                              strb.append(e.getKey());
                              strb.append(" : ");
                              strb.append(e.getValue());
                              strb.append('\n');
                          }
                          return strb.toString();
                      }
                      
                      public static void main(String[] args) {
                          Dictionary dict = new Dictionary();
                          dict.addWord("Abode", "Home");
                          dict.addWord("Abode", "House");
                          dict.addWord("Car", "Automobile");
                          dict.addWord("Car", "Vehicle");
                          System.out.println(dict);
                      }
                  }
                  Good luck.
                  • 6. Re: How to create a dicitionary using hash map.
                    807599
                    Thanks for the advice, I will try this and get back.
                    • 7. Re: How to create a dicitionary using hash map.
                      807599
                      May I suggest a slightly different approach?
                      Do not create a String ==> String mapping, but a
                      String ==> List<String> mapping. And do NOT place
                      everything inside your main method.
                      Here's a small demo:
                      import java.util.ArrayList;
                      import java.util.Map;
                      import java.util.HashMap;
                      import java.util.List;
                      
                      public class Dictionary {
                      
                      private Map< String, List<String >> dictionary;
                      
                      public Dictionary() {
                      dictionary = new HashMap< String, List<String
                      
                      ();
                      } public void addWord(String base, String meaning) { List<String> allMeanings = dictionary.get(base); if(allMeanings == null) { // if null, there is no entry mapped to 'base' yet: create a new one List<String> newMeanings = new ArrayList<String>(); newMeanings.add(meaning); dictionary.put(base, newMeanings); } else { allMeanings.add(meaning); } } public List<String> search(String base) { // your code here return null; } // ... other methods ... public String toString() { StringBuilder strb = new StringBuilder(); for(Map.Entry< String, List<String >> e : dictionary.entrySet()) { strb.append(e.getKey()); strb.append(" : "); strb.append(e.getValue()); strb.append('\n'); } return strb.toString(); } public static void main(String[] args) { Dictionary dict = new Dictionary(); dict.addWord("Abode", "Home"); dict.addWord("Abode", "House"); dict.addWord("Car", "Automobile"); dict.addWord("Car", "Vehicle"); System.out.println(dict); }
                      Good luck.
                      Hi,

                      I tried debugging the code which you gave, but i couldnot understabd the flow properly, I am not good at debugging.

                      and,I am not to good at creating methods inside a class, if I want to create a method inside a class what should i concentrate on how to create that..

                      Can you tell me how to implement , that it should search the word.
                      >  public List<String> search(String base) {
                      
                      // your code here
                      return null;
                      }
                      and

                      how should i add other methods say, i want to check if a word is there in the dicionary or not.

                      to check this do i have to create a method or directly can i check this in the main.

                      Message was edited by:
                      New_to_Java
                      • 8. Re: How to create a dicitionary using hash map.
                        807599
                        Thanks anyways ,,, i got it i just added this in that method
                          
                        public List<String> search(String base)
                            {
                                System.out.println("\nMeaning of the Word Specified is: ");
                                System.out.printf("%s:\t%s\n", base, dictionary.get(base));
                                return null;
                            }
                        Now, I am able to find the meaning of the word specified.

                        Thanks a lot,

                        Message was edited by:
                        New_to_Java
                        • 9. Re: How to create a dicitionary using hash map.
                          800282
                          and,I am not to good at creating methods inside a class, if I
                          want to create a method inside a class what should i concentrate
                          on how to create that..
                          By looking at how I implemented the addWord(...) method.
                          Or, if it's still unclear, by doing a tutorial on how to define methods:
                          http://java.sun.com/docs/books/tutorial/java/javaOO/methods.html

                          Can you tell me how to implement , that it should search the word.
                          Look at the Map API docs:
                          http://java.sun.com/j2se/1.4.2/docs/api/java/util/Map.html
                          to see what method you can call on your dictionary variable inside your Dictionary class.

                          to check this do i have to create a method or directly can i check this in
                          the main.
                          No, do it through methods defined in your Dictionary class. Then invoke those methods on the dict variable inside your main-method.
                          • 10. Re: How to create a dicitionary using hash map.
                            807599
                            Thanks a lot, I will go through the tutorial and try going through the API's.
                            • 11. Re: How to create a dicitionary using hash map.
                              800282
                              Thanks anyways ,,, i got it i just added this in that
                              method

                              public List<String> search(String base)
                              {
                              String word = "Car";
                              System.out.println("\nMeaning of the Word
                              Specified is: ");
                              System.out.printf("%s:\t%s\n", word,
                              dictionary.get(word));
                              return null;
                              No, you should return the value associated with base (the key) so you can do something like this in your main method:
                                  public static void main(String[] args) {
                                      Dictionary dict = new Dictionary();
                                      dict.addWord("Abode", "Home");
                                      dict.addWord("Abode", "House");
                                      dict.addWord("Car", "Automobile");
                                      dict.addWord("Car", "Vehicle");
                                      
                                      String lookingFor = "Car";
                                      List<String> meaning = dict.search(lookingFor);
                                      System.out.println(lookingFor+" = "+meaning);
                                  }
                              • 12. Re: How to create a dicitionary using hash map.
                                807599
                                No, you should return the value associated
                                with base (the key) so you can do
                                something like this in your main method:
                                    public static void main(String[] args) {
                                Dictionary dict = new Dictionary();
                                dict.addWord("Abode", "Home");
                                dict.addWord("Abode", "House");
                                dict.addWord("Car", "Automobile");
                                dict.addWord("Car", "Vehicle");
                                
                                String lookingFor = "Car";
                                List<String> meaning = dict.search(lookingFor);
                                System.out.println(lookingFor+" =
                                "+meaning);
                                }
                                Thanks, I tried what you just told, I think the last line is not required:
                                System.out.println(lookingFor+" ="+meaning);
                                Because when i executed with the line above the output was this :

                                The Words in the dictionary and there meanings are as below:
                                Dark : [Lacking brightness]
                                Car : [Automobile, Vehicle]
                                promise : [commitment]
                                Adroit : [Skillful]
                                Abode : [Home, House]


                                Meaning of the Word Specified is:
                                Car:     [Automobile, Vehicle]
                                Car = null

                                So, I removed that line of code and then I checked by searching meaning of two words..

                                i did this at the end of the code:
                                   String lookingFor = "Car";
                                        List<String> meaning = dict.search(lookingFor);
                                        //System.out.println(lookingFor+" = "+meaning);
                                        
                                        /*String lookingFor2 = "Abode";
                                        List<String> meaning2 = dict.search(lookingFor2);*/
                                And the out which i got was this:
                                Output :

                                The Words in the dictionary and there meanings are as below:
                                Dark : [Lacking brightness]
                                Car : [Automobile, Vehicle]
                                promise : [commitment]
                                Adroit : [Skillful]
                                Abode : [Home, House]


                                Meaning of the Word Specified is:
                                Car:     [Automobile, Vehicle]

                                Meaning of the Word Specified is:
                                Abode:     [Home, House]

                                Now, If i have to find a word which is not there in the dictionary what should i do in the search class.
                                • 13. Re: How to create a dicitionary using hash map.
                                  800282
                                  ...
                                  Thanks, I tried what you just told, I think the last
                                  line is not required:

                                  System.out.println(lookingFor+" ="+meaning);

                                  Because when i executed with the line above the
                                  output was this :

                                  The Words in the dictionary and there meanings are as
                                  below:
                                  Dark : [Lacking brightness]
                                  Car : [Automobile, Vehicle]
                                  promise : [commitment]
                                  Adroit : [Skillful]
                                  Abode : [Home, House]


                                  Meaning of the Word Specified is:
                                  Car:     [Automobile, Vehicle]
                                  Car = null

                                  So, I removed that line of code and then I checked by
                                  searching meaning of two words..

                                  i did this at the end of the code:

                                  String lookingFor = "Car";
                                  List<String> meaning = dict.search(lookingFor);
                                  //System.out.println(lookingFor+" = "+meaning);

                                  /*String lookingFor2 = "Abode";
                                  List<String> meaning2 =
                                  dict.search(lookingFor2);*/

                                  And the out which i got was this:
                                  Output :

                                  The Words in the dictionary and there meanings are as
                                  below:
                                  Dark : [Lacking brightness]
                                  Car : [Automobile, Vehicle]
                                  promise : [commitment]
                                  Adroit : [Skillful]
                                  Abode : [Home, House]


                                  Meaning of the Word Specified is:
                                  Car:     [Automobile, Vehicle]

                                  Meaning of the Word Specified is:
                                  Abode:     [Home, House]
                                  That's because you are printing the words inside your search(...) method. You SHOULD return the result instead of printing them in there.
                                  So this
                                    
                                      public List<String> search(String base)
                                      {
                                          System.out.println("\nMeaning of the Word Specified is: ");
                                          System.out.printf("%s:\t%s\n", base, dictionary.get(base));
                                          return null;
                                      }
                                  is incorrect. You should return dictionary.get(base). I only added the return null so that the code would compile and run.

                                  Now, If i have to find a word which is not there in
                                  the dictionary what should i do in the search class.
                                  There is no search class. Try to rephrase that AFTER you have properly implemented the search method.

                                  Good luck.
                                  • 14. Re: How to create a dicitionary using hash map.
                                    807599
                                    That's because you are printing the words inside your
                                    search(...) method. You SHOULD return the result
                                    instead of printing them in there.
                                    So this
                                      
                                    public List<String> search(String base)
                                    {
                                    System.out.println("\nMeaning of the Word
                                    Specified is: ");
                                    System.out.printf("%s:\t%s\n", base,
                                    dictionary.get(base));
                                    return null;
                                    code] is incorrect. You should return
                                    dictionary.get(base). I only added the
                                    return null so that the code would compile and
                                    run.
                                    
                                    
                                    
                                    Now, If i have to find a word which is not there
                                    in
                                    the dictionary what should i do in the search
                                    class. There is no search class. Try to rephrase that AFTER you have properly implemented the search method. Good luck.
                                    Sorry For this:
                                    Now, If i have to find a word which is not there
                                    in
                                    the dictionary what should i do in the search
                                    class.
                                    Its search method, I ll try implementing what you have just suggested.
                                    1 2 Previous Next