如何在Java8中将字符串数组转换为长整型数组?

huangapple go评论76阅读模式
英文:

How to convert a String Array to a Long Array in a Java8 one-liner?

问题

我正在解决HackerRank上的“计数三元组”问题。
https://www.hackerrank.com/challenges/count-triplets-1/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=dictionaries-hashmaps
它说我必须使用一个数组,尽管HC将其转换为List。
我想将其转换为数组而不是列表。

public static void main(String[] args) throws IOException {
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
    BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

    String[] nr = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");

    int n = Integer.parseInt(nr[0]);

    long r = Long.parseLong(nr[1]);

    // 将以下代码从列表(List)转换为数组(Array)
    long[] arr = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
            .mapToLong(Long::parseLong)
            .toArray();

    long ans = countTriplets(arr, r);

    bufferedWriter.write(String.valueOf(ans));
    bufferedWriter.newLine();

    bufferedReader.close();
    bufferedWriter.close();
}
英文:

I'm solving the "Count Triplets" problem on HackerRank.
https://www.hackerrank.com/challenges/count-triplets-1/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=dictionaries-hashmaps
It says that I must work with an array, although HC converts it to a List.
I want to convert in into an array instead of a list.

public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        String[] nr = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");

        int n = Integer.parseInt(nr[0]);

        long r = Long.parseLong(nr[1]);

        **List<Long> arr = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
                .map(Long::parseLong)
                .collect(toList());**

        long ans = countTriplets(arr, r);

        bufferedWriter.write(String.valueOf(ans));
        bufferedWriter.newLine();

        bufferedReader.close();
        bufferedWriter.close();
    }

答案1

得分: 2

Long[] arr = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
                .map(Long::parseLong)
                .collect(Collectors.toList())
                .toArray(Long[]::new);

# Update:

Courtesy [Holger](https://stackoverflow.com/questions/63298831/how-to-convert-a-string-array-to-a-long-array-in-a-java8-one-liner/63298923#comment111936073_63298923)

There is no need to collect into a `List` as shown above. You can do it simply as

Long[] arr = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
                .map(Long::parseLong)
                .toArray(Long[]::new);

Alternatively,

long[] arr = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
                .mapToLong(Long::parseLong) 
                .toArray()
英文:

Do it as follows:

Long[] arr = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
                .map(Long::parseLong)
                .collect(Collectors.toList())
                .toArray(Long[]::new);

Update:

Courtesy Holger

There is no need to collect into a List as shown above. You can do it simply as

Long[] arr = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
                .map(Long::parseLong)
                .toArray(Long[]::new);

Alternatively,

long[] arr = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
                .mapToLong(Long::parseLong) 
                .toArray()

答案2

得分: 0

使用.toArray(Long[]::new)替代.collect(Collectors.toList())

Long[] arr = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
                   .map(Long::parseLong)
                   .toArray(Long[]::new);
英文:

Use .toArray(Long[]::new) instead of .collect(Collectors.toList())

Long[] arr = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
                .map(Long::parseLong)
                .toArray(Long[]::new);

答案3

得分: 0

我将执行此操作

Arrays.stream(strArray).map(Long::valueOf).toArray(Long[]::new)
英文:

I will do this

Arrays.stream(strArray).map(Long::valueOf).toArray(Long[]::new)

huangapple
  • 本文由 发表于 2020年8月7日 17:18:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63298831.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定