2016-07-21 2 views
1

나는 java에 익숙하며 hashMapList 목록 아래에 선언했다.목록 정렬 방법 <HashMap <String, String >> ls in java?

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

나는 hashMapList에 데이터를 입력하는 다른 방법 insertData을 만들었습니다.

나는 또한 다른 목록을 만들었습니다 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를 필터링 할.

자바로 가능합니까? 도와주세요. 감사합니다. 최선을 다했지만 이해할 수 없습니다.

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; 

    } 
+3

올바른 값을지도에서 가져 와서 비교하는 'Comparator >'과 함께 Collections.sort()를 사용하여 비교합니다. – Thomas

+0

@ 토마스 당신은 나에게 예를 들어 줄 수 있습니다 – ADMIN

+0

Yikes : 그 많은 매개 변수가있는 방법은 항상 모호합니다. 예를 들어 잘못된 순서로 두 개의 매개 변수를 전달하는 것은 매우 쉽습니다. [builder pattern] (https://en.wikipedia.org/wiki/Builder_pattern) 사용을 고려해야합니다. –

답변

2

(정렬에 관한 제목을 기반으로하지 않음) 질문에 기초합니다. 당신은 COA (계정의 차트)

당신이 자바 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); 

내가 스트림으로 목록을 변환 람다를 사용하여 필터링하고를 사용하는 희망에 따라 목록을 필터링 할 수있는 솔루션이 필요합니다.

자바 8을 사용하지 않는 경우 아래를 확인하십시오.

Lambdaj

그것은 당신이 컬렉션을 필터링 할 수 있습니다.

희망이 있으면 도움이 될 것입니다.

+0

나는 특정 값을위한 것이 아니라 코아만을 사용하는 필터 목록을 가지고 있습니다. @ Beniton – ADMIN

+0

@ADMIN 미안하지만 당신을 얻지 못했습니다. 더 자세한 정보를 주시겠습니까? 당신은 모든 코카를 기반으로 필터링하고 싶습니까? 올바른 코카콜라가 있다면 뜻이 맞는지 확인해야합니다. – Beniton

0

나는 내 질문을 해결했습니다.

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")); 
      } 
      }); 
관련 문제