Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I need to allow the user to target a variable using a string name. Example,
int number = 5;
String variableTarget = "number";
I need to target the int, number, and then change its value in later code. Any way?
I cannot think of any way to do it. There seems to be alot of class targeting, but I already have that.
I don't think that there is a way to do this in java. But, to have such a functionality, you can use a map to assign the value to the number key in the Map :
HashMap <String, Integer> myMap = new HashMap<>();
myMap.put("number", 5);
and you can alter the value later using the same put statment :
myMap.put("number", myMap.get("number")+1);
You can use Java Reflections, or create another structure like a HashMap answered by #DodgyCodeException
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
This is the main method from where I am trying to pass values to the skill class which accepts array. I do not want to give employee e1 all the skills but just s4, how can I do that so my code becomes logical and correct
You have different way to achieve this.
Taking into consideration the piece of code you showed above, the easiest way to do that which doesn't imply to add any additional method to the Employee class, is to create a new array of Skill while invoking setEmployeesSkills on e1, so your code will appear more or less like this:
e1.setEmployeesSkills(new Skill[]{s4});
Hope that is what you are looking for, otherwise please share more info and I will try to help you further.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
object Test extends App {
val i: Iterable[(String, Long)] = List(("a", 1), ("b", 2))
val sortedMap: SortedMap[String, Long] = i.toList.sortBy(_._2)
}
I don't want to convert Iterable to List/Array etc since it's coming form a jdbc query.
You can't do that. SortedMap sorts by keys, not values.
If you want it sorted by value, you gotta use ListMap, and can't avoid converting to List:
ListMap(i.toList.sortBy(-_._2):_*)
There isn't really too much wrong with converting to list, since you are loading the whole thing in memory anyway. This is faster too, than building a tree one element at a time.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
We can put single null key in hashMap. but in linkedHashMap we can't pull null key. what is the reason behind this.
You can put a null key in a LinkedHashMap. Example:
Map<String, String> m = new LinkedHashMap<> ();
m.put(null, "a");
System.out.println(m.size());
System.out.println(m.get(null));
prints 1 and a.
Following documentation
This class provides all of the optional Map operations, and permits
null elements.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Is it possible to search without iterator without using map.
Map<String,Short> map = new HashMap<String,Short>();
map.put("String2", (short)2);
map.put("String1", (short)1);
map.put("String3", (short)4);
I am looking for a way to get the value based on the key (Return 2 for value String2). Is Map the right one to use in this scenario.
Thanks !
Returning values based on a key is precisely what maps are for.
To retrieve your value you can simply do:
short returnVal = map.get("String2");
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I know my question is so silly and we found a lot's of resources on internet, but as a beginner i hesitate between two declaration to fill a HashMap in Java:
I have a MySQL request looking like this :
select coordinates,x, y,z from log
i want to fill a Map or a HashMap( whatever) with the values of x,y and z for each coordinates point.
that's what i did :
List<Map<String, Object>> res = h.select("select coordinates,x, y,z from log");
i never used Maps in Java, so i don't know if it's the good way to get an organized map.
So now i hesitate between the declaration above and this one:
Map<String,Map<String, Object>>
I really don't know with one i have to use and how i can read the map after .
Thank You
in case you need this data structure afterward for getting (X,Y) coordinated by Z then the second choice would give you quick access.
Map = Map.get(key);
Otherwise you will have to loop throw the list.
If this is not the case a simple ArrayList should be fine.
In any case see more details here:
http://docs.oracle.com/javase/tutorial/collections/implementations/index.html