- [Java 9 中的字符串(String)压缩](https://www.ossez.com/t/java-9-string/14024)

This commit is contained in:
YuCheng Hu 2022-07-05 10:14:24 -04:00
parent 08abc1855a
commit b9bf30f350
1 changed files with 4 additions and 8 deletions

View File

@ -9,17 +9,13 @@ public class CompactStringDemo {
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
List strings = IntStream.rangeClosed(1, 10_000_000)
.mapToObj(Integer::toString).collect(toList());
List strings = IntStream.rangeClosed(1, 10_000_000).mapToObj(Integer::toString).collect(toList());
long totalTime = System.currentTimeMillis() - startTime;
System.out.println("Generated " + strings.size() + " strings in "
+ totalTime + " ms.");
System.out.println("Generated " + strings.size() + " strings in " + totalTime + " ms.");
startTime = System.currentTimeMillis();
String appended = (String) strings.stream().limit(100_000)
.reduce("", (left, right) -> left.toString() + right.toString());
String appended = (String) strings.stream().limit(100_000).reduce("", (left, right) -> left.toString() + right.toString());
totalTime = System.currentTimeMillis() - startTime;
System.out.println("Created string of length " + appended.length()
+ " in " + totalTime + " ms.");
System.out.println("Created string of length " + appended.length() + " in " + totalTime + " ms.");
}
}