Update specific field from custom class list or update specific object field from list in java or android using Stream api.


Java 8+ Stream api updaing specific from list. or
Update a single object from a list using stream or
Modifying Objects within stream in Java8 while iterating
Java 8 modify stream elements or Update nested list of objects using java stream api. or A Java Stream to UPDATE a Subset of Columns

Example:-

int messageLenght = 1500;

    List<ClsSMSLog> list = getList();
    list  = list.stream()
             .filter(item -> {
                 if (item.getStatusCode() == 1 || 
                               item.getStatusCode() == -1){
                     item.setCreditCount((int) 
      Math.ceil((double) messageLenght / 145 ));
                 }
                 return true;
             }).collect(Collectors.toList());
private static List<ClsSMSLog> getList() {
    return Arrays.asList(
      new ClsSMSLog(12, 11, 0, -1, "7778888295", "Neel", "Pending", "22/02/2019"),      new ClsSMSLog(13, 11, 0, 1, "77734888295", "Deep", "Send", "22/02/2019"),      new ClsSMSLog(14, 11, 0, 1, "7723488295", "Karan", "Send", "22/02/2019"),      new ClsSMSLog(15, 11, 0, -1, "7732888295", "meet", "Pending", "22/02/2019")
    );}

-> Pojo Class:
public class ClsSMSLog {


int logID,bulkID,CreditCount,StatusCode;
String Mobile = "",CustomerName= "",Status= "",StatusDateTime= "";

public ClsSMSLog(int logID, int bulkID, int creditCount, int statusCode,
                 String mobile, String customerName,
                 String status, String statusDateTime) {

    this.logID = logID;
    this.bulkID = bulkID;
    CreditCount = creditCount;
    StatusCode = statusCode;
    Mobile = mobile;
    CustomerName = customerName;
    Status = status;
    StatusDateTime = statusDateTime;
}

public int getLogID() {
    return logID;
}

public void setLogID(int logID) {
    this.logID = logID;
}

public int getBulkID() {
    return bulkID;
}

public void setBulkID(int bulkID) {
    this.bulkID = bulkID;
}

public int getCreditCount() {
    return CreditCount;
}

public void setCreditCount(int creditCount) {
    CreditCount = creditCount;
}

public int getStatusCode() {
    return StatusCode;
}

public void setStatusCode(int statusCode) {
    StatusCode = statusCode;
}

public String getMobile() {
    return Mobile;
}

public void setMobile(String mobile) {
    Mobile = mobile;
}

public String getCustomerName() {
    return CustomerName;
}

public void setCustomerName(String customerName) {
    CustomerName = customerName;
}

public String getStatus() {
    return Status;
}

public void setStatus(String status) {
    Status = status;
}

public String getStatusDateTime() {
    return StatusDateTime;
}

public void setStatusDateTime(String statusDateTime) {
    StatusDateTime = statusDateTime;
}}

-> Check the result:
for (ClsSMSLog item : list){
    System.out.println(item.getCreditCount());}

Output:-


11
11
11
11




Comments

Popular posts from this blog

Android Studio 3.5 or above usb device not detected in Android Studio or Unable to detect adb version, exit value: 0xc0000135. ADB depends on the Windows Universal C Runtime, which is usually installed by default via Windows Update. You may need to manually fetch and install the runtime package here: https://support.microsoft.com/en-ca/help/2999226/update-for-universal-c-runtime-in-windows

Lint found fatal errors while assembling a release target.

ViewModelProviders.of(this) is deprecated what to use instead ViewModelProviders.of(this) OR if you don't want to use ViewmodelFactory OR ViewModelProviders error.