I'm looking how to handle a Map<String,String> map; to get a key value from it. For example in dozer :
<a map-get-method="get" key="valueFromTheMap">map</a>
Thank you
ManyToOne? https://github.com/jmapper-framework/jmapper-core/wiki/Many-To-One
Related
Is there any Java API for DynamoDB to convert from Item to Map<String, AttributeValue> without implementing it on my own?
EDIT
item.asMap() will return Map<String, Object>, not Map<String, AttributeValue>. Just wondering is there any direct API for this?
Yeah, but I've managed to find it:
// Item item
InternalUtils.toAttributeValues(item)
However, above API is deprecated in newer DynamoDB library which basically delegates the call to ItemUtils which is not deprecated fortunately. So I ended up using this:
ItemUtils.toAttributeValues(item)
Hope this will help others in future!
You can use the method asMap:
Returns all attributes of the current item as a map.
Updated answer:
To get a Map<String, AttributeValue> you can use ItemUtils.toAttributeValue:
Converts an Item into the low-level representation; or null if the input is null.
as follow
Map<String, AttributeValue> map = ItemUtils.toAttributeValue(item);
I am currently programming a bukkit plugin that stores a bunch of information about the player in a YAML configuration file. Now I want the plugin to read the YAML file when the server starts up and then add on the that information. I have my loader, but I cant use it because my plugin uses a custom map. Here is the code for the map:
Map<Integer, Map<String, Object>>
And here is the code to get the information from the file:
info = (Map<Integer, Map<String, Object>>) ticket.getConfigurationSection("tickets");
But when I try to run the plugin with that line of code i get this error:
Caused by: java.lang.ClassCastException: org.bukkit.configuration.MemorySection cannot be cast to java.util.Map
Full code is posted here: http://pastebin.com/Xgu8hwM0
The solution to this is not using a custom map. You already get a MemorySection from your configuration.
Work with that. Instead of casting you should use the method: getValues(boolean) which returns a Map<String, Object> containing all the relevant information and is specified by the Interface ConfigurationSection.
ticket.getConfigurationSection("tickets").getValues();
See also the relevant excerpt at bukkit's Configuration API Reference:
The getValues method will return the values in the
ConfigurationSection as a map, it takes a boolean which controls if
the nested maps will be returned in the map.
Yes I solved this. I HAD to use the Map<String, Object> but it worked because the way I had it(Map<Integer, Map<String, Object>>) that is the second part!
How to retrieve values from the following hashmap in velocity template? Please help..
LinkedHashMap<String, LinkedHashMap<Integer, Object>> hashmap = new LinkedHashMap<String, LinkedHashMap<Integer,Object>>();
First, add the hashmap to your backing Java class (reference here).
context.put("myhashmap", hashmap);
Then you can reference anywhere in your Velocity template, e.g:
<span>$myhashmap.get("foo").get(1).toString()</span>
This worked for me:
$!myhashmap.get($!foo).toString();
Note: foo is a variable in my case.
How would you go about serialising a Map using simple XML so that it looks something like:
<elem foo="value">key</elem>
Instead of the normal
<elem foo="key">value</elem>
(The map is one to many, and since this will be edited by humans, I wanted it to be clearer.)
[EDIT]: Doesn't Fix.
Have you tried something like:
#ElementMap(entry="property", value="value", attribute=true, inline=true)
private Map<String, String> map;
or some combination, i.e. to use the other attributes of the #ElementMap annotation too?
I have:
public enum MyEnum{
One, Two, Three
}
From controller, I put in the model:
HashMap<MyEnum, Long> map = new HashMap<MyEnum, Long>();
map.put(MyEnum.One, 1L);
mav.addObject( "map", map);
How do I in my JSTL access the object in the map for key enum MyEnum.One, in a neat way?
${map['One']} //does not seem to work...
nor does
${map[MyEnum.One]}
It's not exactly true that you can't do it, but the solution isn't completely straight forward. The issue is EL is not converting the string you pass in as the map key to the corresponding enum for you, so putting ${map['One']} does not use the enum constant MyEnum.One in the map lookup.
I ran into the same issue and didn't want to revert to going with a String keyed map, so the challenge then was in JSTL how to get the actual enum reference to use in the map lookup.
What is required is to get the Enum constants into the scope of the JSP so that you can then use the actual Enum itself as the key. To do this, in the controller you do something like this:
for (MyEnum e : MyEnum.values()) {
request.putAttribute(e.toString(), e);
}
What you've done here is added variables into the scope named as the string representation of the enum. (you could of course avoid naming issues by prepending the e.toSring() with some value)
Now, when you do the following
${map[ONE]}
You will be using the actual enum constant as the key and will therefore get back the proper corresponding value from the map. (notice there are no quotes around ONE, that is because you are referencing the request attribute ONE in this case, that was added above)
You can't. Your best bet is to change your map to use enum.name() as key:
HashMap<String, Long> map = new HashMap<String, Long>();
map.put(MyEnum.One.name, 1L);
map.addObject( "map", map);
Your first approach would work then:
${map['One']} // works now
Or you can write a custom EL function to do the above for you if you can't / don't want to change the map.
I usually use this solution:
<%#page import="my.package.MyEnum"%>
<c:set var="One" value="<%=MyEnum.One %>" />
<c:set var="MyEnum_values" value="${map[One]}" />
First, I import the enum. Then, I save the enum value I want into JSTL variable. Then I can access the map with this variable as the key.
${map[MyEnum.One]}
It works for me. But you have to write the complete name of your class: my.package.MyEnum or to import MyEnum class:
<%#page import="my.package.MyEnum"%>