0

I have an object the represent an entity. By example i have the "user" java object that have the followings field, String name, String first name, String address, boolean deadOrAlive. But now instead of having field i want to put them into a hashmap, so my first reflex was to make it this way :

private HashMap<String, Object>  fieldsHM;

This would means that i have to cast my HM value when i want to use it and i don't want to make that because i need to know the type before i use my value. I want to make something like :

Set<String> keys = fieldsHM.keySet();
for(String key : keys) {
    if(fieldsHM.get(key).isBoolean()) {
        // Do the appropriate things
    } else {
        // Do the thing for this case...
    }
}

I think it's possible in java, would this be good to do it this way ?

EDIT 1: I don't want to make a hashMap because this is not what i need. What i need is a way to browse the fields of the Entity user fields by fields, and depending the type of the field do the appropriate things.

Omegaspard
  • 1,828
  • 2
  • 24
  • 52
  • 1
    `boolean deadOrAlive` - surely `boolean alive` is a better name? It's fairly obvious that a `boolean` can only take two values. `HashMap`? – Boris the Spider Jul 11 '16 at 09:26
  • No ! It was making a reference to the game dead Or Alive so this name will remains. HashMap isn't what i want, i need to have the fields of my Object as the keys of my HM. – Omegaspard Jul 11 '16 at 09:31
  • why do you want to convert an object to a hashmap ? why not accessing the fields right away ? – niceman Jul 11 '16 at 09:33
  • Why? Why would you put _fields_ in a `Map`? 1) Create a `User` , 2) put it into the `Map`. If you want to put arbitrary data then you have to use `Object` and have to cast - this is exactly why it's recommended that you don't. – Boris the Spider Jul 11 '16 at 09:33
  • @Boris the Spider i'm refactoring the code, if this is possible i just want the user to have one fields, the HM that contains all the previous fields. – Omegaspard Jul 11 '16 at 09:36
  • Is that because you want a dynamic class ? but why would you want this ? – niceman Jul 11 '16 at 09:39
  • No i don't want a dynamic class, i just want a way to browse the fields of an Entity field by field. – Omegaspard Jul 11 '16 at 09:42
  • Create an `interface` for the `User`. Implement the `interface` with your `class`. Mark the `class` as `@Deprecated`. Create a new implementation of the `interface`. Refactor away. It seems you have some very tight coupling that you need to abstract away. – Boris the Spider Jul 11 '16 at 09:43
  • 1
    Your main problem now is just to recognize the type of object contained in the Map.Isn't it ? – Radouane ROUFID Jul 11 '16 at 09:44
  • @Radouane ROUFID yes it is ! – Omegaspard Jul 11 '16 at 09:44
  • The only way to acheive this is by using `instanceof`look at my answer at the bottom. You have to keep in mind that it's not the best way to code. Take the application coupling in consideration – Radouane ROUFID Jul 11 '16 at 09:46
  • browse the fields ?!! what's wrong with `user.field1,user.field2,...` ? – niceman Jul 11 '16 at 09:47
  • @niceman "what's wrong with user.field1,user.field2,... ?", firstly the fields are private, so i can't do that and secondly i need the name of the field to do it, that's why i want to map the name of the fields with the value in hashmap instead of having multiple fields in my object – Omegaspard Jul 11 '16 at 09:51
  • `user.getField1(),user.getField2(),...` , if your class isn't going to be dynamic then it's much better to have multiple fields in your object than to maintain a hashmap, what if the name doesn't exist ? what if you put value `2` for boolean field1 ? a hashmap won't prevent any of this by itself – niceman Jul 11 '16 at 09:53
  • The name will have to exist, and it's not the problem here. I may have a lot of fields and if i having to write the user.getFieldX() for every one of them is exactly why i want to use an hashmap. – Omegaspard Jul 11 '16 at 09:58
  • so `user.getFieldsMap().get("fieldx")` is better ? "the name will have to exist" if you made a typo who will warn you ? you'll get a NPE , by having the fields in the object if you made a typo like this `user.getFieldt()` where it should be `user.getField1()` you'll have a compiler error, AFAIK the IDE will warn you before you even compile – niceman Jul 11 '16 at 10:06
  • if you're worried about writing code for 100 field every time, make a method on the class `User` and code once, if you want to do arbitrary operation still you can code it once – niceman Jul 11 '16 at 10:13
  • "so user.getFieldsMap().get("fieldx") is better ?" yes it would be better, i can NPE are not that bad and i can handle typo. Maybe their is a better way, this wha ti'm looking for, But what you propose me "user.getField1(),user.getField2()" is what i'm already doing and i don't want it anymore so... – Omegaspard Jul 11 '16 at 10:22
  • 1
    in that case I suggest you put the fields in the class not in hashmap, then use one of the answers to [this](http://stackoverflow.com/questions/739689/how-to-convert-a-java-object-bean-to-key-value-pairs-and-vice-versa) question, those will mostly use reflection(what Silverclaw said) but they will save you from learning it – niceman Jul 11 '16 at 10:50

2 Answers2

1

I don't want to make a hashMap because this is not what i need. What i need is a way to browse the fields of the Entity user fields by fields, and depending the type of the field do the appropriate things.

I guess that would be a job for Reflection, like User.class.getFields(). It will still be uncomfortable to distinguish between primitive field, but you could use their wrapper classes instead.

But whatever path you choose, I think there would be a better solution if you would state what the actual goal is. Depending on your actual use case, it might make sense to use JSON (maybe with databind) or even a database.

Silverclaw
  • 1,316
  • 2
  • 15
  • 28
0

You could use the heterogeneous container pattern, but I would abandon the map idea and use a proper object.

M. McLean
  • 84
  • 3