2
\$\begingroup\$

I'm using 1.8.0_162 version of Java and here is my task about stream framework

Map<String, BigDecimal> paymentAmountInfoMap = serviceResponse
        .getPayments()
        .stream()
        .collect(Collectors.groupingBy(x -> String.valueOf(x.getDueDate().getYear())))
        .entrySet()
        .stream()
        .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue()
            .stream()
            .map(ConsumerPolicyPayment::getPaymentAmount)
            .collect(Collectors.toList()).stream()
            .reduce(BigDecimal.ZERO, BigDecimal::add)));

and here is my serviceResponse object:

    {
    "payments": [
        {
            "paymentDate": {
                "dateValue": "2018-01-02",
                "formattedValue": "02 Ocak 2018"
            },
            "dueDate": {
                "dateValue": "2018-01-24",
                "formattedValue": "24 Ocak 2018"
            },
            "paymentAmount": {
                "numberValue": 273,
                "formattedValue": "273",
                "numberValueInUsd": 61.04,
                "formattedValueInUsd": "62"
            },
            "receiptNo": "7520387",
            "expectedAmount": {
                "numberValue": 273,
                "formattedValue": "273",
                "numberValueInUsd": 61.04,
                "formattedValueInUsd": "62"
            },
            "investmentDate": {
                "dateValue": null,
                "formattedValue": null
            },
            "investmentAmount": {
                "numberValue": null,
                "formattedValue": null,
                "numberValueInUsd": null,
                "formattedValueInUsd": null
            },
            "presentValue": {
                "numberValue": null,
                "formattedValue": null,
                "numberValueInUsd": null,
                "formattedValueInUsd": null
            },
            "valueIncreaseRatio": null
        },
        {
            "paymentDate": {
                "dateValue": "2017-12-01",
                "formattedValue": "01 Aralık 2017"
            },
            "dueDate": {
                "dateValue": "2017-12-24",
                "formattedValue": "24 Aralık 2017"
            },
            "paymentAmount": {
                "numberValue": 273,
                "formattedValue": "273",
                "numberValueInUsd": 61.04,
                "formattedValueInUsd": "62"
            },
            "receiptNo": "7440108",
            "expectedAmount": {
                "numberValue": 273,
                "formattedValue": "273",
                "numberValueInUsd": 61.04,
                "formattedValueInUsd": "62"
            },
            "investmentDate": {
                "dateValue": "2018-01-08",
                "formattedValue": "08 Ocak 2018"
            },
            "investmentAmount": {
                "numberValue": 252.53,
                "formattedValue": "253",
                "numberValueInUsd": 56.46,
                "formattedValueInUsd": "57"
            },
            "presentValue": {
                "numberValue": 194.58,
                "formattedValue": "195",
                "numberValueInUsd": 43.51,
                "formattedValueInUsd": "44"
            },
            "valueIncreaseRatio": -0.2295
        },
        {
            "paymentDate": {
                "dateValue": "2017-11-01",
                "formattedValue": "01 Kasım 2017"
            },
            "dueDate": {
                "dateValue": "2017-11-24",
                "formattedValue": "24 Kasım 2017"
            },
            "paymentAmount": {
                "numberValue": 273,
                "formattedValue": "273",
                "numberValueInUsd": 61.04,
                "formattedValueInUsd": "62"
            },
            "receiptNo": "7362663",
            "expectedAmount": {
                "numberValue": 273,
                "formattedValue": "273",
                "numberValueInUsd": 61.04,
                "formattedValueInUsd": "62"
            },
            "investmentDate": {
                "dateValue": "2017-12-11",
                "formattedValue": "11 Aralık 2017"
            },
            "investmentAmount": {
                "numberValue": 252.53,
                "formattedValue": "253",
                "numberValueInUsd": 56.46,
                "formattedValueInUsd": "57"
            },
            "presentValue": {
                "numberValue": 197.74,
                "formattedValue": "198",
                "numberValueInUsd": 44.21,
                "formattedValueInUsd": "45"
            },
            "valueIncreaseRatio": -0.2169
        },
        {
            "paymentDate": {
                "dateValue": "2017-10-02",
                "formattedValue": "02 Ekim 2017"
            },
            "dueDate": {
                "dateValue": "2017-10-24",
                "formattedValue": "24 Ekim 2017"
            },
            "paymentAmount": {
                "numberValue": 273,
                "formattedValue": "273",
                "numberValueInUsd": 61.04,
                "formattedValueInUsd": "62"
            },
            "receiptNo": "7285959",
            "expectedAmount": {
                "numberValue": 273,
                "formattedValue": "273",
                "numberValueInUsd": 61.04,
                "formattedValueInUsd": "62"
            },
            "investmentDate": {
                "dateValue": "2017-11-09",
                "formattedValue": "09 Kasım 2017"
            },
            "investmentAmount": {
                "numberValue": 252.53,
                "formattedValue": "253",
                "numberValueInUsd": 56.46,
                "formattedValueInUsd": "57"
            },
            "presentValue": {
                "numberValue": 197.83,
                "formattedValue": "198",
                "numberValueInUsd": 44.23,
                "formattedValueInUsd": "45"
            },
            "valueIncreaseRatio": -0.2166
        },
        {
            "paymentDate": {
                "dateValue": "2017-09-05",
                "formattedValue": "05 Eylül 2017"
            },
            "dueDate": {
                "dateValue": "2017-09-24",
                "formattedValue": "24 Eylül 2017"
            },
            "paymentAmount": {
                "numberValue": 273,
                "formattedValue": "273",
                "numberValueInUsd": 61.04,
                "formattedValueInUsd": "62"
            },
            "receiptNo": "7212680",
            "expectedAmount": {
                "numberValue": 273,
                "formattedValue": "273",
                "numberValueInUsd": 61.04,
                "formattedValueInUsd": "62"
            },
            "investmentDate": {
                "dateValue": "2017-10-13",
                "formattedValue": "13 Ekim 2017"
            },
            "investmentAmount": {
                "numberValue": 252.53,
                "formattedValue": "253",
                "numberValueInUsd": 56.46,
                "formattedValueInUsd": "57"
            },
            "presentValue": {
                "numberValue": 199.99,
                "formattedValue": "200",
                "numberValueInUsd": 44.72,
                "formattedValueInUsd": "45"
            },
            "valueIncreaseRatio": -0.2081
        },
        {
            "paymentDate": {
                "dateValue": "2017-08-01",
                "formattedValue": "01 Ağustos 2017"
            },
            "dueDate": {
                "dateValue": "2017-08-24",
                "formattedValue": "24 Ağustos 2017"
            },
            "paymentAmount": {
                "numberValue": 273,
                "formattedValue": "273",
                "numberValueInUsd": 61.04,
                "formattedValueInUsd": "62"
            },
            "receiptNo": "7137982",
            "expectedAmount": {
                "numberValue": 273,
                "formattedValue": "273",
                "numberValueInUsd": 61.04,
                "formattedValueInUsd": "62"
            },
            "investmentDate": {
                "dateValue": "2017-09-08",
                "formattedValue": "08 Eylül 2017"
            },
            "investmentAmount": {
                "numberValue": 252.53,
                "formattedValue": "253",
                "numberValueInUsd": 56.46,
                "formattedValueInUsd": "57"
            },
            "presentValue": {
                "numberValue": 202.18,
                "formattedValue": "203",
                "numberValueInUsd": 45.21,
                "formattedValueInUsd": "46"
            },
            "valueIncreaseRatio": -0.1994
        },
        {
            "paymentDate": {
                "dateValue": "2017-07-03",
                "formattedValue": "03 Temmuz 2017"
            },
            "dueDate": {
                "dateValue": "2017-07-24",
                "formattedValue": "24 Temmuz 2017"
            },
            "paymentAmount": {
                "numberValue": 255,
                "formattedValue": "255",
                "numberValueInUsd": 57.01,
                "formattedValueInUsd": "58"
            },
            "receiptNo": "7060723",
            "expectedAmount": {
                "numberValue": 255,
                "formattedValue": "255",
                "numberValueInUsd": 57.01,
                "formattedValueInUsd": "58"
            },
            "investmentDate": {
                "dateValue": "2017-08-10",
                "formattedValue": "10 Ağustos 2017"
            },
            "investmentAmount": {
                "numberValue": 235.88,
                "formattedValue": "236",
                "numberValueInUsd": 52.74,
                "formattedValueInUsd": "53"
            },
            "presentValue": {
                "numberValue": 190.89,
                "formattedValue": "191",
                "numberValueInUsd": 42.68,
                "formattedValueInUsd": "43"
            },
            "valueIncreaseRatio": -0.1907
        },
        {
            "paymentDate": {
                "dateValue": "2017-06-01",
                "formattedValue": "01 Haziran 2017"
            },
            "dueDate": {
                "dateValue": "2017-06-24",
                "formattedValue": "24 Haziran 2017"
            },
            "paymentAmount": {
                "numberValue": 255,
                "formattedValue": "255",
                "numberValueInUsd": 57.01,
                "formattedValueInUsd": "58"
            },
            "receiptNo": "6992100",
            "expectedAmount": {
                "numberValue": 255,
                "formattedValue": "255",
                "numberValueInUsd": 57.01,
                "formattedValueInUsd": "58"
            },
            "investmentDate": {
                "dateValue": "2017-07-10",
                "formattedValue": "10 Temmuz 2017"
            },
            "investmentAmount": {
                "numberValue": 235.88,
                "formattedValue": "236",
                "numberValueInUsd": 52.74,
                "formattedValueInUsd": "53"
            },
            "presentValue": {
                "numberValue": 193.34,
                "formattedValue": "194",
                "numberValueInUsd": 43.23,
                "formattedValueInUsd": "44"
            },
            "valueIncreaseRatio": -0.1803
        },
        {
            "paymentDate": {
                "dateValue": "2017-05-02",
                "formattedValue": "02 Mayıs 2017"
            },
            "dueDate": {
                "dateValue": "2017-05-24",
                "formattedValue": "24 Mayıs 2017"
            },
            "paymentAmount": {
                "numberValue": 255,
                "formattedValue": "255",
                "numberValueInUsd": 57.01,
                "formattedValueInUsd": "58"
            },
            "receiptNo": "6919784",
            "expectedAmount": {
                "numberValue": 255,
                "formattedValue": "255",
                "numberValueInUsd": 57.01,
                "formattedValueInUsd": "58"
            },
            "investmentDate": {
                "dateValue": "2017-06-09",
                "formattedValue": "09 Haziran 2017"
            },
            "investmentAmount": {
                "numberValue": 235.88,
                "formattedValue": "236",
                "numberValueInUsd": 52.74,
                "formattedValueInUsd": "53"
            },
            "presentValue": {
                "numberValue": 195.61,
                "formattedValue": "196",
                "numberValueInUsd": 43.74,
                "formattedValueInUsd": "44"
            },
            "valueIncreaseRatio": -0.1707
        },
        {
            "paymentDate": {
                "dateValue": "2017-04-03",
                "formattedValue": "03 Nisan 2017"
            },
            "dueDate": {
                "dateValue": "2017-04-24",
                "formattedValue": "24 Nisan 2017"
            },
            "paymentAmount": {
                "numberValue": 255,
                "formattedValue": "255",
                "numberValueInUsd": 57.01,
                "formattedValueInUsd": "58"
            },
            "receiptNo": "6848263",
            "expectedAmount": {
                "numberValue": 255,
                "formattedValue": "255",
                "numberValueInUsd": 57.01,
                "formattedValueInUsd": "58"
            },
            "investmentDate": {
                "dateValue": "2017-05-15",
                "formattedValue": "15 Mayıs 2017"
            },
            "investmentAmount": {
                "numberValue": 235.88,
                "formattedValue": "236",
                "numberValueInUsd": 52.74,
                "formattedValueInUsd": "53"
            },
            "presentValue": {
                "numberValue": 197.19,
                "formattedValue": "198",
                "numberValueInUsd": 44.09,
                "formattedValueInUsd": "45"
            },
            "valueIncreaseRatio": -0.164
        },
        {
            "paymentDate": {
                "dateValue": "2017-03-01",
                "formattedValue": "01 Mart 2017"
            },
            "dueDate": {
                "dateValue": "2017-03-24",
                "formattedValue": "24 Mart 2017"
            },
            "paymentAmount": {
                "numberValue": 255,
                "formattedValue": "255",
                "numberValueInUsd": 57.01,
                "formattedValueInUsd": "58"
            },
            "receiptNo": "6776034",
            "expectedAmount": {
                "numberValue": 255,
                "formattedValue": "255",
                "numberValueInUsd": 57.01,
                "formattedValueInUsd": "58"
            },
            "investmentDate": {
                "dateValue": "2017-04-10",
                "formattedValue": "10 Nisan 2017"
            },
            "investmentAmount": {
                "numberValue": 235.88,
                "formattedValue": "236",
                "numberValueInUsd": 52.74,
                "formattedValueInUsd": "53"
            },
            "presentValue": {
                "numberValue": 199.45,
                "formattedValue": "200",
                "numberValueInUsd": 44.6,
                "formattedValueInUsd": "45"
            },
            "valueIncreaseRatio": -0.1544
        },
        {
            "paymentDate": {
                "dateValue": "2017-02-01",
                "formattedValue": "01 Şubat 2017"
            },
            "dueDate": {
                "dateValue": "2017-02-24",
                "formattedValue": "24 Şubat 2017"
            },
            "paymentAmount": {
                "numberValue": 255,
                "formattedValue": "255",
                "numberValueInUsd": 57.01,
                "formattedValueInUsd": "58"
            },
            "receiptNo": "6707743",
            "expectedAmount": {
                "numberValue": 255,
                "formattedValue": "255",
                "numberValueInUsd": 57.01,
                "formattedValueInUsd": "58"
            },
            "investmentDate": {
                "dateValue": "2017-03-13",
                "formattedValue": "13 Mart 2017"
            },
            "investmentAmount": {
                "numberValue": 235.88,
                "formattedValue": "236",
                "numberValueInUsd": 52.74,
                "formattedValueInUsd": "53"
            },
            "presentValue": {
                "numberValue": 201.38,
                "formattedValue": "202",
                "numberValueInUsd": 45.03,
                "formattedValueInUsd": "46"
            },
            "valueIncreaseRatio": -0.1462
        },
        {
            "paymentDate": {
                "dateValue": "2017-01-02",
                "formattedValue": "02 Ocak 2017"
            },
            "dueDate": {
                "dateValue": "2017-01-24",
                "formattedValue": "24 Ocak 2017"
            },
            "paymentAmount": {
                "numberValue": 255,
                "formattedValue": "255",
                "numberValueInUsd": 57.01,
                "formattedValueInUsd": "58"
            },
            "receiptNo": "6637517",
            "expectedAmount": {
                "numberValue": 255,
                "formattedValue": "255",
                "numberValueInUsd": 57.01,
                "formattedValueInUsd": "58"
            },
            "investmentDate": {
                "dateValue": "2017-02-13",
                "formattedValue": "13 Şubat 2017"
            },
            "investmentAmount": {
                "numberValue": 235.88,
                "formattedValue": "236",
                "numberValueInUsd": 52.74,
                "formattedValueInUsd": "53"
            },
            "presentValue": {
                "numberValue": 203.35,
                "formattedValue": "204",
                "numberValueInUsd": 45.47,
                "formattedValueInUsd": "46"
            },
            "valueIncreaseRatio": -0.1379
        }
    ],
    "repayments": [],
    "graphData": {
        "payments": {
            "2017": 3150,
            "2018": 273
        },
        "repayments": {}
    },
    "totalPaymentAmount": {
        "numberValue": 3423,
        "formattedValue": "3.423",
        "numberValueInUsd": 765.28,
        "formattedValueInUsd": "766"
    },
    "totalInvestmentAmount": {
        "numberValue": 2913.81,
        "formattedValue": "2.914",
        "numberValueInUsd": 651.44,
        "formattedValueInUsd": "652"
    },
    "totalPresentValue": {
        "numberValue": 2373.53,
        "formattedValue": "2.374",
        "numberValueInUsd": 530.65,
        "formattedValueInUsd": "531"
    },
    "remainingInstallmentCount": 54,
    "lastPaymentDate": {
        "dateValue": "2018-01-24",
        "formattedValue": "24 Ocak 2018"
    },
    "nearestPaymentDate": {
        "dateValue": "2018-02-24",
        "formattedValue": "24 Şubat 2018"
    }
}

