• Post Reply Bookmark Topic Watch Topic
  • New Topic

Confused by using Map type instance variable to make methods?  RSS feed

 
Justin Robbins
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all!

I want to be able to create two methods using a Map instance variable. It's my understanding that the Map interface takes in pairs, one which is a Key and the other a corresponding Value. So we have it set up like Map<K,V> and this goes for all the Map derivatives like HashMap<K,V> and linkedHashMap<K,V> and they are always stored in those pairs, and K and V can be any object combination. My question is how do we use it when it's an instance variable, if that even makes sense to have a map as a type. What I have done here is set up a class like so:



What I want to do is be able to use that instance variable in some way and create two methods, one that would add a game to a kid's after school games, and one which would remove a game from a kids after school games. Unsure how this would be done? Trying to get experience with using Maps and Map type instance variables.

Here in the instance variable the first String would represent the kids name, and the ArrayList would compose all the games the kid is taking. Is this possible to do?

I want to be able to do two checks in the add method. One to check whether the kid is in the system already or not. And two, if the kid is already in the system then add the game to the kids pre-existing list.


Grateful for any help.
Thank you
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'd need a method something like this

I'm thinking that you won't want to remove just any-old game from the list. Perhaps you want a HashSet instead of a List.
 
Justin Robbins
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:You'd need a method something like this

I'm thinking that you won't want to remove just any-old game from the list. Perhaps you want a HashSet instead of a List.



Thank you.
I am trying to understand it. I was thinking something of making something on the lines of:



would this work? am I checking all the corner cases?
>
 
Justin Robbins
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:You'd need a method something like this

I'm thinking that you won't want to remove just any-old game from the list. Perhaps you want a HashSet instead of a List.



Thank you.
I am trying to understand it. I was thinking something of making something on the lines of:



would this work? am I checking all the corner cases?
>
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:You'd need a method something like this

I'm thinking that you won't want to remove just any-old game from the list. Perhaps you want a HashSet instead of a List.


Oops. That was suppose to be the remove() method.
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the relationship between "course" and "game" ?
 
Justin Robbins
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:What is the relationship between "course" and "game" ?


Nothing haha made a mistake on that. The second version is the correct version. Confused if it's right or would work.
 
Justin Robbins
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:



Would this work?



I want to be able to add to that gamesTaking ArrayList, have that as it is and then do a check, and if that check passes
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Justin Robbins wrote:

You already have gamesTaking, which is a list of games, just add your new games to that list. However, if gamesTaking is null, then you have to create a new list, add  the new game, and put it into the map.
 
Justin Robbins
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:
You already have gamesTaking, which is a list of games, just add your new games to that list. However, if gamesTaking is null, then you have to create a new list, add  the new game, and put it into the map.


Thank you
I don't understand. Why if it's null would I have to create a new list and add a new game then put that into a map?
Could you please explain what this would look like. I know how to create an arraylist but not sure how to add to that list. And how to put into into a map. Really confused here.
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Justin Robbins wrote:
Carey Brown wrote:
You already have gamesTaking, which is a list of games, just add your new games to that list. However, if gamesTaking is null, then you have to create a new list, add  the new game, and put it into the map.


Thank you
I don't understand. Why if it's null would I have to create a new list and add a new game then put that into a map?
Could you please explain what this would look like. I know how to create an arraylist but not sure how to add to that list. And how to put into into a map. Really confused here.

To get the list for a kid: list = map.get(kid); // may return null if no kid in map
To create a new list: list = new ArrayList<String>(); // only necessary if above is null
To put into map: map.put(kid,list); // only necessary if above is null
To add game to list: list.add(game);
 
Justin Robbins
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:
Justin Robbins wrote:
Carey Brown wrote:
You already have gamesTaking, which is a list of games, just add your new games to that list. However, if gamesTaking is null, then you have to create a new list, add  the new game, and put it into the map.


Thank you
I don't understand. Why if it's null would I have to create a new list and add a new game then put that into a map?
Could you please explain what this would look like. I know how to create an arraylist but not sure how to add to that list. And how to put into into a map. Really confused here.

To get the list for a kid: list = map.get(kid); // may return null if no kid in map
To create a new list: list = new ArrayList<String>(); // only necessary if above is null
To put into map: map.put(kid,list); // only necessary if above is null
To add game to list: list.add(game);



For that last section I thought it might look like this
     ArrayList<String> list = new ArrayList<>();

        if (gamesTaking != null) {
            games.put(kid, gameVarList);
        } else {
        list = map.get(kid);
        map.put(kid,list);
        list.add(game);
        }


I am getting an error under the map. Not sure why.



 
Justin Robbins
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually might it be:


 
Justin Robbins
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Should be this, been editing keep switching up variables, this is better:




 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You only need one of "gameTaking" or "list", not both.
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:To get the list for a kid: list = map.get(kid); // may return null if no kid in map
To create a new list: list = new ArrayList<String>(); // only necessary if above is null
To put into map: map.put(kid,list); // only necessary if above is null
To add game to list: list.add(game);

You are making this too complicated. I suggest that you start your add() method over. The above lines of code, in order, is all you need with the exception of adding in an 'if' statement (no 'else').
 
Justin Robbins
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:
Carey Brown wrote:To get the list for a kid: list = map.get(kid); // may return null if no kid in map
To create a new list: list = new ArrayList<String>(); // only necessary if above is null
To put into map: map.put(kid,list); // only necessary if above is null
To add game to list: list.add(game);

You are making this too complicated. I suggest that you start your add() method over. The above lines of code, in order, is all you need with the exception of adding in an 'if' statement (no 'else').


Am confused by what list = map.get(kid); means. Is this to say I create a list? when I write list = map.get(kid); I get errors.

I can create a list like: ArrayList<String> list = new ArrayList<String>();
now sure how to go about map.put(kid,list);

Am I suppose to create a separate map somewhere?

 
Justin Robbins
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Justin Robbins wrote:
Carey Brown wrote:
Carey Brown wrote:To get the list for a kid: list = map.get(kid); // may return null if no kid in map
To create a new list: list = new ArrayList<String>(); // only necessary if above is null
To put into map: map.put(kid,list); // only necessary if above is null
To add game to list: list.add(game);

You are making this too complicated. I suggest that you start your add() method over. The above lines of code, in order, is all you need with the exception of adding in an 'if' statement (no 'else').


Am confused by what list = map.get(kid); means. Is this to say I create a list? when I write list = map.get(kid); I get errors.

I can create a list like: ArrayList<String> list = new ArrayList<String>();
now sure how to go about map.put(kid,list);

Am I suppose to create a separate map somewhere?




Do you mean the code should look like this:

 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Justin Robbins wrote:Do you mean the code should look like this:
Almost there. You have


What you're missing is the case where there IS a kid with an already existing list and how do you add a new course to that list.
All you need to do is move line 7 of your code slightly and you're done.
I'm hoping that this is beginning to make some sense.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Justin Robbins wrote:. . .  and K and V can be any object combination. . . .
Yes, but ... If you make the K a mutable type, and it changes its state in such a way that the hash code changes, you will most probably never find it again with get(XXX); A K shou‍ld be immutable; a String is eminently suitable to be a K. That problem does not apply to the V unless you have a bidirectional HashMap. Types used as Ks need correctly‑overridden hashCode and equals methods.

I presume you have seen the Java™ Tutorials about Maps.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!