Using a single hashmap against multiple variables

Let's say we have a CompHash class with 30 different variables as follows:

public class CompHash{ private String abc1; private int sdf2; : : private float sgh30; } 

and a similar class:

  public class CompHash{ private HashMap diffVariables; } 

In a situation where the number of variables that I will need varies from 1 to 30, which of the two will be better?

+4
source share
4 answers

In general, you should always prefer a strongly typed CompHash (first). It is not only safer, but also much faster.

If you have a requirement to store an arbitrary number of variables, a HashMap might be a good choice. But remember that you are sacrificing type safety without much benefit - the HashMap will probably still take up more memory, and not just one object with a lot of null s.

The only permissible use of HashMap is when you need to store arbitrary pairs (it is not clear whether the variables / key names are constant in your case) key -> value. But in this scenario, I would recommend wrapping the primitives with a class hierarchy that has a common ancestor, and use the Visitor pattern to avoid denger downgrades and ugly instanceof .

By the way, what problem do you actually solve? The data structure you need is a bit exotic ...

+7
source

You can also consider List<T> for storing a list of items, as you are not sure if there can be 1 to 30 variables. you can use a dynamic array, i.e.: list.

Saving an object in a list is not a good idea, casting is required to use it, and if you do not know the type that you saved, you will get a cast exception.

For example: if you want to keep the name of the students who show the class today, you can use the list. since you do not know how many students will appear, you use the reason for its dynamic nature.

 List<String> 

it will be your code.

I would not recommend using HashMap fields to store objects that will store data, i.e. properties.

0
source

If your list of property variables is a known object, since it will have the largest X-value, and I know what type of value each of them will have, then, of course, the first approach will make the most sense. In this situation, as @Tomas noted in his answer, it is preferable. You do not have to worry about type safety.

However, if your object has a set of properties, and then should have a list of other things of a different type and a different number, then a HashMap may make sense. There are some things to keep in mind.

First indicate the types as well as possible. For instance:

 HashMap<String,Object> myHash = new HashMap<String, Object>(); 

If you created this way, your hashmap keys should ALWAYS be a string. If you are dealing with things that are called dynamically, this can be a good way. You will notice that I am only specifying Object. Because of this, you can save any type of object, and if you want to save primitives, you need to do it with them as Integer, Double, String, ect., Not int, double, string.

Then the stressful part of structuring, like this one, is that you will need to display and check the types and be prepared to solve it. When you pull out pieces of information, you need to do something like this:

 if myHash.get(key) instanceof Integer Integer myVal = (Integer) myHash.get(key); else if myHash.get(key) instanceof Double Double myVal = (Double) myHash.get(key); 

Then you will need to check the type and throw when outputting the values, and then send them to new methods / logic based not only on the name of the property, but also on the type of the object. There are some really useful things you can do this way ... but it can be hard to do without painting yourself in an overly complex corner. Especially if the Java reflection api can be used to dynamically enhance properties (but not without its own headaches).

0
source

I like the first option with strongly typed variables. BUT, I will strongly consider grouping 30 attributes into at least 4-5 classes, if possible.

0
source

All Articles