What I want to achieve is like that:

{
"2017":432,45,
"2018":25,1,
"2019":3566,4
}

It is working but not the best practice.

\$\endgroup\$
2
\$\begingroup\$

You may extract out different parts of the code as methods like so:

 Map<String, BigDecimal> paymentAmountInfoMap = 
                serviceResponse.getAllPaymentsPerYearEntrySet()
                .stream()
                .collect(Collectors.toMap(Map.Entry::getKey, e -> getAllPaymentSum(e)));

Method definitions are:

private Set<Map.Entry<String, List<Payemnt>>> getAllPaymentsPerYearEntrySet() {
        return getPayments()
        .stream()
        .collect(Collectors.groupingBy(Payment::getDueDateYearAsString))
        .entrySet();
    }

private BigDecimal getAllPaymentSum(Map.Entry<String, List<Payemnt>> e) {
    return e.getValue()
            .stream()
            .map(ConsumerPolicyPayment::getPaymentAmount)
            .reduce(BigDecimal.ZERO, BigDecimal::add);
}

in Payment class

public String getDueDateYearAsString(){
     return String.valueOf(getDueDate().getYear());
}
\$\endgroup\$
1
\$\begingroup\$

I have improved the solution with this:

Map<String, BigDecimal> paymentAmountInfoMap = serviceResponse
    .getPayments()
    .stream()
    .collect(Collectors.groupingBy(x -> String.valueOf(x.getDueDate().getYear()),
        Collectors.mapping(e -> e.getPaymentAmount(), Collectors.reducing(BigDecimal.ZERO, BigDecimal::add))));
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.