首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在java中对List<HashMap<String,String>> ls进行排序?

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

Stack Overflow用户
提问于 2016-07-21 16:26:36
回答 2查看 192关注 0票数 1

我是java新手,我在hashMapList列表下面声明了这一点。

代码语言:javascript
复制
List<HashMap<String, String>> hashMapList = new ArrayList<HashMap<String, String>>();

我做了另一个将数据放入hashMapList的方法insertData

我还做了另一个列表psData

代码语言:javascript
复制
HashMap<String, String> psData = new HashMap<String, String>();

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

代码语言:javascript
复制
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中是可能的吗?请帮帮我。谢谢你,我已经尽力了,但是我不明白。

代码语言:javascript
复制
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;

  }
EN

回答 2

Stack Overflow用户

发布于 2016-07-21 17:05:33

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

希望您使用的是java 8。

代码语言:javascript
复制
        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

它允许您对集合进行过滤。

希望能对你有所帮助。

票数 2
EN

Stack Overflow用户

发布于 2016-07-21 17:53:12

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

代码语言:javascript
复制
Collections.sort(hashMapList, new Comparator<HashMap<String, String>>() {
            public int compare(HashMap<String, String> mapping1, HashMap<String, String> mapping2) {
              return mapping1.get("coa").compareTo(mapping2.get("coa"));
            }
          });
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38498898

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档