jayjaypg22
jayjaypg22

Reputation: 1751

Transform a List of List into into a List of another object

I use a method which returns a list of Lists. This List contains 6 Lists, each List contains 3 String : [[5, 0.01, 2], [7, 0.01, 3], [3, 0.02, 2], [8, 0.05, 3],[9, 0.1, 2], [6, 0.1, 3]]

Continuing from a previous answer...

But I have to split again this list in 3 differents values : [3, 0.01, 2]

The main goal is to transform this list of object in list of Change. A Change :

public class Change {
  private static final String value;
  private static final String amount;
  private static final String column;
...
}

I attempted but I think it's not the greatest way to accomplish :

List saisie = ServiceTableau.getInstance().deserialiser(quantites);
List<String> saisieToStrings = new ArrayList<String>();

for (Object object : saisieIHM) {
    saisieIHMToStrings.add(object != null ? object.toString() : null);
}

Is there a system method to do this? I can't use external libraries

Upvotes: 1

Views: 125

Answers (1)

nickb
nickb

Reputation: 59699

Create a constructor in your Change class:

public Change( String input) {
    String[] values = input.split(",");
    this.value = values[0];
    this.amount = values[1];
    this.column = values[2];
}

And then pass that string when you create the list:

List<Change> saisieToStrings = new ArrayList<Change>();
for (Object object : saisieIHM) {
    saisieIHMToStrings.add( new Change( object.toString()));
}

Upvotes: 1

Related Questions