2

I have a string, refid = "asdkjdndkdkkd", and another refid = "jsdjnfrejnfkn". How can I use the refid as part of the name of a list?

For example, something like:

list<String> list_+refid
tshepang
  • 12,111
  • 21
  • 91
  • 136
  • You want to dynamically generate the reference name??? – TheLostMind Nov 28 '13 at 04:24
  • @TheLostMind seems like it. That's a good question - not sure why anyone would downvote/close-vote it. He might be able to do it using reflection but I'm not sure I know the answer to this one. – Nir Alfasi Nov 28 '13 at 04:25
  • okey just i dont get your real Problem can u just tell me more about what you want please so we can help u/.:) – Kishan Bheemajiyani Nov 28 '13 at 04:27
  • Achieving kind of similar behavior will be to use a hashMap and map the string `list_refid` to that list. – Nir Alfasi Nov 28 '13 at 04:28
  • 2
    The answer to this problem in every language is to use a map. – Ry- Nov 28 '13 at 04:29
  • @alfasin - ya.. i always suggest people to give reasons for downvoting... BTW, How can you do it using reflection?... Names of variables should be decided during compile time. Now, he will have to find a way of getting the value during compilation... – TheLostMind Nov 28 '13 at 04:30
  • @TheLostMind it was a bad guess - but I think that [*this*](http://stackoverflow.com/a/16485489/1057429) is the right direction – Nir Alfasi Nov 28 '13 at 04:39
  • @alfasin - The link explains a lot..Thanks.. But then again, this guy wants to create field names dynamically... Will involve a lot of headbanging i guess... – TheLostMind Nov 28 '13 at 04:45
  • 1
    @TheLostMind if that's what he wants to do - who are we to stop him ? :) – Nir Alfasi Nov 28 '13 at 04:46
  • @user2981726 - If You dont need to use the same class ( the class which you are running currently), then you can dynamically create a new class(.java file) with whatever fields you want (using java code). Compile it, load it and use it. – TheLostMind Nov 28 '13 at 05:49
  • Is the value of refid known at compile time or only known at runtime? – Werner Henze Nov 28 '13 at 08:47

2 Answers2

3

The only way to achieve this is by writing code that emits new bytecode at run-time, or other metaprogramming approaches, as explained in this thread.

Unless you have a very good reason to do it, you might want to stick to a HashMap (or other Map):

HashMap<String, List<String>> map = new HashMap<String, List<String>>();

// ...

List<String> list;
if (map.containsKey(refId))
    list = map.get(refId);
else
    map.put(refid, list = new ArrayList<String>());

// work with list here
Community
  • 1
  • 1
Domi
  • 22,151
  • 15
  • 92
  • 122
-1

It is possible to access a certain field by using string concatenation and reflection, but not to create one. However, you typically have multiple fields you want to create like this, so a map mapping from a string to a list seems like what you would use here. Example:

Map<String,List<String>> map = new HashMap<>();
map.put(refid, new ArrayList());
map.get(refid)
flaviut
  • 2,007
  • 3
  • 23
  • 32
  • I am already using a map to this but the problem with this solution is that the contents of the arraylist key on changing i.e i have 1000 refid which i am taking as keys in my map and the corresponding values are array lists.I have to loop the contents of a file and update the contents of the array list with each iteration so i have to give them names so that i can access them in next iterations – user2981726 Nov 28 '13 at 04:45
  • You can access any values in a map in future iterations, try doing map.get(refid), which will return a list, which can be used in the exact same way as `list_+refid` or any other list. Some actual sample code might be helpful here. – flaviut Nov 28 '13 at 04:53
  • Why were you downvoted? :-/ – Boann Nov 28 '13 at 05:01
  • List logcontent=new ArrayList(); HashMap> file_contents = new HashMap>(); try{ BufferedReader br=new BufferedReader(new FileReader("C:\\Users\\vamshir\\Documents\\Logg.txt")); while((entry=br.readLine()) != null) {preRefId=Sample.getPreRefId(entry); if(!file_contents.containsKey(preRefId)) { logcontent.add(entry.toString()); file_contents.put(preRefId,logcontent); //logcontent.clear(); } else {logcontent=file_contents.get(preRefId); logcontent.add(entry); file_contents.put(preRefId,logcontent); //logcontent.clear();} – user2981726 Nov 28 '13 at 05:03
  • 1
    Your code has several issues. The most severe is that you only ever create a single list. Use my code above, which generates a new List whenever there was none for the current refid. – Domi Nov 28 '13 at 05:08
  • you are creating a list with name list but i have to create that for each refid.So the contents of old list get changed. – user2981726 Nov 28 '13 at 05:27
  • @user2981726 Whether you use a map or fields won't make any difference here. Your actual problem as Domi says is that you only create one single list object, which contains all the log entries together. Your entire map has only one list, and it is associated with every map key. Domi's answer shows you how to create the list separately for each map entry. – Boann Nov 28 '13 at 05:27