0

Is nesting collections in Java something that I should be doing?

I'm currently working on a project where I want to have a bunch of hashmaps that would contain a String key and an arrayList value. That way when I create and add an object of another class to the collection, it would be able to use some piece of information that if it matched up with one of the keys of one of the hashmaps it would then be deposited in the associated arrayList value. That way the list can later on be accessed through the correct key for a specific hashmap.

Is this a good idea? Or is it too convoluted and if so is there a better way to do this?

1 Answer 1

1

There are times to nest, for sure. But in the humble opinion of this seasoned dev, you shouldn't do it unless you have a good reason. All too often you would be much better off with some class that represents the inner collection.

So if you find yourself with a Map<String,List<Foo>> ask yourself what that List<Foo really represents. If it's Map<String,List<Student>> then maybe you need Map<String, Roster> or Map<String, Team>. I find this yields faster time to market and fewer bugs. The fact you're asking the question means you think there's a chance that might be true too.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the response! Yeah I wasn't sure if it was really the smartest thing to do. The idea of this project (its for a class) is to create a sort of videogame ItemInventory, where a game dev could create something like a gun through the use of a Gun class and then can store it in a collection that corresponds with the type (say a collection of gun objects that have all been assigned the type sniper rifle). I'm not familiar with Roster or Team. but maybe I can take a look and see if they are suitable?
@dynothunder You missed the point of this wise Answer. Roster is an example of a class a programmer like you might create if the app was tracking students in a school. Team is an example where you might be tracking players in a sport. If you have characters owning some guns and such, rather than program for a list or set of guns, perhaps you should create a class WeaponsCache that manages acquiring, owning, and discarding weapons for that player.
@BasilBourque Ah I see. I did indeed consider the briefly consider making my own class to accomplish this, so I'm gonna go back and look more into this, maybe look at Roster and Team to help me gain a better understanding of how to go about things here. I most likely will still end up nesting collections for the project at the moment as my due date is approaching very fast, but after that, will come back to it to create a working WeaponsCache class or something of the sort. Thanks for the advice, it's greatly appreciated!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.