java - 如何在java中对List<HashMap<String, String>> ls进行排序?

标签 java arraylist

我是java新手,我已经在下面声明了hashMapList列表。

List<HashMap<String, String>> hashMapList = new ArrayList<HashMap<String, String>>();

我制作了另一种方法insertData将数据放入hashMapList中。

我还制作了另一个列表 psData

HashMap<String, String> psData = new HashMap<String, String>();

因此,为了在 hashMapList 中插入列表,我使用了下面的代码。

HashMap<String, String> psData = new HashMap<String, String>();

so for insert the list in hashMapList i have use below code.

psData = insertData(payment.getDocumentNo(), payment.getId(),
                  payment.getPaymentDate(), creditLeft, payment.getBusinessPartner(), group,
                  recOrPay.equals("RECEIVABLES") ? paymentInTab : paymentOutTab, dateFormat, true,
                  BigDecimal.ZERO, addressline1, addressline2, City, postalcode, state, email,
                  phoneno, payment.getAmount(), duration, discount,
                  payment.getFinancialTransactionAmount(), payment.getWriteoffAmount(),
                  Outstandings, Coa, Ev);
              hashMapList.add(psData); 

现在我想使用我在 insertData 方法中传递的 coa 属性来过滤 hashMapList。

这在java中可能吗?请帮我。 谢谢你,我已经尽力了,但我不明白。

private HashMap<String, String> insertData(String documentNo, String id, Date date,
      BigDecimal amount, BusinessPartner bpartner, int group, String tabId,
      SimpleDateFormat dateFormat, boolean credits, BigDecimal doubtfulDebt, String Address1,
      String Address2, String Zip, String City, String State, String Email, String Phone_h,
      BigDecimal InvoiceAmount, int Durations, BigDecimal Discount, BigDecimal Payments,
      BigDecimal Writeoffs, BigDecimal Outstandings, String Coa, ElementValue ev) {
    HashMap<String, String> psData = new HashMap<String, String>();
    psData.put("INVOICE_NUMBER", documentNo);
    psData.put("INVOICE_ID", id);
    psData.put("INVOICE_DATE", dateFormat.format(date));
    psData.put("AMOUNT" + group, amount.compareTo(BigDecimal.ZERO) == 0 ? null : amount.toString());
    psData.put("DOUBTFUL_DEBT",
        doubtfulDebt.compareTo(BigDecimal.ZERO) == 0 ? null : doubtfulDebt.toString());
    BigDecimal percentage = calculatePercentage(amount.add(doubtfulDebt), doubtfulDebt);
    psData.put("PERCENTAGE",
        percentage.compareTo(BigDecimal.ZERO) == 0 ? null : percentage.toString());
    if (credits) {
      psData.put("SHOW_NETDUE", amount.add(doubtfulDebt).toString());
    } else {
      psData.put("NETDUE", amount.add(doubtfulDebt).toString());
      psData.put("SHOW_NETDUE", amount.add(doubtfulDebt).toString());
    }
    psData.put("BPARTNER", bpartner.getId().toString());
    psData.put("BPARTNERNAME", bpartner.getIdentifier().toString());
    psData.put("TABID", tabId);
    psData.put("address1", Address1);
    psData.put("address2", Address2);
    psData.put("zip", Zip);
    psData.put("city", City);
    psData.put("state", State);
    psData.put("email", Email);
    psData.put("phone_h", Phone_h);
    psData.put("invoiceamount",
        InvoiceAmount.compareTo(BigDecimal.ZERO) == 0 ? "-" : InvoiceAmount.toString());
    psData.put("durations",
        Integer.toString(Durations).equals("0") ? "-" : Integer.toString(Durations));
    psData.put("payments", Payments.compareTo(BigDecimal.ZERO) == 0 ? "0.00" : Payments.toString());
    psData.put("writeoffs",
        Writeoffs.compareTo(BigDecimal.ZERO) == 0 ? "0.00" : Writeoffs.toString());
    psData.put("outstandings",
        Outstandings.compareTo(BigDecimal.ZERO) == 0 ? "0.00" : Outstandings.toString());
    psData
        .put("discounts", Discount.compareTo(BigDecimal.ZERO) == 0 ? "0.00" : Discount.toString());
    psData.put("coa", Coa);
    psData.put("ev", ev.getId());
    return psData;

  }

最佳答案

基于问题(而不是基于有关排序的标题)。您需要一个解决方案来根据 coa(科目表)过滤列表

希望您使用的是 java 8

        List<Map<String,String>> list = new ArrayList<>();

        Map<String,String> map = new HashMap<>();
        map.put("prop1", "value1");
        map.put("prop2", "value2");
        map.put("coa", "value3");
        list.add(map);

        map = new HashMap<>();
        map.put("prop1", "value1");
        map.put("prop2", "value2");
        map.put("coa", "value3");
        list.add(map);

        map = new HashMap<>();
        map.put("prop1", "v1");
        map.put("prop2", "v2");
        map.put("coa", "v3");
        list.add(map);

        List<Map<String,String>> listMapWithProp3EqualValue3 = list.stream()
                .filter(p -> p.get("coa").equals("value3")).collect(Collectors.toList());

        System.out.println("Filtered List with coa=value3 :" + listMapWithProp3EqualValue3);

        listMapWithProp3EqualValue3 = list.stream()
                .filter(p -> p.get("coa").equals("v3")).collect(Collectors.toList());

        System.out.println("Filtered List coa=v3 :" + listMapWithProp3EqualValue3);

我正在将列表转换为流并使用 lambda 对其进行过滤。

如果您不使用 java 8,请检查以下内容。

Lambdaj

它允许您过滤集合。

希望对您有帮助。

关于java - 如何在java中对List<HashMap<String, String>> ls进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38498898/

相关文章:

java - Hadoop WordCount 按单词出现次数排序

java - 接受void的通用参数类型

java - org.json.JSONException : No value for status Android Parsing

java - 通用数组列表冒泡排序问题

java - 如何从字符串的数组列表中搜索字符串中的特定单词

java - selenium cucumber JUnit 框架中的空指针异常

java - 如何为字符串设置字体

java - 复杂 Hashmap ArrayList 生成器

java - 使用递归对数字 ArrayList 进行排列

java - 将逗号分隔的字符串添加到 ArrayList,反之亦然