猿问

如何在 Java 中映射数组?

我有一个字符串数组


String[] suffixes = getSuffixes();

我想要一个基于旧数组的新数组,并附加前缀。我会在 Ruby、JavaScript、Perl、Lisp 等中做这样的事情:


// pseudocode for what I would like


String[] full_words = suffixes.map(suffix => "re" + suffix);

我的应用程序中仅限于数组。(这是学校作业的一小部分,我不允许使用 Collections 库。)在 Java 8 中有什么方法可以做到这一点?


www说
浏览 218回答 1
1回答

慕桂英546537

String [] full_suffixes = Arrays.stream(suffixes)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .map(suffix -> "re" + suffix)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .toArray(size -> new String[size])“老式”循环太冗长了:String [] full_suffixes = new String[suffixes.length];for(int i = 0; i < suffixes.length; i++) {&nbsp; &nbsp; full_suffixes[i] = "re" + suffixes[i];}要解决“太快”的评论:import java.util.Arrays;public class Test {&nbsp; &nbsp; static int N = 10000;&nbsp; &nbsp; public static void main(String [] args) throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; if(args.length > 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; N = Integer.parseInt(args[0]);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; String [] array = new String[100000];&nbsp; &nbsp; &nbsp; &nbsp; for(int i = 0; i < array.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array[i] = "abcdef" + i;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; long start = System.currentTimeMillis();&nbsp; &nbsp; &nbsp; &nbsp; fancy(array);&nbsp; &nbsp; &nbsp; &nbsp; System.err.println("Fancy took " + (System.currentTimeMillis() - start) + "ms");&nbsp; &nbsp; &nbsp; &nbsp; start = System.currentTimeMillis();&nbsp; &nbsp; &nbsp; &nbsp; oldSchool(array);&nbsp; &nbsp; &nbsp; &nbsp; System.err.println("Loop took " + (System.currentTimeMillis() - start) + "ms");&nbsp; &nbsp; }&nbsp; &nbsp; public static void fancy(String [] array) {&nbsp; &nbsp; &nbsp; &nbsp; for(int i = 0; i < N; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String [] full_suffixes = Arrays.stream(array)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .map(suffix -> "re" + suffix)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .toArray(size -> new String[size]);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public static void oldSchool(String [] array) {&nbsp; &nbsp; &nbsp; &nbsp; for(int i = 0; i < N; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String [] full_suffixes = new String[array.length];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int j = 0; j < array.length; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; full_suffixes[j] = "re" + array[j];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}运行(jdk 1.8.0_60):$ java Test 100Fancy took 502msLoop took 398ms$ java Test 1000Fancy took 3067msLoop took 2227ms$ java Test 5000Fancy took 13288msLoop took 11494ms$ java Test 10000Fancy took 27744msLoop took 27446ms我不会说这更快,但不幸的是,在当前的流框架实现中,性能和代码清晰度之间存在权衡。编辑:为了解决关于 map 与 for 循环的讨论——这与要输入的行数或字符数无关,也许我也被具有高阶函数的语言所宠坏,但对我来说,转换 a集合(这就是 map 的作用)并将我的大脑放入其大小以及如何获取/分配元素的细节中。另外值得一提的是,Java 流框架仍然落后。
随时随地看视频慕课网APP

相关分类

Java
我要回答