Can I store Int value inside Firebase database [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have store resource values in variables, and now I want to store this variable inside the Firebase database but the data type is Integer (int) and I don't know can we store an integer in Firebase or not.
I want to store below two variables of Integer type inside Firebase Database:
int textColor = ResourcesCompat.getColor(getResources(),R.color.black, null);
int textSize = (int) (getResources().getDimension(R.dimen.text10sp));

Can I store Int value inside Firebase database?
Sure you can, in a property of type Long, which is a supported data-type. An int value will always fit in a Long.

You can directly cast your int as Integer
Integer.valueOf(textColor)
Integer.valueOf(textSize)

Related

Java: Sum of values inside a Collection of DTO [closed]

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 4 years ago.
Improve this question
Calculate the sum of amountField in MyDTO, Java 8
private Double getSum(List<MyDTO> myDTOList) {
return myDTOList.stream().map(MyDTO::getAmount).reduce(0d, Double::sum);
}
You can use Stream.mapToDouble to convert it into a DoubleStream and then use DoubleStream.sum:
return myDTOList.stream().mapToDouble(MyDTO::getAmount).sum();
To mention, the above shall work for amount being of integer type as well, while the code shared by you and this shall still work fine if the type of amount is already double.
Or as pointed out in comments by you, if your DTO object can be null, you can filter out those values using:
return myDTOList.stream().filter(Objects::nonNull).mapToDouble(MyDTO::getAmount).sum();

How to check a Float variable is empty or null IN JAVA [closed]

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 5 years ago.
Improve this question
I have a Float variable which is initialised from a function returning the Float value, and there can be chance to return null in some cases. So I need to check the value in the variable is null or empty. If the value is empty I need to initialise variable wb1 with value 1. I am using Java Spring. This is the code currently I amusing,
Float wb1 = !StringUtils.isEmpty(getArea())?getArea() : 1.0f;
What is the correct way to check and initilize with value 1.0f
First: Float is an object and float is the primitive type.
null check is used for objects. For primitives you should use the default values check. For float the default value is 0.0f.
Assume you mean Float, the box of float, you can use
Optional.ofNullable(wbCalculation(area.getArea(), CitySizeByArea.SIZE1.value, area.getRate1(), task.getTaskId())).orElse(0f)

How to get a random object from firebase database in android? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am working on an Android app, where I need to get a random object from firebase from a child? How to do it in java?
This is how I would do it, assuming I had a child with n objects:
If I didn't know how the value of n, I would first do this in my listener to get the total number of objects:
long n = dataSnapshot.getChildrenCount();
Then I would generate a random integer, i, between 0 and n. If you don't know how to do this, Google it.
Finally, I would get the ith item from the child:
final ArrayList<MyObject> objects = new ArrayList<>();
for (DataSnapshot child : children) {
MyObject object = child.getValue(MyObject.class);
objects.add(object);
}
MyObject objectToUse = objects.get(i);
"MyObject" should obviously be whatever class you're using.
Am I missing something? Is there a better way to do this? I'm pretty new to Android and very new to Firebase so take what I have to say with a big grain of salt haha.

How to judge isInstance(ArrayList<>) in java [closed]

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
How to judge whether a variable is an ArrayList in Java? Like the isInstance function.
Just use instanceof, blow is very simple example how to detect a variable is ArrayList:
List<String> strings = new ArrayList<>();
if (strings instanceof ArrayList) {
System.out.println("ArrayList");
}
a.getClass().getName() - will give you the type of the value of the variable, but not the variable's type.
boolean b = a instanceof String - will give you whether or not it is an instance of a specificclass.
I took this information from: How know a variable type in java?
This can happen. I'm trying to parse a String into an int and I'd like to know if my Integer.parseInt(s.substring(a, b) is kicking out an int or garbage before I try to sum it up.

compare input strings and store in database if that is equal [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I want to compare input string get from html using bean with certain strings that I will use constant in my code and if that input strings are equal with the specified string i want to store that user as a admin in database where I have to update row in database table.thank you
private final String User_role ="admin";
if(User_role.eguals(UserRole.getUser())){
PreparedStatement st = session.connection().prepareStatement("{update tblUser user set user.user_role = ? where (Condition)}");
st.setString(1,User_role );
st.execute();
}

Categories

Resources