Reflection vs. MapListString etc. etc

1

Random question: I have a JSON of a lot of kilobytes, already almost reaching the mega.

I'm about to make it Java-istic and I'm in a list map: Map<List<Map<List, OMG>>>> data

Or a full-fledged class:

Departamento { Empleado { Direccion { String calle; } } } Etc. etc

The question is that you have to access some properties dynamically depending on the user interaction (example there is a combo with departments and if you choose HR, that is if it is Conta etc.)

With the map I do

Map<List<Map, ETC>>> depa = data.get( depaSeleccionadoPorElUsuarioEnUnCombo );

With the class I would have to use reflection ... right? true?

for ( Field f : data.getClass().fields ) { if ( f.getName() == depaSeleccionaroporElusuarioEnUNCombo ) { String value = f.invoke( o, whacha whacha); }

I can use annotations but the subject is the same

Question: With everything and everything, it is significantly faster / slower to use a specific class and then reflection to access the content OR it is better to use the map of lists of maps of lists of lists of maps of etc. etc.

I'm starting to do a proof of concept for this, but if someone has already gone through there and has some information to advise.

    
asked by OscarRyz 25.04.2017 в 23:42
source

2 answers

0

It looks very ugly to use reflection in that case. It is not so much about execution efficiency but rather about code quality (and therefore clarity, maintenance, debugging, testing, etc.).

If your supermapa has a fixed structure, known in advance, then in general it is convenient to do a class. More so if the alternative is a bunch of generic collections (maps and lists) very nested. But I do not understand why you would need to use reflection. If you know by which field you are looking for the data (or, in the structure map of lists, in depth level) then you should not need to use reflection.

    
answered by 26.04.2017 в 00:21
0

Before you use reflection (such as for clarity and performance) it would make more sense to use AttributeSet - basically a'Map 'of attributes.

If you find it necessary to restrict the keys you can use constants in your implementation and implement a validation of the arguments in getAttribute(Object o) methods and in the methods in which you add attributes, possibly by throwing a customized exception.

    
answered by 26.04.2017 в 00:44