Converting a list of strings to a BigDecimal list in java

I'm having a problem converting a String list to a Bigdecimal list in java.

I have a List of String like,

List<String> stringList = new LinkedList<String>();
stringList.add("123");
stringList.add("456");
stringList.add("789");

      

and BigDecimal List as

List<BigDecimal> bigDecimalList = new LinkedList<BigDecimal>();

      

Now I want to convert stringList

to bigDecimalList

. I know we can iterate through stringList

and add to bigDecimalList

using new BigDecimal(string)

. Is there any other job besides looping?

Any help is appreciated. Thank.

+3


source to share


4 answers


Well, something is needed for a loop - and until Java 8 comes with lambda expressions to make the conversion expression easier, there is no general way to convert. Obviously, you could write a method that took the name of the class and always passed each element of the incoming list as an argument to the constructor of the target class through reflection, but that would not work in all other situations.

Given how short the loop is, I would definitely do this:



List<BigDecimal> bigDecimalList = new LinkedList<BigDecimal>();
for (String value : stringList) {
    bigDecimalList.add(new BigDecimal(value));
}

      

Do you really need to avoid these four lines of code?

+8


source


At some level - either in an external library, a lower level library, or in code - you will have to iterate over the structure and create new objects BigDecimal

in another list.

Now that Java 8 is effectively failing, it can be expressed quite succinctly like this. This code assumes you have already defined stringList

.



List<BigDecimal> bigDecimalList = stringList.stream()
        .map(BigDecimal::new)
        .collect(Collectors.toList());

      

This takes all of the content stringList

, maps them to the constructor, BigDecimal

and collects them into a new object List

, which we then assign.

+5


source


Use Google Common transform

. You provide an original list and a transform function, and it loops through applying the transform to each item. This is done lazily, so no transformations are applied until you iterate or retrieve the items.

List<String> strings = ...
List<BigDecimal> numbers = Lists.transform(strings, new Function<String, BigDecimal>() {
  public BigDecimal apply(String str) {
    return new BigDecimal(str);
  }
});

      

Doing this will take you one step closer to functional programming.

+2


source


If you are using Eclipse Collections (formerly GS Collections) and changed stringList to MutableList or something similar, you can write:

MutableList<String> stringList = FastList.newListWith("123", "456", "789");

MutableList<BigDecimal> bigDecimalList = stringList.collect(new Function<String, BigDecimal>()
{
    public BigDecimal valueOf(String eachString)
    {
        return new BigDecimal(eachString);
    }
});

      

With Java 8 method guidelines, this becomes:

MutableList<BigDecimal> bigDecimalList = stringList.collect(BigDecimal::new);

      

Note: I am a committer for Eclipse collections.

+2


source







All Articles