Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

462
Views
Kotlin How to use java streams .map() in kotlin to map a different object response

I am trying to map an array of objects to another array with different kind of objects, I used to do this using streams in java 8 it was pretty straight forward, instantiate an object set its values and return the object. I just switched to Kotlin and really sometimes is more confusing to do this kind of operations. All the examples I found are really simple and could not find something I want.

I have this BalanceMap class:

data class BalanceMap @JsonCreator constructor( var balType: String, var value: Any )

I am getting the data from web service.

val balances: List<AcctBal> = res.getAcctBals();

the AcctBal class looks like this

public class AcctBal {

  @SerializedName("CurAmt")
  @Expose
  private CurAmt curAmt;

  @SerializedName("Desc")
  @Expose
  private String desc;

  @SerializedName("ExpDt")
  @Expose
  private LocalDateTime expDt;
}

and try to map that response to var balanceList: List<BalanceMap>

balances.map {}

--> var balanceList: List<BalanceMap> = balances.map { t -> fun AcctBal.toBalanceMap() = BalanceMap( balType = "", value = "" )}

I want to do something like this:

List<ProductDetail> details = acctBal.stream().filter(f -> f.getBalType() != null).map(e -> {
                String bal = e.getBalType();
                if (avalProductInfo.getBankId().equals("00010016")) {
                    bal = e.getBalType();
                }
                ProductDetail detail = new ProductDetail();
                detail.setItem(bal);
                if (e.getCurAmt() != null) {
                    detail.setValue(e.getCurAmt().getAmt().toString());
                } else if (e.getRelationDt() != null) {
                    detail.setValue(e.getRelationDt().toGregorianCalendar().getTimeInMillis());
                } else if (e.getMemo() != null) {
                    detail.setValue(e.getMemo());
                }
                return detail;
            }).collect(toList());

I've been experimenting but is always wrong, any help will be highly appreciated. Happy coding!

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

some quick prototyping

    details  = acctBal
                     .filter{ f -> f.getBalType() != null }
                     .map { it -> mapToProductDetail (it) }

you can have a look here

over 4 years ago · Santiago Trujillo Report

0

Thanks to @Hakob Hakobyan for pointing in the right direction,

I left my solution like this:

fun mapRs(rs: AthProductResponse): BalanceByAccountRs {
    val res = rs.getPartyAcctRelRec();
    val balances: List<AcctBal> = res.getAcctBals();
    val account = Account(res.getPartyAcctRelInfo().depAcctId.acctId, res.getPartyAcctRelInfo().depAcctId.acctType)
    var balanceList: List<BalanceMap> = balances
        .filter { f -> f.getDesc() != null }
        .map { it -> mapToProductDetail(it) }
        .toList()
    return BalanceByAccountRs(account, balanceList)
}

fun mapToProductDetail(bal: AcctBal): BalanceMap {
    var propertyValue: Long = 0L;
    if(bal.getExpDt() != null) {
        propertyValue = Timestamp.valueOf(bal.getExpDt()).getTime()
    } else {
        propertyValue = bal.getCurAmt().getAmt().toLong()
    }
    return BalanceMap(bal.getDesc(), propertyValue)
}

Just in case someone is going through the same. Happy coding

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!