Win a copy of GANs in ActionE this week in the AI forum
or WebAssembly in Action in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

ArrayList as a HashMap value

 
Greenhorn
Posts: 29
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to create a HashMap which contains movie titles as the keys, and ratings for those movies, in the form of an ArrayList() of ratings for that movie, as the values.  Movies can have multiple ratings from multiple critics (one rating per movie per critic).  At this point I'm only trying to assign ratings to movies; assigning movies/ratings to critics will come later.

When I create the HashMap with the first key/value set, I am unable to figure out how to create the ArrayList for a second movie without altering the first movie's ArrayList.  Everything I've tried either destroys the first movie's ArrayList or alters it.  I was under the impression that once the ArrayList was added to the HashMap it would be "immune" from alteration unless the HashMap key was used to access the ArrayList, but that seems not to be the case.  It seems I need to create a new ArrayList for each HashMap, but, so far, every time I've tried it alters/destroys the existing ArrayList.  I can't figure out how to create a new ArrayList (with a new variable name) which can be repeated "infinitely" as new movies are rated.  Here's one of the many non-working attempts I have made to do this; I've included it to give an idea of what I'm trying to achieve.  

 
Marshal
Posts: 24937
61
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Al Davis wrote:I can't figure out how to create a new ArrayList (with a new variable name) which can be repeated "infinitely" as new movies are rated.



The name of the variables you use are irrelevant to your problem. And yes, you do need to create a new ArrayList for each movie. Here's how to create a new ArrayList:



What you're doing now is reusing the same list for each movie -- which you have already intuited. You need to create the new ArrayList in the else-clause of the code you posted.

I was under the impression that once the ArrayList was added to the HashMap it would be "immune" from alteration unless the HashMap key was used to access the ArrayList, but that seems not to be the case.



You've intuited that correctly, too. As long as you keep a reference to the ArrayList, you can still access it and alter it. You don't even need to keep a reference, for that matter, you can just get the reference out of your map.

It looks like tempList is an instance variable of your class -- you don't need to do that either. Just create a new ArrayList<Rating> inside the addRating method, you don't even need a local variable for that:



 
Paul Clapham
Marshal
Posts: 24937
61
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So like this:

 
Saloon Keeper
Posts: 6743
63
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

 
Al Davis
Greenhorn
Posts: 29
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well that was embarrassingly easy.  Thanks.
 
Marshal
Posts: 67410
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The computeIfAbsent() method was introduced in Java8, 4½ years ago, and may still be new to some people.
Why are you creating a separate List? Why isn't that List of Ratings a field of the Movie class?
 
Sheriff
Posts: 14740
245
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Why are you creating a separate List? Why isn't that List of Ratings a field of the Movie class?


Great questions.

This is one of those times that some tests would come in handy, so you could experiment with writing the code you wish you had. A chain of method calls should raise a Code Smell red flag, especially when it's in a disembodied method that seems to be suffering from Feature Envy like that.
 
Junilu Lacar
Sheriff
Posts: 14740
245
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What class does this method belong to? It's quite possible just from what Campbell and I pointed out that the responsibility you have coded here has been assigned to the wrong class.
 
Hang a left on main. Then read this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!