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
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 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
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 7 years ago.
Improve this question
Can someone help me or guide me with resources that would help me write this code in a simple way? We are internally storing some values which have a different representation across the company. I was wondering if there is a way in JAVA where I store this value ahead of time with its actual standard values which everyone knows.
For instance,
{
ab -> apple
bc -> ball
cd -> cat
}
In my code, I retrieve ab from the database. Instead of checking each time what ab represent, I can on the fly pass the standard value to some other service.
I'm looking to implement something pass apple instead of ab without the use of if and else statement. I know that I will have to store the standard value and point to the internal value which would be a one time thing of writing.
Something in the direction of an array
instead at index[0] -> 1
I say index[ab] -> apple
So something like: let's say I retrieved a from the database, in the code, I just simply call a convert.(a) which return apple instead..
I can't think of a way ... need some help
If I understood you correctly you want to retreive values from a database and transform these values into something you "hard code". This could be achieved be a "HashMap". This Map is able to store key-value-pairs.
This Map has basically this structure: HashMap<Key, Value>.
Where Key is anything you choose to identify a Value.
In your case it would look like this (considering you want to store String-String-pairs):
Map<String, String> map = new HashMap<String, String>();
map.put("a","apple");
map.put("b","ball");
map.put("c","cat");
This basically says e.g. "take an "a" and associate it with "apple". Those two values do not need to be String. They can be anything: Integer, Float, Object, MyClass,....
Now if you want to get the transformation for your a you simply do:
map.get("a");
This call will return "apple" since you associated apple with it earlier.
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");