• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HashMapping

 
Eli Andrus
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys, do you know(well obviously you do) how to link HashMaps with ArrayLists. instead of linking them with other HashMaps.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you need to explain a bit more about what you mean by that: maybe it's just me, but it really isn't obvious.
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In particular the word "link" needs an explanation.
 
Eli Andrus
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok sorry about that, I have to make some sort of mail system, and the keys to the HashMap should be the names of the recipients, and each value should be an ArrayList containing all the Mail items stored for that recipient. Does this help?
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try using: Hashmap<String, ArrayList<Mail>>. Which would imply that your hash map has string keys, but its values are ArrayList of something.
 
Eli Andrus
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, but I see now, that helps me a lot.
 
Eli Andrus
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But how would I actually link the two? When I tried what you told me to do, I just got an error.
Here is the code I am working with. There are other classes, but are irrelevant to this question.




 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, first of all, if you get an error message then the first thing you should do is to read it. It isn't just a rude noise, it's relevant information about the problem you have. So start with that.

And I don't see in that code anything like Mohamed's suggestion.

(By the way I put your code into the "Code" tags so it wasn't totally unreadable any more. There's a "Code" button at the top of the posting box for your future use.)

Edit: Also, looking at that now-readable code, I don't see why you think you need a HashMap of any kind.
 
Eli Andrus
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I have to use a hash map, it is an assignment. I have to link the HashMap to the references (people) and the values to array list.
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So the keys of the hashmap have to be String, and the values have to be List<MailItem>? Then your first step is to define a HashMap like that. The one you have in your code is declared in the right place (at least I think it is, I have no idea what you are supposed to do with this map) but it isn't defined in the right way.
 
Eli Andrus
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How so?
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would point you to Mohamed Sanaulla's suggestion, but I already did that. So let me ask you: if you declare a Map<K, V>, which of those two type parameters is the type of the key and which is the type of the value?

And once you answer that, compare your answer to what you put in your code.
 
Rajagopal Mani
Ranch Hand
Posts: 84
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
MailItem is added to whom mail? if possible to post with credentials, below code can be considered. Not able to visualize the requirements & scenarios that you working towards.

 
Eli Andrus
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Key would be type persons(string) of course, and V(value), each value would be any array that contains all of the mail items stored for this person(referring back to person of key) I guess Im just wondering how I should form this, Im new to coding, and just need the type of structure I guess.
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eli Andrus wrote:The Key would be type persons(string) of course, and V(value), each value would be any array that contains all of the mail items stored for this person(referring back to person of key) I guess Im just wondering how I should form this, Im new to coding, and just need the type of structure I guess.

let me give you a head start


Update: I think I just repeated what Rajagopal Mani had posted before. May be both these posts should help you?
Also try to code a simple example, understand how and why it works. Then you can improve the code to suit your requirement.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajagopal Mani wrote:MailItem is added to whom mail? if possible to post with credentials, below code can be considered. Not able to visualize the requirements & scenarios that you working towards.


Line 9 is wrong.
 
Eli Andrus
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why do you say that?
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at what your are trying to put into the map at line 9. Compare that to what happens on line 11. Will this work?
 
Eli Andrus
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, because it is putting item in and then it is trying to add item in again (line 11) right?
 
