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

Configuration 'androidTestCompile' is obsolete and has been replaced with 'androidTestImplementation' and 'androidTestApi'. It will be removed at the end of 2018. For more information see: http://d.android.com/r/tools/update-dependency-configurations.html Configuration 'compile' is obsolete and has been replaced with 'implementation' and 'api'. It will be removed at the end of 2018. For more information see: http://d.android.com/r/tools/update-dependency-configurations.html Configuration 'androidTestApi' is obsolete and has been replaced with 'androidTestImplementation'. It will be removed at the end of 2018. For more information see: http://d.android.com/r/tools/update-dependency-configurations.html Configuration 'testCompile' is obsolete and has been replaced with 'testImplementation' and 'testApi'. It will be removed at the end of 2018. For more information see: http://d.android.com/r/tools/update-dependency-configurations.html Configuration 'testApi' is obsolete and has been replaced with 'testImplementation'. It will be removed at the end of 2018. For more information see: http://d.android.com/r/tools/update-dependency-configurations.html

Get Two digit after decimal point in Android or Java

Error:java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant). at com.google.android.material.internal.ThemeEnforcement.checkTheme(ThemeEnforcement.java:240) at com.google.android.material.internal.ThemeEnforcement.checkMaterialTheme(ThemeEnforcement.java:215) at com.google.android.material.internal.ThemeEnforcement.checkCompatibleTheme(ThemeEnforcement.java:143) at com.google.android.material.internal.ThemeEnforcement.obtainStyledAttributes(ThemeEnforcement.java:78) at com.google.android.material.chip.ChipDrawable.loadFromAttributes(ChipDrawable.java:359) at com.google.android.material.chip.ChipDrawable.createFromAttributes(ChipDrawable.java:292) at com.google.android.material.chip.Chip.(Chip.java:193) at com.google.android.material.chip.Chip.(Chip.java:186) at com.google.android.material.chip.Chip.(Chip.java:182) at com.demo.nspl.restaurantlite.Navigation_Drawer.SalesFragment.getFilterChip(SalesFragment.java:158) at com.demo.nspl.restaurantlite.Navigation_Drawer.SalesFragment.lambda$onCreateView$0(SalesFragment.java:71) at com.demo.nspl.restaurantlite.Navigation_Drawer.-$$Lambda$SalesFragment$KCm-iiczjdYbpiNmaNw12gtFOoQ.onClick(lambda) at android.view.View.performClick(View.java:5268) at android.view.View$PerformClick.run(View.java:21550) at android.os.Handler.handleCallback(Handler.java:822) at android.os.Handler.dispatchMessage(Handler.java:104) at android.os.Looper.loop(Looper.java:207) at android.app.ActivityThread.main(ActivityThread.java:5811) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:681)