【问题标题】:Sum Array values of a Firestore field总和 Firestore 字段的数组值
【发布时间】:2020-04-24 18:21:09
【问题描述】:

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

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

java.lang.ClassCastException: java.lang.Long 无法转换为 java.lang.Integer

我的要求如下:

我有一个 db Collection -> "ALLUSERS",每个文档 id 都是用户的电话号码。

在此文档下,我有集合“化学”和文档“摘要”。在此,我有一个带有点数的章节数组。

enter image description here

我可以通过 DocuementSnapshot.get(...the chapter...) 获取此详细信息。但是,一旦我尝试对数组中的值求和,就会出现上述错误。

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

                         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 android firebase arraylist google-cloud-firestore


    【解决方案1】:

    您收到以下错误行:

    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 值创建总和。

    【讨论】:

      猜你喜欢
      • 2016-06-13
      • 2018-06-22
      • 1970-01-01
      • 2011-08-18
      • 2019-02-06
      • 2020-07-17
      相关资源
      最近更新 更多