java 在java中将字符串列表转换为BigDecimal列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14701757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 17:05:57  来源:igfitidea点击:

Convert List of String to List of BigDecimal in java

javalistcollectionstype-conversion

提问by Raja Asthana

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

我在将字符串列表转换为 java 中的 Bigdecimal 列表时遇到问题。

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

和 BigDecimal 列表为

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

Now I want to convert stringListto bigDecimalList. I know we can iterate through the stringListand can add to bigDecimalListusing new BigDecimal(string). Is there any other work around than looping???

现在我想转换stringListbigDecimalList. 我知道我们可以遍历stringList并且可以添加到bigDecimalList使用new BigDecimal(string). 除了循环还有其他解决方法吗???

Any help is appreciated. Thanks.

任何帮助表示赞赏。谢谢。

回答by Jon Skeet

Well something'sgot to loop - and until Java 8 comes with lambda expressions to make it easier to express the conversion, there's no general purpose way of doing the conversion. Obviously you could write a method which took the class name and always passed each element of the incoming list as an argument to the constructor of the target class via reflection, but that would fail in all kinds of other situations.

好吧,有些东西必须循环——在 Java 8 附带 lambda 表达式以更容易表达转换之前,没有进行转换的通用方法。显然,您可以编写一个方法,该方法采用类名并始终通过反射将传入列表的每个元素作为参数传递给目标类的构造函数,但这在其他各种情况下都会失败。

Given how short the loop is, I'd definitely do that:

鉴于循环有多短,我肯定会这样做:

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

Do you reallyneed to avoid those 4 lines of code?

真的需要避免这 4 行代码吗?

回答by Makoto

At somelevel - either in an external library, a lower level library, or in your code - you'll have to iterate over the structure and create new BigDecimalobjects in your other list.

某个级别 - 无论是在外部库、较低级别的库中,还是在您的代码中 - 您必须遍历结构并BigDecimal在其他列表中创建新对象。

Now that Java 8 is effectively out, it can be expressed rather tersely as thus. This code assumes that you already have stringListdefined somewhere.

现在 Java 8 已经正式发布了,可以这样简单地表达出来。此代码假定您已经在stringList某处进行了定义。

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

This takes all of the contents of the stringList, maps them across to the constructor of BigDecimal, and collects them into a new Listobject, which we then assign.

这获取 的所有内容stringList,将它们映射到 的构造函数BigDecimal,并将它们收集到一个新List对象中,然后我们分配该对象。

回答by Steve Kuo

Use Google Common's transform. You supply the original list and the transform function, and it does the looping, applying the transform to each element. This is actually done lazily, so the transforms aren't applied until you iterate or get the elements.

使用 Google Common 的transform. 您提供原始列表和转换函数,它执行循环,将转换应用于每个元素。这实际上是惰性完成的,因此在您迭代或获取元素之前不会应用转换。

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 move you one step closer to functional programming.

这样做将使您更接近函数式编程。

回答by Craig P. Motlin

If you use Eclipse Collections(formerly GS Collections) and change stringList to a MutableList or something similar, you can write:

如果您使用Eclipse Collections(以前称为 GS Collections)并将 stringList 更改为 MutableList 或类似的东西,您可以编写:

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 references, this becomes:

使用 Java 8 方法引用,这变成:

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

Note:I am a committer for Eclipse Collections.

注意:我是 Eclipse Collections 的提交者。