映射和arraylist,如果index为getEntry()为null

时间:2018-12-11 17:59:52

标签: java android

我有2个数组... 1个,分别是支付商品的人的名字,另一个是每个支付价格的商品。如果某人什么都没买,则在“名称”位置具有与nameArray相同索引的arrayPrice为空。如果条目为空,如何将默认值设置为“ 0”->大十进制? 那是我的代码,即使我有1个人付款1次,也只能用[0]填充数组:

db.collection("users").document(email).collection("Group").document(groupName)
            .get()
            .addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                @Override
                public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                    if (task.isSuccessful()) {
                        DocumentSnapshot document = task.getResult();

                      // Getting all the partecipants of a group
                        String[] values = (String.valueOf(document.get("partecipant")).replace("[", "").replace("]", "").replace(" ", "").split(","));
                        for(String value : values){
                            nameArray.add(value);
                        }
                        Log.v("nameArraydownloaded", nameArray.toString());
                    }
                }
            });

    DocumentReference docRef = db.collection("users").document(email).collection("Group").document(groupName);

    docRef.collection("Payments")
            .get()
            .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                @Override
                public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                    for (QueryDocumentSnapshot document : queryDocumentSnapshots) {

                        //Extracting payment description from each document


                        cont += 1;
                        //Extracting cost and who payed from each document

                        price = document.getString("cost");
                        priceFloat += Float.parseFloat(price);
                        priceArray.add(new BigDecimal(price));
                        name = document.getString("paid by");
                        nameArray.add(name);

                    }

                    numberOfPaymentTV.setText(String.valueOf(cont));
                    totalCost.setText(decimalFormat.format(priceFloat) + "€");
                    cpp = decimalFormat.format(priceFloat / Float.parseFloat(num_partecipants));
                    costPerPerson.setText(cpp + "€");


                    while(priceArray.size() < nameArray.size()){
                        priceArray.add(BigDecimal.valueOf(0));
                    }
                    if (nameArray.size() > 0) {
                        // Manipulate the arrays
                        Map<String, BigDecimal> totals = new HashMap<>();

                        for (int i = 0; i < nameArray.size(); ++i) {
                            String name = nameArray.get(i);

                            BigDecimal price = priceArray.get(i);
                            BigDecimal total = totals.get(name);

                            if (total != null) {
                                totals.put(name, total.add(price));
                            } else {
                                totals.put(name, price);
                            }
                        }
                        nameArray.clear();
                        priceArray.clear();
                        // Adding single name and price value for each partecipant
                        for (Map.Entry<String, BigDecimal> entry : totals.entrySet()) {
                            nameArray.add(entry.getKey());
                            priceArray.add(entry.getValue());

                        }
                        // Order the array by descending price
                        for (int i = 0; i < priceArray.size(); i++) {
                            for (int j = 0; j < priceArray.size() - 1; j++) {
                                if (priceArray.get(j).compareTo(priceArray.get(j + 1)) < 0) {
                                    BigDecimal tempPrice = priceArray.get(j);
                                    String tempName = nameArray.get(j);
                                    priceArray.set(j, priceArray.get(j + 1));
                                    nameArray.set(j, nameArray.get(j + 1));
                                    priceArray.set(j + 1, tempPrice);
                                    nameArray.set(j + 1, tempName);
                                }
                            }
                        }
                        Log.v("priceArray", priceArray.toString());
                        for (int i = 0; i < nameArray.size() - 1; i++) {
                            if (BigDecimal.valueOf(Float.parseFloat(cpp.replace(",", "."))).subtract(priceArray.get(i)).compareTo(BigDecimal.ZERO) < 0) {
                                receiverArray.add(nameArray.get(i));

                            }

                        }


                        for (int i = 1; i < nameArray.size(); i++) {

                            if (BigDecimal.valueOf(Float.parseFloat(cpp.replace(",", "."))).subtract(priceArray.get(i)).compareTo(BigDecimal.ZERO) > 0) {

                                debtorArray.add(nameArray.get(i));
                            }
                        }
                        if (receiverArray.size() > debtorArray.size()) {
                            for (int i = 0; i < nameArray.size() - 1; i++) {
                                differenceArray.add(priceArray.get(i).subtract(BigDecimal.valueOf(Float.parseFloat(cpp.replace(",", ".")))).abs());

                            }
                        } else {
                            for (int i = 1; i < nameArray.size(); i++) {
                                differenceArray.add(priceArray.get(i).subtract(BigDecimal.valueOf(Float.parseFloat(cpp.replace(",", ".")))).abs());
                            }
                        }

                        if (!receiverArray.isEmpty() && !debtorArray.isEmpty()) {
                            while (receiverArray.size() < debtorArray.size()) {
                                receiverArray.add(receiverArray.get(receiverArray.size() - 1));
                            }
                            while (debtorArray.size() < receiverArray.size()) {
                                debtorArray.add(debtorArray.get(debtorArray.size() - 1));
                            }
                        }
                        Log.v("recArray", receiverArray.toString());
                        Log.v("debArray", debtorArray.toString());
                        Log.v("difArray", differenceArray.toString());

                        customAdapter = new ReportAdapter(getActivity(), debtorArray, receiverArray, differenceArray);
                        listView.setAdapter(customAdapter);
                    }
                }

具有此代码的数组为[14.00],但名称数组中有3个人,我需要priceArray为:[14.00,0,0] 我该怎么办?

1 个答案:

答案 0 :(得分:0)

您需要map.getOrDefault("key", <defaultValue>)

  

返回指定键所映射到的值,或者   如果此映射不包含键的映射,则为defaultValue。