如何排序List <hashmap <string,string =“”>&gt;在java中?

时间:2016-07-21 08:26:36

标签: 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;

  }

2 个答案:

答案 0 :(得分:2)

基于问题(不基于关于排序的标题)。您需要一个解决方案来根据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);

我正在将列表转换为流并使用lambdas过滤它。

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

Lambdaj

它允许您过滤集合。

希望它能帮助你。

答案 1 :(得分:0)

我已经解决了我的问题,谢谢。

<root>
  <child>....</child>
  .
  .
  <special>
   <event><![CDATA[text->text]]></event>
   <event><![CDATA[text->text]]></event>
  ...
  </special>
</root>