Eli Andrus
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Although this is my code so far, the thing is it complaining about it mail.Items.put(who,item), it says cannot find symbol. method put(java.lang.String.mailItem)
Why does it do this?

 
Eli Andrus
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
also I'm sorry, it won't let me put the code into a box( I don't know how to do that yet)
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your Map could talk, it would say: Hi! I accept Strings as keys, and map them to objects that are type List<MailItem>. When you call my get method, and pass me a String, I agree to return a List<MailItem> object (if I can find it). When you call my put method, you agree to give me a String object and a List<MailItem> object.

So: does line 9 adhere to this agreement?
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eli Andrus wrote:also I'm sorry, it won't let me put the code into a box( I don't know how to do that yet)


I fixed the code tags in your last post. It really isn't hard to do it. Let me tell you two ways:

(1) Paste your code in the box where you're typing your reply. Select it by dragging your mouse over it. Click the "Code" button at the top.

(2) Click the "Code" button at the top. You'll see something like "[ code=java][/code]" Put the cursor between the opening tag and the closing tag, i.e. between "]" and "[". Paste your code.
 
Eli Andrus
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


It is acting weird with the mailItems.put. wats wrong with that? it is saying it cannot find the symbol put. This is my code for the HashMap and ArrayList mapping it to the keys and values.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're trying to add a MailItem to the map as the value - but the map is declared as containing ArrayList<MailItem>. So instead you need to add an ArrayList containing the MailItem.
 
Eli Andrus
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So how is that going to look different? Im sorry I don't get this very much, so if you can explain it as much as possible that'd be awesome. It means allot.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50277
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are better declaring the Map as Map<Foo, List<Bar>>.
If you have a List as a “V”, you need to find the List. Use this pseudocode:
  • If the Map doesn’t contain the key,
  • Create a new List, retaining a reference to it.
  • Put the List into the Map as a value.
  • Add the item to the list.
  • The second and third stages above can be reversed.
  • else (ie the Map does contain the key),
  • get the List
  • Add the item to that List.
  • finished
  •  
    Rajagopal Mani
    Ranch Hand
    Posts: 84
    Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Line9 - If map not contains key, new key added with corresponding MailItem in the form of List.
    Line11 - If map contains the key, obtain existing List<MailItem> refrence to add another MailItem to list for the corresponding key/user
     
    Joanne Neal
    Rancher
    Posts: 3742
    16
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Rajagopal Mani wrote:Line9 - If map not contains key, new key added with corresponding MailItem in the form of List.

    That's what it should do. It's not what it is actually doing.
     
    Harsha Smith
    Ranch Hand
    Posts: 287
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
     
    Joanne Neal
    Rancher
    Posts: 3742
    16
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    And that could be simplified to
     
    Eli Andrus
    Greenhorn
    Posts: 20
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    With your code joanne, it says that

    is an incompatible type. it found a java List, but was expecting a ArrayList.
     
    Mohamed Sanaulla
    Saloon Keeper
    Posts: 3159
    33
    Google App Engine Java Ruby
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Eli Andrus wrote:With your code joanne, it says that

    is an incompatible type. it found a java List, but was expecting a ArrayList.

    Can you please post the code you are trying? Its kind of getting disconnected.
     
    Eli Andrus
    Greenhorn
    Posts: 20
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Here is the overall just so you know the big idea.

     
    Mohamed Sanaulla
    Saloon Keeper
    Posts: 3159
    33
    Google App Engine Java Ruby
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    this one private HashMap<String, List<MailItem>> mailItems; has to be private HashMap<String, ArrayList<MailItem>> mailItems;

    because when you do- map.get(someKey) it returns a List item and in turn you are trying to assign a List instance to a ArrayList reference which is a violation of rule.

    "Subclass instances can be assigned to Super class reference, but the other way is not possible"
     
    Eli Andrus
    Greenhorn
    Posts: 20
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    when I fixed that, line 26 in my code says ;incompatible types found java.util.HashMap<java.lang.String.java.util.List<MailItem>> but expected java.util.HashMap<java.lang.String.java.util.ArrayList<mailItem>>
     
    Eli Andrus
    Greenhorn
    Posts: 20
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    actually all i had to do was add ArrayList instead of List right?
     
    Eli Andrus
    Greenhorn
    Posts: 20
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Another question though it refers to another classes problem.
    here is the code for the other class;


    the code that I had to change in the class we have been working with was initially this;




    Although it is having a problem with line 10 in the first part of the code(from the other class)
    it says that post in mail server cannot be applied to (MailItem)
    -do you know what this means? or what I should do more like it?
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic