GeeCON Prague 2014*
The moose likes Java in General and the fly likes HashMapping Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "HashMapping" Watch "HashMapping" New topic
Author

HashMapping

Eli Andrus
Greenhorn

Joined: Oct 17, 2011
Posts: 20
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

Joined: Apr 06, 2010
Posts: 4396
    
    8

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
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

In particular the word "link" needs an explanation.
Eli Andrus
Greenhorn

Joined: Oct 17, 2011
Posts: 20
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

Joined: Sep 08, 2007
Posts: 3071
    
  33

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.


Mohamed Sanaulla | My Blog
Eli Andrus
Greenhorn

Joined: Oct 17, 2011
Posts: 20
No, but I see now, that helps me a lot.
Eli Andrus
Greenhorn

Joined: Oct 17, 2011
Posts: 20
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
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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

Joined: Oct 17, 2011
Posts: 20
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
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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

Joined: Oct 17, 2011
Posts: 20
How so?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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

Joined: Mar 24, 2011
Posts: 84

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

Joined: Oct 17, 2011
Posts: 20
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

Joined: Sep 08, 2007
Posts: 3071
    
  33

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

Joined: Aug 05, 2005
Posts: 3599
    
  15
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.


Joanne
Eli Andrus
Greenhorn

Joined: Oct 17, 2011
Posts: 20
why do you say that?
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
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

Joined: Oct 17, 2011
Posts: 20
No, because it is putting item in and then it is trying to add item in again (line 11) right?
Eli Andrus
Greenhorn

Joined: Oct 17, 2011
Posts: 20

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

Joined: Oct 17, 2011
Posts: 20
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

Joined: Mar 12, 2011
Posts: 808
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
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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

Joined: Oct 17, 2011
Posts: 20


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

Joined: Apr 06, 2010
Posts: 4396
    
    8

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

Joined: Oct 17, 2011
Posts: 20
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

Joined: Oct 13, 2005
Posts: 39044
    
  23
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

    Joined: Mar 24, 2011
    Posts: 84

    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

    Joined: Aug 05, 2005
    Posts: 3599
        
      15
    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

    Joined: Jul 18, 2011
    Posts: 287
    Joanne Neal
    Rancher

    Joined: Aug 05, 2005
    Posts: 3599
        
      15
    And that could be simplified to
    Eli Andrus
    Greenhorn

    Joined: Oct 17, 2011
    Posts: 20
    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

    Joined: Sep 08, 2007
    Posts: 3071
        
      33

    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

    Joined: Oct 17, 2011
    Posts: 20
    Here is the overall just so you know the big idea.

    Mohamed Sanaulla
    Saloon Keeper

    Joined: Sep 08, 2007
    Posts: 3071
        
      33

    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

    Joined: Oct 17, 2011
    Posts: 20
    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

    Joined: Oct 17, 2011
    Posts: 20
    actually all i had to do was add ArrayList instead of List right?
    Eli Andrus
    Greenhorn

    Joined: Oct 17, 2011
    Posts: 20
    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?
     
    GeeCON Prague 2014
     
    subject: HashMapping