Results 1 to 11 of 11
  1. #1
    lochdara is offline Member
    Join Date
    Mar 2012
    Posts
    7
    Rep Power
    0

    Default hashsets in hash maps

    Hi

    Im new to java and am having a problem with hashmaps.
    i have a simple hashset.

    Java Code:
    public class MyFavouriteClasses
    {
      Set<String> classes;
      
    public MyFavouriteClasses()
        {
            
        }
        
       public void populate()
        
        {
        HashSet<String> classes = new HashSet<String>();
           
        classes.add("ArrayList");
        classes.add("Boolean");
        classes.add("Button");
        classes.add("Console");
        
        
        System.out.println("The hash set has been sucessfully populated with the following:");
       System.out.println(classes);
     
        }

    I want to use this set as keys in a hash map called Sortingandsearching()

    the values are the associated packages in the java library.
    for now i manually entered the details in. How can get iteratate from one class into the other and assign values to each key.



    Java Code:
     public class SortingAndSearching
        {
            private MyFavouriteClasses MyFavouriteClasses;
            Map <String, String> mappings;  
            
    
    
         /**
         * create an instance of myfavourite class
         */
        
        public SortingAndSearching()
        {
            MyFavouriteClasses  = new MyFavouriteClasses(); //creates an instance of MyFavouriteClasses class
           
            
               MyFavouriteClasses.populate(); // calls the populate method within MyFavouriteClasses class
              
                HashMap<String, String> mappings = new HashMap<String,String>();
                
        }
      
    
         public void createMappings()
        {
         
          mappings = new HashMap<String, String>();
           
            
           
            
            mappings.put("ArrayList","Util");
            mappings.put("Boolean","lang");
            mappings.put("Button","awt");
            mappings.put("Console","io");
            
          
        System.out.println(mappings);
        }
    Last edited by lochdara; 10-28-2012 at 09:09 PM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: hashsets in hash maps

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    lochdara is offline Member
    Join Date
    Mar 2012
    Posts
    7
    Rep Power
    0

    Default Re: hashsets in hash maps

    post updated.. Yeah there related. but not yet working.

    when i compiled
    Java Code:
       MyFavouriteClasses class = new MyFavouriteClasses();
    it said it was not a statement.
    Can you suggest any further info.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: hashsets in hash maps

    You can't use a keyword as a variable identifier.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    lochdara is offline Member
    Join Date
    Mar 2012
    Posts
    7
    Rep Power
    0

    Default Re: hashsets in hash maps

    any suggesgtions how i handle my code?

    As far i can have looked i havent seen any question similair on any forum or video

  6. #6
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,828
    Rep Power
    19

    Default Re: hashsets in hash maps

    For starters your 'classes' Set isn't being populated.
    At least the one that is part of the class.
    The local variable in 'populate()' is, however.

    I'm not entirely clear what you are trying to achieve, but if you want keys in that HashMap that come from the Set then iterate over the Set and 'put()' them in, though I have no idea what values you will be using.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    lochdara is offline Member
    Join Date
    Mar 2012
    Posts
    7
    Rep Power
    0

    Default Re: hashsets in hash maps

    maybe my code is confusing,

    my aim is to create the hashset called classes. To my knowledge i have done this in the method populate() as per my assignment.
    the official question is

    "Have a private method named “createMappings” that will:
    o Create a HashMap using the class names from the HashSet as “keys” and their corresponding package names as “values”.
     This can be done in a number of ways so try to be as efficient as possible when designing this method."

    so then i created a hashmap.

    at this stage i have tried so many things my code is everywhere. But on the upside i have came across so many other tutorials that has showed me lots of new stuff. Research is good, but draws you away from the problem.
    but so far i just manually done this as how i learned.
    Im taking from the question it should be entered from the Set.

    Do you think i took the question up entirely wrong.

  8. #8
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,828
    Rep Power
    19

    Default Re: hashsets in hash maps

    Java Code:
    HashSet<String> classes = new HashSet<String>();
    No you haven't.
    That line there is declaring a local variable called 'classes' that you then initialise.
    That is not the class attribute 'classes', which is still null once the populate() method exits.

    Removing the HashSet<String> from the front will mean you are actually initialising the correct object.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    lochdara is offline Member
    Join Date
    Mar 2012
    Posts
    7
    Rep Power
    0

    Default Re: hashsets in hash maps

    i also realised i called my method

    public void populate()

    Am i correct that this should be public HashSet<String> populate()

    that will return my classes;

    so if this is now correct its on to my next stage..

    getting that info into the hashmap.

    When i was trying during the week i was using the statment

    Java Code:
     HashMap<HashSet<String>, String>> mappings = new HashMap<HashSet<String>, String>>();
    but i couldnt bring the set in..
    Was i going in the right direction here.

    its not included in the code in my original post.

    if i create a Hashset within the hashmap and use these as the keys would they always be inorder
    i.e would "ArrayList","Util" always be along side.

    Thanks for your input so far

  10. #10
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,828
    Rep Power
    19

    Default Re: hashsets in hash maps

    To me populate() doesn't need to return anything.
    Just add a getClasses method that returns the Set.

    Your 'mappings' is a HashMap<String, String>, the first being the class name and the second being the package name.
    So:
    Java Code:
    populate the Set.
    For each class in the Set
       Figure out the package name (I have no idea what is expected for this bit).
       Add an entry into the Map.
    That is (as far as I can make out) your logic.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    lochdara is offline Member
    Join Date
    Mar 2012
    Posts
    7
    Rep Power
    0

    Default Re: hashsets in hash maps

    Thanks..

    At least I am half way correct.. The rest i will continue to pull my hair out, but i have a direction to travel in.

Similar Threads

  1. Java HashSets
    By sameerr_007 in forum New To Java
    Replies: 5
    Last Post: 03-09-2011, 11:30 AM
  2. Java HashSets
    By sameerr_007 in forum Advanced Java
    Replies: 1
    Last Post: 03-09-2011, 10:13 AM
  3. help using hashsets?
    By yasmin k in forum New To Java
    Replies: 1
    Last Post: 04-17-2010, 10:09 PM
  4. HashSets
    By DavidG24 in forum New To Java
    Replies: 5
    Last Post: 04-19-2009, 06:02 PM
  5. how does the remove method work for sets and hashsets
    By haridharna in forum Advanced Java
    Replies: 4
    Last Post: 08-06-2007, 12:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •