If you want to preallocate with known keys then a dictionary is probably not the right data structure. You could use a class with _ _ slots _ _ or a struct.
But usually the people telling you not to prematurely optimize are right. You didn’t really explain why you feel this is bad advice.
If I know what the keys are, I don't really want to load them one at a time, right? This is for situations where I have a specific sized table that I want to use for counting. (I was originally going to use a list but decided that they're not really designed for lookup like dictionaries are). I don't consider it premature optimization because I'm not really optimizing anything, just applying what I know about the problem.
But based on what you’re describing a collection makes more sense. That being said they’re all right that this is sweating the small stuff. For one, using Python and getting the count when querying the database is going to be faster than anything Java can do.
3
u/Smallpaul Jan 05 '18
If you want to preallocate with known keys then a dictionary is probably not the right data structure. You could use a class with _ _ slots _ _ or a struct.
But usually the people telling you not to prematurely optimize are right. You didn’t really explain why you feel this is bad advice.