Normally I would store URL parameters (GET request parameters) with a HashMap<String,String> but that doesnt account for URLs like test.php?request=id1&request=id2. Is there any data structure implemented in Java that can deal with this (and that I can query a parameter by name)? Alternatively, is there a single class somewhere that I can use (no libraries please)?
You can fake a multimap with something like this:
Map<String, List<String>> multimap = new HashMap<String, List<String>>();
See the 'MultiMap' section on this page for more information:
The Map Interface
If you are talking about using java to write servlet or something, as far as I know, the HttpServletRequest class comes with the parameter as a list, and for each parameter, the value is an array.
HttpServletRequest req=...
Enumeration<String> names=req.getParameterNames(); //
String para=names.getNextElement();//return "request"
String[] values=para.getParameterValues();// Should get["id1","id2"]
Now you certainly can use a list for it, like whiskeyspider suggested.
You can also make a wrapper on it before put into a hashMap,even just to serialize it. then we you need to use it, just unwrap it. But I would suggest use a list all the way, you can always just check the length to know if it is a single parameter or list.
Related
I have some fields in Solr that I'm currently mapping to Java like so:
#Field("file_*") #Dynamic private final Map<String, List<String>> fileDetails;
this matches a number of dynamic fields like:
file_name_1
file_type_1
file_size_1
...
file_name_2
file_type_2
file_size_2
...
...
I then convert that fileDetails map to a FileAttachments object to be used elsewhere in the code.
That is currently resulting in this class having some specific code for converting from how the data is stored in Solr, into the POJO.
Ideally, I'd want to map it more like so:
#Field("file_*") private final FileAttachments attachments;
or even better (but possibly even harder to accomplish):
#Field("file_*") private final List<FileAttachment> attachments;
Then (aside from annotations) the class knows nothing about how the data is stored in Solr.
However, the problem, is, when I try and use a custom convertor for the field (as detailed here), the convertor doesn't get passed a Map<String, List<String>> instance, holding all the fields that matched file_*, instead it simply gets passed a String instance, holding the value of the first field that matched the pattern.
Is there any way to get it to pass the Map<String, List<String>> instance, or any other thoughts as to how I can get this conversion to happen outside of the class?
In an ideal world I'd change the solr document so it was a child object (well, list of them), but that isn't possible as it's part of a legacy system.
Currently am using Hazelcast and persistence database as Hbase,
So far I have 10 maps, for each map am using a map store, So Am using 10 mapstore classes (i.e) In all the 10 classes am implementing the MapStore. It creates a complexity in maintenance.
So What I did is, I kept a generic map store and implemented the same class for all the maps, It has the ability to accept it, To make it clear, I did something like
Map1 - com.test.GenericMapStore
Map2 - com.test.GenericMapStore
Map3 - com.test.GenericMapStore
...
Map10 - com.test.GenericMapStore
It gets mapped as above,
But for the methods in store, storeAll, loadAllKeys, loadAll am able to check the instance of object and find the mapName ---- Not a good way
But for methods like delete, deleteAll, load - I dont have any clue to find the mapName,
Pls tell me like any way to use a singleMapStore for all the maps???
So I need a map store implementation where, for all methods in mapstore, I need the PARAM called mapName to be passed, So In case, If I have common Implementation, I can make use of it just by using MAP NAME param in all the methods,
Example :
Store(String key, Object object, String mapName),
StoreAll(Map, String mapName),
delete(String key, String mapName)
delete(Collections keys, String mapName) ...
If there is a way already available, Pls do let me know...
Thanks hazelcast team,,, You ppl are doing the great job... Much Apprecaiated...
Thanks and Regards,
Harry
You should be able to achieve this with a MapStoreFactory (docs).
The MapStoreFactory is called with the name of the map and you can pass that name into the GenericMapStore.
In you MapStoreFactory :
public MapLoader newMapStore(mapName, props) {
return new GenericMapStore(mapName);
}
then in GenericMapStore you will have the mapName for each operation.
I need something like a property bag to throw key value pairs into.
I want to create list of such objects, initialize them in bean and then use the list to render table in JSF template.
I know how to do it, but I want to avoid creating some special class for that case, like OrderLineItem and using List<OrderLineItem>.
I do not want to define new class.
In PHP I could use StdClass.
StdClass is a sparsely documented class in PHP which has no predefined members.
$object = new StdClass;
$object->foo = 'bar';
But, as far as I know, Primefaces <p:dataTable> list item must be an object with getter.
If I want to reference <h:outputText value="#{item.title}" />, my list object should have getTitle() method.
Is there any walkaround in my case or I really need to define special class to make life easier?
Thanks.
When you want a simple key/value table, then the HashMap might be what you are looking for.
Map<String, String> myMap = new HashMap<>();
myMap.put("foo", "bar");
System.out.println(myMap.get("foo")); // outputs "bar"
This example matches Strings to Strings, but you can use HashMaps to map any type to any other type. You can even create a Map<Object, Object> to create a weakly-typed map which maps anything to anything. But for most use-cases you would rather use a more specialized form.
What you need is a Map.
You can store key-value pairs in it pretty easy:
Map<KeyClass, ValueClass> myMap = new HashMap<KeyClass, ValueClass>();
Use the put method to put data in it. If you use simple String values it will be like this:
myMap.put("key", "value");
I don't know if I understood you well. But I think you mean SelectItem or JsfMap.
I would recommend to use an anonymous class:
return new HashMap<String, String>() {
{
this.put("key", "value");
}
};
Well OK, I got confused. I believe it returns the pointer to the original map?
private HttpServletRequest originalRequest;
Map params = originalRequest.getParameterMap();
params.remove("parameter-to-remove");
params.put("parameter-to-add", "<a value>");
Now are the parameters in the originalRequest going to change after these actions? Or does it just copy the values to params and it doesn't matter what I do with them and nothings going to be changed in originalRequest?
Returned map is immutable Map, that could be the reason why you are not seeing the changes reflected.
As per getParameterMap javadoc
an immutable java.util.Map containing parameter names as keys and parameter values as map values. The keys in the parameter map are of type String. The values in the parameter map are of type String array.
If you would like to set some value to request, you should use setAttribute.
No, you are not allowed to remove or add any request parameter(s) to the request object. They must remain (as they arrived to the server) until the request object goes out of scope (after the end of the request processing cycle).
Logically, if you were allowed to do something like that, then the request object would not represent the original request any more. During the whole request processing cycle, we want to process the request sent by the client, not the one that has been tampered.
The method you should use instead is void setAttribute(java.lang.String name, java.lang.Object o).
I have a object which I want to serialize to JSON. This object is a map with list of a specific objects. This looks similar to it:
Map<String, List<Object>> map = new Map<String, List<Object>>();
I am using FlexJSON. I can work only with flexjson.JSONSerializer. There are my tries:
JSONSerializer jsonSerializer = new JSONSerializer();
// jsonSerializer.include("map");
// jsonSerializer.include("map.myList.object");
jsonSerializer.transform(new MapTransformer(), "map");
jsonSerializer.exclude("*.class");
As you can see I am trying now with Transformer classes but I haven't succeeded. The commented lines will work, as I suppose, if my List<Object> has own name for example myList. But it doesn't haven own name, because it is values of my map.
How can I serialize such object to JSON with FlexJSON?
Quoi is correct, and most likely the answer you want to use. However, just so you can learn more about how it works you could do this:
new JSONSerializer().include("values.values").serialize( myMap );
Maps and Lists are containers and have special expressions for the elements contained within them. When you want to talk about the path of the values of a container you use "values" expression in the path. Map's two special expressions "keys" and "values". Other Collections have "values".
Another option is to use wildcards like so:
new JSONSerializer().include("values.*").serialize( myMap );
You can see wildcards used in Quoi's answer as well to exclude the class property.
Try
String jsonString = new JSONSerializer().exclude("*.class").deepSerialize(map);
deepSerialize method performs a deep serialization of the target instance. It will include all collections, maps, and arrays by default so includes are ignored except if you want to include something being excluded by an annotation.