英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论