Results 1 to 4 of 4
  1. #1
    jamster0099 is offline Member
    Join Date
    Mar 2014
    Posts
    2
    Rep Power
    0

    Default Using objects in HashMaps

    Hi there,

    This might be a basic question but hopefully someone can be of some help.

    I am currently trying to implement a hashmap from a class i currently have that is called 'Paper'

    My paper class consists of:

    String Title;
    String[] author;

    What I am wondering, is if there is anyway I can call these seperate attributes to be both the key and the value of the hashmap. Currently I cannot see a way to call individual values, only the class itself.
    Again this is due to my limited programming knowledge that I am asking this question..

    Many thanks for any help.
    Jamie.

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: Using objects in HashMaps

    Why do you mean by 'call attributes' ? Post some examples of what you've been trying so far.

    Also in stead of posting to ask how to do a specific technical solution, you might in stead want to post what problem you are trying to solve. Then perhaps someone can make a suggestion for a solution that will make more sense to you.

    Finally: this is a beginner question, you admit that yourself. So why post it in the "advanced java" forum?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,528
    Blog Entries
    7
    Rep Power
    20

    Default Re: Using objects in HashMaps

    Quote Originally Posted by gimbal2 View Post
    Finally: this is a beginner question, you admit that yourself. So why post it in the "advanced java" forum?
    Problem solved ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    typedef is offline Member
    Join Date
    Feb 2014
    Posts
    52
    Rep Power
    0

    Default Re: Using objects in HashMaps

    To my understanding you are asking a question about how you can implement your instance variables in a hash map. What is confusing about our question is how you want to implement these variables in the map.
    It seems you are asking how can I implement an array of these variables in a hash map such that I can make either variable a key or value. In this case you must use the put method and insert both variables as a key and value. Small example, let us assume that x is an array of titles and that y is an array of authors:
    Java Code:
    HashMap<String, String> bookHash = new HashMap<>();
    
    for (int i = 0; i < x.length; i++)        // This assumes that the array size of titles == array size of authors.
    {
        bookHash.put(x[i], y[i]);
        bookHash.put(y[i], x[i]);
    }
    On the other hand, if your asking about putting a pair of instance variables in a structure to access another pair/single instance variable you must wrap the pair in a data structure (such as an ArrayList) before putting it in the map. In this case make x or y an ArrayList before inserting. For example, I can make a pair of title and author to access a book. This would something like this:
    Java Code:
    String[] titles = {"aa", "bb", "cc", "dd"};
    String[] authors = {"a", "b", "c", "d"};
    
    String[] books = {"aaa", "bbb", "ccc", "ddd"};
    
    HashMap<ArrayList<String>, String> bookHash = new HashMap<>();
    
    for (int i = 0; i < titles.length; i++)
    {
    	ArrayList<String> al = new ArrayList<>();
    	al.add(titles[i]);
    	al.add(authors[i]);
    	bookHash.put(al, books[i]);
    }
    
    ArrayList<String> sample = new ArrayList<String>();
    sample.add("bb");
    sample.add("b");
    System.out.println(bookHash.get(sample));

Similar Threads

  1. Arrays and HashMaps
    By kiloran in forum New To Java
    Replies: 4
    Last Post: 01-06-2013, 02:51 PM
  2. Maps and HashMaps
    By moonah in forum Advanced Java
    Replies: 1
    Last Post: 01-26-2012, 03:22 PM
  3. hashmaps
    By santa in forum New To Java
    Replies: 6
    Last Post: 05-03-2011, 11:07 PM
  4. Concerning HashMaps
    By hjensen in forum New To Java
    Replies: 7
    Last Post: 10-18-2010, 03:36 PM
  5. HashMaps get key for a specific value
    By andre1011 in forum Advanced Java
    Replies: 2
    Last Post: 03-11-2009, 02:30 AM

Posting Permissions

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