java JAVA中不循环将字符串分解为Long数组或Long列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9994180/
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-30 23:10:58  来源:igfitidea点击:

Decompose a String into Array of Long or List of Long without Loop in JAVA

java

提问by Lalit Bhudiya

I want to decompose a String array into Long array or List. I don't want to use Loop.

我想将一个字符串数组分解为长数组或列表。我不想使用循环。

Is there any Java Method to do this.

是否有任何 Java 方法可以做到这一点。

回答by Andreas Dolk

There is no O(1) operation to "convert" a String[](with numeric strings) to a long[]. It will always be O(n), if the loop visible or hidden in some thirdparty method.

没有 O(1) 操作将 a String[](带有数字字符串)“转换”为 a long[]。如果循环在某些第三方方法中可见或隐藏,则它将始终为 O(n)。

If you don't want to "see" the loop, simply implement a method

如果您不想“看到”循环,只需实现一个方法

Long[] pseudoOneStepConversion(numbers);

and implement

并实施

privat Long[] pseudoOneStepConversion(String[] numbers) {
  Long[] result = new long[numbers.length];
  for (int i = 0; i < numbers.length; i++)
     result[i] = Long.parseLong(numbers[i]);
  return result;
}


We can do it recursively too - it is still O(n), less performant and doesn't look like a loop:

我们也可以递归地做——它仍然是 O(n),性能较低,看起来不像一个循环:

public static void main(String[] args) {
    List<Long> target = new ArrayList<Long>();
    copy(new String[]{"1", "2", "3"}, target, 0);
    System.out.println(target);
}

private static void copy(String[] source, List<Long> target, int index) {
    if (index == source.length)
        return;
    target.add(Long.parseLong(source[index]));
    copy(source, target, index+1);
}

Note - because I start getting downvotes for the recursion example: It is purely academic and not inteded for use in production code - thought, that was clear ;)

注意 - 因为我开始对递归示例投反对票:它纯粹是学术性的,不打算用于生产代码 - 想,这很清楚;)

回答by vklidu

Simplified Eugene answer with Guava library. Since Guava 16.0.

使用 Guava 库简化 Eugene 答案。自番石榴 16.0.

List<Long> longList = Lists.transform(Arrays.asList(stringArray), Longs.stringConverter());

Update:Solution with Java 8, without 3th party libraries:

更新:使用 Java 8 的解决方案,没有第 3 方库:

List<Long> longList = Stream.of(stringArray).map(Long::valueOf).collect(Collectors.toList());

回答by Eugene Kuleshov

With a little help of 3rd party librariesyou can avoid coding loops in your own code, but there would be a loop somewhere under the hood. For example:

3rd 方库的帮助下,您可以避免在您自己的代码中编码循环,但在幕后某处会有一个循环。例如:

List<String> stringList = Arrays.asList(stringArray);
List<Long> longList = Lists.transform(stringList, new Function<String, Long>() {
   public Long apply(String s) {
      return Long.valueOf(s);
   }
});

Classes Listsand Functionare from Guava library.

列表函数来自番石榴库。

回答by talnicolas

No there is no way to do this without a loop (even if you don't code explicitly a loop, the method you will call will use one), unless you now the number of longvalues contained in the Stringand add them manually to your List.

不,没有循环就没有办法做到这一点(即使您没有明确地编写循环代码,您将调用的方法也将使用一个),除非您现在将 中long包含的值的数量String手动添加到您的List.

回答by Zapodot

Your best option is Guavas Lists.transformfunction.

您最好的选择是 Guavas Lists.transform函数。

String[] stringArray = {"1999", "20000"};
List<String> stringList = Arrays.asList(stringArray);
List<Long> longList = Lists.transform(stringList, 
                                      new Function<String, Long>() {
                                              Long apply(String value) {
                                                return Long.valueOf(value);
                                      }
                       });

Note: Guava will of course have to do a loop to achieve this, but there are no loops in your code.

注意:Guava 当然必须执行循环才能实现此目的,但您的代码中没有循环。

回答by UdayKiran Pulipati

Split String value with comma and Converting String Array to Wrapper Long Array / Primitive long array using Java 8.

使用 Java 8 使用逗号拆分字符串值并将字符串数组转换为包装长数组/原始长数组。

String values = "2846, 4514, 8495, 4587, 5772, 3813, 6947, 3186";
Long[] longArray = Arrays.stream(values.split(","))
                                .map(String::trim)
                                .map(Long::valueOf)
                                .toArray(Long[]::new);//Converting String array to Long array
long[] longPrimitiveArray = Arrays.stream(values.split(","))
                                        .map(String::trim)
                                        .mapToLong(Long::valueOf)
                                        .toArray();//Converting String array to long array