I have a collection of groups, each group document has a map of admins. I store admins ids as keys of the map and for the values, I use the null type. So, when I want to retrieve this map, what is the java type to use, since null is not type in java, It's more of a value. I mean I can not have a variable of type null in java. So, How should my map look like :
HashMap<String, ?what-type> map = new HashMap();
What is that ?what-type ?
Even if null is a supported data-type in Cloud Firestore, don't store null as values, rather store a boolean value, for example, true. In this way, the type of the object you were looking for is Boolean. So now you can use:
HashMap<String, Boolean> map = new HashMap();
However, a more appropriate solution would be to use:
HashMap<String, Object> map = new HashMap();
As you don't always know the type of the value. So setting that as an Object might be more helpful in this case. Please note, that an object can also have the value of null, so this solution will cover all situations.
Related
In the code below I can get the appropriate data type value.
Map<String, Value> mutationMap = mutation.asMap();
Value locationValue = mutationMap.getOrDefault("location", null);
Long location = locationValue .getInt64();
How to get it within a loop?
new Gson().toJson(mutation.asMap().entrySet().stream().collect(Collectors.toMap(Entry::getKey,e->e.getValue().toString())));
In the above I'm converting everything to string. If the value has data type int, how do I get that?
Value is defined as an Abstract class in Google Cloud Spanner Code
Value.java
It is overriden by Individual types.
So, using
if (value instanceOf INT64) {
return value.getInt64();
}
Would be the way to go.
I understand your concern about iterating over all data types.
You can either do that, or create a map of col name to type, and use that for better lookup.
I would also recommend using Jackson ObjectMapper to convert POJO to JSON, it may save some iterations.
It is not as optimized as "knowing" what you're about to use, but, it will solve your problem
I have a multivalued Hashmap (technically a LinkedHashMap):
private LinkedHashMap<String, ArrayList<BodyPart>> bodyParts = new LinkedHashMap<>();
I want to find the number of values associated with a given key. However, bodyParts.get("sample key") returns null if the key isn't present, whereas I want it to return 0 (as there are zero values associated with that key).
I could shield it in an if statement:
int numberOfValues;
if(bodyParts.containsKey("sample"){
numberOfValues = bodyParts.get("sample").size();
}
but I was wondering if there is an easier/better way to do it? I've read the documentation for computeIfPresent but, truthfully, didn't really understand it.
Use Map.getOrDefault(Object key, V defaultValue).
Returns the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key.
You can use getOrDefault method of Java Map interface.
It allows you to set default value that is to be returned in case value corresponding to key is not found. So in use case mentioned above you can use :
numberOfValues = bodyParts.getOrDefault("sample", new ArrayList<BodyPart>()).size();
How can i add multiple values with the same name in a HttpUrlConnection request.
example:
HashMap<String, String> params = new HashMap<>();
params.put("key[]", value1)
params.put("key[]", value2)
If i try to add multiple values with the same in postman i works fine, the application will send only one values (depends on request property, URLConnection setRequestProperty vs addRequestProperty).
I want to add both values as a parameter with the same name
This is not possible with Maps or HashMaps.
Taken from Oracles documentation on Maps:
http://docs.oracle.com/javase/7/docs/api/java/util/Map.html
An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.
The put command will replace the previous value associated with the given key in the map (you can think of this like an array indexing operation for primitive types).
The Oracle Documentation for put states:
Associates the specified value with the specified key in this map. If
the map previously contained a mapping for the key, the old value is
replaced.
Returns the previous value associated with key, or null if there was
no mapping for key.
This can be found here:
http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html#put%28K,%20V%29
Alternatively You can do this and it will work fine.
You can make a JSONArray like this
JSONArray array = new JSONArray();
array.put("value1");
array.put("value2");
//and then you can send them as parameter like this-
params.put("key", array.toString());
It is not possible with params.put() But it is possible with params.add()
Reference : Difference between RequestParams add() and put() in AndroidAsyncHttp
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");
}
};
Just for experimenting, I added multiple null keys in a Hashmap instance. And it didn't complain. What's the benefit of doing that?
The code is,
Map hmap = new HashMap();
hmap.put("sushil","sushil11" );
hmap.put(null,null);
hmap.put(null,"king");
hmap.put(null,"nagasaki");
hmap.put(null,null);
How many keys are there in the map?
I would guess you haven't added multiple null-keys. You just overwrote the same nullkey multiple times.
A normal hashmap will have unique keys, so you're overwriting the entry for the null key repeatedly. You won't have multiple identical keys (for this you need a MultiMap or similar)
It is used to get switch:case:default behavior.
Example:
Problem Definition: Coffee shop in CS Department building. They provide coffee to CS Student for $1.00, to IT department students $1.25 and others for $1.50.
Then Map will be:
Key -> Value
IT -> 1.25
CS -> 1.00
null -> 1.50
if(map.containsKey(dept))
price = map.get(dept);
else
price = map.get(null);
P.S. - I am not "Department-ist" if that's a word. :)
There's an API call for this:
size: Returns the number of key-value mappings in this map.
hmap.size();
As noted you're just overwriting the key/value pair with a new value.
The question has asked about having multiple keys in HashMap which is not possible.
If you pass null again and again the old value is replaced only.
Refer:
http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/HashMap.java#HashMap.putForNullKey%28java.lang.Object%29
HashMap allows multiple null values but only one null key.
If you write multiple null keys like below, then null will be overrides and you'll get the final overridden result. i.e "world"
Map<String, String> valueMap = new HashMap<>();
valueMap.put(null, "hello");
valueMap.put(null, "world");
System.out.println(valueMap.get(null));
Output:
"world"