繁体   English   中英

对 Firestore 字段的数组值求和

[英]Sum Array values of a Firestore field

我无法从 Firestore 文档字段中对array值进行求和。

无论我做什么,我都会继续收到以下错误:

java.lang.ClassCastException: java.lang.Long 不能转换为 java.lang.Integer

我的要求如下:

我有一个 db Collection -> "ALLUSERS" 并且每个文档 ID 是用户的电话号码。

在这个文件下,我有集合“化学”和一个文件“摘要”。 在这方面,我有许多带有点的章节数组。

在此处输入图片说明

我可以通过DocuementSnapshot.get(...thechapter...)获得这个细节。 但是,一旦我尝试对数组中的值求和,就会出现上述错误。

下面是我试图用流求和的代码。

                         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);
                                            }
                                        });

我在这一行收到错误:

long intsum = chaptersum.stream().mapToLong(Integer::longValue).sum();

谢谢您的帮助

您收到以下错误行:

java.lang.ClassCastException: java.lang.Long 不能转换为 java.lang.Integer

因为你被添加到chapter1阵列数字,这基本上存储为长值,而不是整数。 因此,要解决此问题,请更改以下代码行:

List<Integer> chaptersum = (List<Integer>) documentSnapshot.get(chapternumb);                                                                            
long intsum = chaptersum.stream().mapToLong(Integer::longValue).sum();

List<Long> chaptersum = (List<Long>) documentSnapshot.get(chapternumb);                                                                            
long intsum = chaptersum.stream().mapToLong(Long::longValue).sum();

看,列表现在是Long类型,我们使用 Long 值创建总和。

暂无
暂无

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

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