this is an example for a question asked here :
getValue(String.class) vs getValue().toString();
I had stored into my firebase table a child "Vyear" as Integer and it looked as such
and I used to retrieve it as below, and the app was always crashing ;
child4.child("Vyear").getValue(String.class);
after reading the question, and the answers , I solved my problem and now I am using;
child4.child("Vyear").getValue().toString();
Your first snippet:
child4.child("Vyear").getValue(String.class);
This retrieves the String value from Vyear. Since Vyear stores a numeric value, it means that the snippet returns null.
The second snippet:
child4.child("Vyear").getValue().toString();
This returns the value from Vyear in its underlying type. So if Vyear has a value, this returns a number (a long or Long). Calling toString() on that, gives you the number as a string.
Note that in case Vyear does not exist, the second snippet will thow a NullPointerException on the call to toString().
Related
I'm getting my head around Streams API.
What is happening with the 2 in the first line? What data type is it treated as? Why doesn't this print true?
System.out.println(Stream.of("hi", "there",2).anyMatch(i->i=="2"));
The second part of this question is why doesn't the below code compile (2 is not in quotes)?
System.out.println(Stream.of("hi", "there",2).anyMatch(i->i==2));
In the first snippet, you are creating a Stream of Objects. The 2 element is an Integer, so comparing it to the String "2" returns false.
In the second snippet, you can't compare an arbitrary Object to the int 2, since there is no conversion from Object to 2.
For the first snippet to return true, you have to change the last element of the Stream to a String (and also use equals instead of == in order not to rely on the String pool):
System.out.println(Stream.of("hi", "there", "2").anyMatch(i->i.equals("2")));
The second snippet can be fixed by using equals instead of ==, since equals exists for any Object:
System.out.println(Stream.of("hi", "there",2).anyMatch(i->i.equals(2)));
You should instead make use of:
System.out.println(Stream.of("hi", "there",2).anyMatch(i->i.equals(2)));
The reason for that is the comparison within the anyMatch you're doing is for i which is an Object(from the stream) and is incompatible with an int.
Also, note that the first part compiles successfully since you are comparing an integer(object) with an object string "2" in there and hence returns false.
I am using trove library to create hash maps
http://trove.starlight-systems.com/
The class I am using is TObjectIntMap in which I had to use the function get.
The issue is that get returns 0 if two cases
1- If the value of the specified key is zero
2- If the key does not exist
For example in the following code
TObjectIntMap<String> featuresMap = new TObjectIntHashMap<String>();
if(String.valueOf(featuresMap.get("B")) == null)
System.out.println("NULL");
else
System.out.println("NotNull");
System.out.println(featuresMap.get("B"));
The program will print the following
1- NotNull: because it gets zero. Although the key "B" has not been set
2- Zero: The return of featuresMap.get("B") is zero instead of null.
I have checked their documentation in the link below and it was a mistake that they solved. So get actually return zero instead of null because int cannot be null.
https://bitbucket.org/robeden/trove/issue/43/incorrect-javadoc-for-tobjectintmapget
Now my question is: How to differentiate between a zero and Null in this case. Is their any way around to address this issue.
Try their containsKey method. If the value comes back 0, use that method to check if the map contains the key - if it does, then the key's value really is 0. If it doesn't, then the key is not set.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
my case:
I try to get a value from my MySQL database. This data is defined as a VARCHAR.
This is done in java with a prepared statement and resultset.
String add1 =rs.getString("include");
according to this website table 5.1 states "Can always be converted to these Java types".
therefore a VARCHAR should be able to be converted in a java.lang.string.
When i check the data type in java it indeed gives java.lang.string. So for so good.
My issue is when i try to compare this obtained "String" value to a in java defined String value they don't seem to be equal.
Let's give a simple example.
I get the (VARCHAR) value "1" from the database and store in in String add1
I want to check if this value is indeed "1" and therefore I use the following statement
if(add1=="1")
{
/* do something */
}
Only the values are , according to java, not the same. So I was curious about why this is.
I started searching why those two "Strings" are not equal.
First of I checked the length of both strings. Both came out to be 1 in length as expected.
Afterwards I checked the value of both. To see if both values where indeed the same i wanted to check their hexadecimal representance.
Both came out with the value 0x31. Still as expected.
But then why isn't my if statement accepted by java if both seem to represent the same string-hex-bin-you name it value.
In short: Why is my VARCHAR "String" obtained from a MySQL databse in java not the same as a String defined in java.
Compare values, not memory references:
if(add1.equals("1"))
{
/* do something */
}
Please see this post best answer.
You must always compare strings with the .equals method in your case it will be
add1.equals("1")
"==" or equality operator in Java is a binary operator It is good for compairing primitives like boolean, int, float "==" works fine but when it comes to compare objects it creates confusion with equals method in Java. "==" compare two objects based on memory reference. so "==" operator will return true only if two object reference it is comparing represent exactly same object otherwise "==" will return false.
String comparison is a common scenario of using both == and equals method. Since java.lang.String class override equals method, It return true if two String object contains same content but == will only return true if two references are pointing to same object.
Read more: http://javarevisited.blogspot.com/2012/12/difference-between-equals-method-and-equality-operator-java.html#ixzz3PdLbOre0
You need to compare the Strings with .equals() method. because == checks for object reference.
What is the difference between == vs equals() in Java?
What is the difference between getString() and optString() in JSON?
As Diego mentions, it's a good idea to check the documentation (this link is now out of date - good thing we have the Wayback Machine!) before posting a question here, but now that you have:
The difference is that optString returns the empty string ("") if the key you specify doesn't exist. getString on the other hand throws a JSONException. Use getString if it's an error for the data to be missing, or optString if you're not sure if it will be there.
Edit: Full description from the documentation:
Get an optional string associated with a key. It returns an empty string if there is no such key. If the value is not a string and is not null, then it is converted to a string.
If you want to avoid NullPointerException you better make use of optString()
If you are fetching the data from JSON at any time, you might have null data for a particular Key value, at that time instead of implementing Null conditions, better make use of this optimized method optString("<keyname>")
public java.lang.String optString(int index)
Get the optional string value associated with an index. It returns an empty string if there is no value at that index. If the value is not a string and is not null, then it is coverted to a string.
Parameters:
index - The index must be between 0 and length() - 1.
Returns:
A String value.
1) getString (String name):- This method Returns the String value mapped by name if it exists, coercing it if necessary, or throws JSONException if no such mapping exists.
2)optString (String name):- This method Returns the String value mapped by name if it exists, coercing it if necessary, or the empty string ("") if no such mapping exists.
optString() is used to overcome NullPointerException, which we get while using getString() when the required key doesn't exists in json it basically replaces with the default value.
example let the input Json be
{
"name":"abhi",
"country":"india"
}
now in java when you execute
String city = json.getString("city");
it will throw a NullPointerException.
by using optString(String key, String default) we can overcome the above problem.
String city= json.optString("city","default");
System.out.println(city);
Output: default
Hi In My application I need to calcluate the md5 Hash Value for a string value... for that I have called DigestUtils.md5Hex(String string) method... But it is not produced same result always for a same string... The result is inconsistent. In that md5Hex() method internally md5(string) called. That method returns the different byteArray values for a same string. I am not able to get what exactly happening? Please clarify the issue...
As you can guess, this is not possible. You are probably passing different strings, but you don't know it. They may differ by a whitespace, for example, or some invisible character.