Reputation: 121
I am unable to get around to sum values of an array
from a Firestore document field.
Irrespective of what I do, I continue to get the following error:
java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer
My requirement is as follows:
I have a db Collection -> "ALLUSERS" and the each document id is the phone number of the user.
Under this document, I have Collections "CHEMISTRY" and a document "Summary". In this, I have a number array of Chapters with points.
I am able to get this detail with DocuementSnapshot.get(...the chapter...). However, once I try to sum the values inside the array, I get the above error.
Below is the code where I tried to sum the same with stream.
for (DocumentSnapshot document : task.getResult()) {
String phonenumber = document.getString("phonenumb");
students.add(phonenumber);
chemistry.document(phonenumber)
.get()
.addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
Map<String, Object> map = task.getResult().getData();
for (Map.Entry<String, Object> entry : map.entrySet()) {
if (entry.getKey().contains("chemchaps")) {
List<String> stringList = (List<String>) entry.getValue();
List<Integer> integerList = stringList.stream()
.map(Integer::valueOf).collect(Collectors.toList());
Integer maxValue = Collections.max(integerList);
Toast("Max Value " + integerList + " " + maxValue);
for(int i = 0; i < maxValue; i++){
String chapterNo = "Chapter"+String.valueOf(maxValue);
String chapternumb = "chapter"+String.valueOf(maxValue);
chemistry.document(phonenumber).collection("CHEMISTRY").document("Summary")
.get()
.addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if(task.isSuccessful()){
DocumentSnapshot documentSnapshot = task.getResult();
List<Integer> chaptersum = (List<Integer>) documentSnapshot.get(chapternumb);
long intsum = chaptersum.stream().mapToLong(Integer::longValue).sum();
//chaptersum.stream().reduce(0, (a, b)-> a+b);
//chaptersum.values().stream().mapToInt(Integer::intValue).sum();
Toast("Chapter Nos "+chapterNo + " "+ chaptersum+ " "+ intsum);
}
}
});
}
}
}
Toast("Map data " + map);
}
});
I get the error at this line:
long intsum = chaptersum.stream().mapToLong(Integer::longValue).sum();
Thanks for the help
Upvotes: 0
Views: 563
Reputation: 138824
You are getting the following line of error:
java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer
Because you are added to chapter1
array numbers, which are basically stored as long values and not as integers. So to solve this, please change the following lines of code:
List<Integer> chaptersum = (List<Integer>) documentSnapshot.get(chapternumb);
long intsum = chaptersum.stream().mapToLong(Integer::longValue).sum();
to
List<Long> chaptersum = (List<Long>) documentSnapshot.get(chapternumb);
long intsum = chaptersum.stream().mapToLong(Long::longValue).sum();
See, the list now is of type Long
and we create the sum using Long values.
Upvotes: 1