简体   繁体   English

对 Firestore 字段的数组值求和

[英]Sum Array values of a Firestore field

I am unable to get around to sum values of an array from a Firestore document field.我无法从 Firestore 文档字段中对array值进行求和。

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 java.lang.ClassCastException: java.lang.Long 不能转换为 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.我有一个 db Collection -> "ALLUSERS" 并且每个文档 ID 是用户的电话号码。

Under this document, I have Collections "CHEMISTRY" and a document "Summary".在这个文件下,我有集合“化学”和一个文件“摘要”。 In this, I have a number array of Chapters with points.在这方面,我有许多带有点的章节数组。

enter image description here在此处输入图片说明

I am able to get this detail with DocuementSnapshot.get(...the chapter...).我可以通过DocuementSnapshot.get(...thechapter...)获得这个细节。 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谢谢您的帮助

You are getting the following line of error:您收到以下错误行:

java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer java.lang.ClassCastException: java.lang.Long 不能转换为 java.lang.Integer

Because you are added to chapter1 array numbers, which are basically stored as long values and not as integers.因为你被添加到chapter1阵列数字,这基本上存储为长值,而不是整数。 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.看,列表现在是Long类型,我们使用 Long 值创建总和。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM