1

I have an ArrayList of a java object with name say, "mylist". I am getting the name of the list(mylist) from database which comes as a String. I need to find the size of this list in JAVA and need to iterate over it.

Is there a way to do this?

Edit: To be more clear here, I have a class "Candidate"

class Candidate {

    String firstName;
    String lastName;
    List<Education>educationRecords;
    .....
}

class Education {

    String school;
    String degree;
    .....
}

I need to populate Candidate object from a JSONObject whose structure is very different from Candidate object(so can't use Gson or jackson).

The simple fields like firstName, lastName, school and degree are stored in the database.

I fetch a Listfields and iterate over them to set the Candidate object. For setting firstName, lastName, I am using SpEL.

But, for fields like school and degree, it becomes complex as I would need to know the size of education records in JSONObject.

So, what I thought was while iterating over all fields if school field is present in Education class(using reflection), I would need to check if educationRecords is a of List type in Candidate class and then by some logic X(taking educationRecords as variable name) iterate over it.

It is the logic "X" I am missing.

Jason C
  • 38,729
  • 14
  • 126
  • 182
shawGate
  • 19
  • 4
  • 1. What you're asking is pretty unclear, you need to include more specific details about your situation. 2. My first thought is you're probably not taking the best approach, fundamentally, anyways, this is an odd thing to have to do. You might want to sit back and rethink your general design. 3. If you're locked in to doing it the way you are currently doing it, check out the [official tutorial on reflection](https://docs.oracle.com/javase/tutorial/reflect/). It has its limitations but may be helpful for you. You could also hack it with a switch statement. – Jason C Oct 10 '16 at 15:17
  • I'm not sure I understand - you have an `ArrayList`, and you want to find its size, i.e. the number of elements? Why don't you use `mylist.size()`? – Tomáš M. Oct 10 '16 at 15:18
  • @TomášM. The question is as asked: The OP has ArrayList variables with names. The OP gets a string from a database. The OP then wants to access the ArrayList whose variable name equals the retrieved string. But there is not enough context to answer properly. Perhaps a Map, or a big switch block, or reflection, or a redesign. – Jason C Oct 10 '16 at 15:20
  • oh, that certainly seems like an unorthodox design choice – Tomáš M. Oct 10 '16 at 15:23
  • Related, possibly duplicate, will let you guys decide, I already cast my vote: http://stackoverflow.com/questions/13298823/get-variable-by-name-from-a-string and http://stackoverflow.com/questions/2127197/java-how-can-i-access-a-classs-field-by-a-name-stored-in-a-variable – Jason C Oct 10 '16 at 15:30

2 Answers2

1

I don't quite understand the use case here. However, my understanding is that you have an ArrayList<Object> mylist and you need to find the size of it based on a string retrieved from DB.

You can store the list in a cache, where the key is the name of the list and value is the actual object. You can use redis, eh-cache or memcache (Or anything similar) to achieve the same.

So when you retrieve the name from DB, get the corresponding object from the cache and do size() on the object after type casting it. If your are storing the list type instead of object type in the cache, then the last part is not needed (typecasting).

Jeevan Varughese
  • 2,159
  • 2
  • 15
  • 20
  • 1
    This is probably a better approach than reflection, bean properties, etc. since it sort of implicitly sanitizes the string from the db. It's also a lot more straightforward. – Jason C Oct 10 '16 at 15:27
0

You can use a map:

Map<String, List> listMap = new Hashmap<>();

Each time you create a list - add it to your map with a key that is a name of your list. Once you get a name you can easily check if such list exists in your map, get it, check its size and iterate through it... Be careful that once you are done with your list you should remove it from your map as well as otherwise you will keep them around and it will eat your memory up.

Michael Gantman
  • 7,315
  • 2
  • 19
  • 36