LochanaT
LochanaT

Reputation: 17

Set variables values of a list to another list variables values in Java

Here is my Allowance class

public class Allowance {
    private String Id;
    private String Name;
    private String Type;
    private String RemainingAmount;
    private String TotalAmount;
}

This is created list

public List<Allowance> getBList(){
    List<Allowance> BList = new ArrayList<>();
    List.add(new Allowance("5964852","Name1","DATA","10","458965"));
    List.add(new Allowance("651655","Name2","DATA","20","5481662"));
    List.add(new Allowance("123","Name3","VOICE","30","8889625"));
    List.add(new Allowance("123423","Name4","DATA","20","325489"));
    return BList;
}

Here is my Benefits class

public class Benefits {
    private String itemId;
    private String bName;
    private String remaining;
    private String bTotal;
}

So, I want to set Allowance class variables values to Benefits class variables values Like this

Id value --> itemId value

Name value --> bName value

RemainingAmount value ---> remaining value

TotalAmount value ---> bTotal value

Then i want to return Benefits list like this

["itemId" : "5964852","bName": "Name1","remaining":"10","bTotal":"458965"]

Any one can help me ?

Upvotes: 0

Views: 625

Answers (3)

dyy.alex
dyy.alex

Reputation: 534

Use stream and lambda is easy to realize it.

List<Allowance> AList = BList.stream().map(B -> {
    new Allowance(
        B.getId(),
        B.getName(),
        B.getRemainingAmount(),
        B.bTotal())
}).collect(Collectors.toList())

Upvotes: 2

Aneesh
Aneesh

Reputation: 148

You can transfer the values from an Allowance instance by using the single arg constructor and then poplating the properties of the Benefits class from the Allowance instance that is passed in. You can also override the toString method of the Benefits class to obtain the desired output. The clone method in the allowance class will take in the list you have already created to return a list of Benefits instances.

The Allowance class:

class Allowance {
    private String Id;
    private String Name;
    private String Type;
    private String RemainingAmount;
    private String TotalAmount;

    public String getId() {
        return Id;
    }

    public String getName() {
        return Name;
    }

    public String getType() {
        return Type;
    }

    public String getRemainingAmount() {
        return RemainingAmount;
    }

    public String getTotalAmount() {
        return TotalAmount;
    }

    public Allowance(String id, String name, String type, String remainingAmount, String totalAmount) {
        Id = id;
        Name = name;
        Type = type;
        RemainingAmount = remainingAmount;
        TotalAmount = totalAmount;
    }

public List<Benefits> clone(List<Allowance> allowanceList){
        
        List<Benefits> benefitList = new ArrayList<>();
        for(Allowance allowance : allowanceList){
            benefitList.add(new Benefits(allowance));
        }
        
        return benefitList;
    }
}

The benefits class:

class Benefits{

    private String itemId;
    private String bName;
    private String remaining;
    private String bTotal;

    public Benefits(Allowance allowance){

        this.itemId = allowance.getId();
        this.bName = allowance.getName();
        this.remaining = allowance.getRemainingAmount();
        this.bTotal = allowance.getTotalAmount();

    }

    @Override
    public String toString() {
        return "[ itemId : " + this.itemId + ", bName : " + this.bName + ", remaining : " + this.remaining + ", bTotal :" + this.bTotal + "]";
    }
}

To create the list of Benefit items, you could also create the following List directly:

      List<Benefits> bList = new ArrayList<>();

        Allowance a = new Allowance("5964852","Name1","DATA","10","458965");
        Allowance b = new Allowance("651655","Name2","DATA","20","5481662");
        Allowance c =  new Allowance("123","Name3","VOICE","30","8889625");
        Allowance d = new Allowance("123423","Name4","DATA","20","325489");

        bList.add(new Benefits(a));
        bList.add(new Benefits(b));
        bList.add(new Benefits(c));
        bList.add(new Benefits(d));

Upvotes: 2

TheFemiFactor
TheFemiFactor

Reputation: 1

So from what I read in the replies to your original question, you'd be looking for something along these lines;

  1. You need to create getters for all the attributes of Allowance.

  2. Then create a constructor that takes an Allowance object as a parameter.

     public Benefits (Allowance a) {
    
         this.itemId = a.getID;
         this.bName = a.getName;
         this.remaning = a.getRemainingAmount;
         this.bTotal = a.getTotalAmount;
     }
    

Upvotes: 0

Related Questions