Merge pull request 'core-java-strings 字符串压缩 Compact' (#43) from core-java-strings into main
Reviewed-on: https://src.ossez.com/cwiki-us-docs/java-tutorials/pulls/43
This commit is contained in:
commit
cd46aea7ac
@ -1,6 +1,7 @@
|
||||
=========
|
||||
## Annotation(注解)
|
||||
Java 注解(Annotation)又称 Java 标注,是 JDK5.0 引入的一种注释机制。
|
||||
|
||||
## Core Java Cookbooks and Examples
|
||||
Java 语言中的类、方法、变量、参数和包等都可以被标注。和 Javadoc 不同,Java 标注可以通过反射获取标注内容。在编译器生成类文件时,标注可以被嵌入到字节码中。Java 虚拟机可以保留标注内容,在运行时可以获取到标注内容 。 当然它也支持自定义 Java 标注。
|
||||
|
||||
### 相关的文章:
|
||||
- [Java @Override Annotation](https://www.baeldung.com/java-override)
|
||||
|
@ -43,7 +43,7 @@ public class NumberFormatExceptionTest {
|
||||
|
||||
|
||||
// Integer aIntegerObj = new Integer("one");
|
||||
// Double doubleDecimalObj = new Double("two.2");
|
||||
// Double doubleDecimalObj = new Double("two.2");
|
||||
}
|
||||
|
||||
|
||||
|
@ -3,14 +3,15 @@
|
||||
本模块中包含有关 Java 字符串(String)有关的文章。
|
||||
|
||||
### 相关文章
|
||||
- [Java 使用 char[] Array 还是 String 存储字符串](https://www.ossez.com/t/java-char-array-string/14015)
|
||||
- [Compact Strings in Java 9](https://www.baeldung.com/java-9-compact-string)
|
||||
- [Java 使用 char[] Array 还是 String 存储字符串密码](https://www.ossez.com/t/java-char-array-string/14015)
|
||||
- [Java 9 中的字符串(String)压缩](https://www.ossez.com/t/java-9-string/14024)
|
||||
- [String Not Empty Test Assertions in Java](https://www.baeldung.com/java-assert-string-not-empty)
|
||||
- [String Performance Hints](https://www.baeldung.com/java-string-performance)
|
||||
- [Java Localization – Formatting Messages](https://www.baeldung.com/java-localization-messages-formatting)
|
||||
- [Java – Generate Random String](https://www.baeldung.com/java-random-string)
|
||||
- [Java String Interview Questions and Answers](https://www.baeldung.com/java-string-interview-questions)
|
||||
- [Java Multi-line String](https://www.baeldung.com/java-multiline-string)
|
||||
- [Guide to Java String Pool](https://www.baeldung.com/java-string-pool)
|
||||
- [Java 中的 String Pool 简介](https://www.ossez.com/t/java-string-pool/14017)
|
||||
- [Fixing “constant string too long” Build Error](https://www.baeldung.com/java-constant-string-too-long-error)
|
||||
- [Java 对象如何安全的 toString](https://www.ossez.com/t/java-tostring/14000)
|
||||
- [编程常用的几种字符编码](https://www.ossez.com/t/topic/14022)
|
||||
|
@ -5,21 +5,22 @@ import java.util.stream.IntStream;
|
||||
|
||||
import static java.util.stream.Collectors.toList;
|
||||
|
||||
/**
|
||||
* Java 9 String Compact testing
|
||||
* <p>
|
||||
* <p><a href="https://www.ossez.com/t/java-9-string/14024">https://www.ossez.com/t/java-9-string/14024</a></p>
|
||||
*/
|
||||
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.");
|
||||
}
|
||||
}
|
||||
|
@ -9,7 +9,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
/**
|
||||
* Java Password by using String or Char array
|
||||
* <p>
|
||||
* * <p><a href="https://www.ossez.com/t/java-char-array-string/14015">https://www.ossez.com/t/java-char-array-string/14015</a></p>
|
||||
* <p><a href="https://www.ossez.com/t/java-char-array-string/14015">https://www.ossez.com/t/java-char-array-string/14015</a></p>
|
||||
*/
|
||||
public class PasswordStoreExamplesUnitTest {
|
||||
|
||||
|
@ -2,6 +2,7 @@ package com.ossez.multiline;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
@ -3,37 +3,44 @@ package com.ossez.stringpool;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class StringPoolUnitTest {
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(StringPoolUnitTest.class);
|
||||
|
||||
@Test
|
||||
public void whenCreatingConstantStrings_thenTheirAddressesAreEqual() {
|
||||
String constantString1 = "Baeldung";
|
||||
String constantString2 = "Baeldung";
|
||||
String constantString1 = "HoneyMoose";
|
||||
String constantString2 = "HoneyMoose";
|
||||
|
||||
assertThat(constantString1).isSameAs(constantString2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCreatingStringsWithTheNewOperator_thenTheirAddressesAreDifferent() {
|
||||
String newString1 = new String("Baeldung");
|
||||
String newString2 = new String("Baeldung");
|
||||
String newString1 = new String("HoneyMoose");
|
||||
String newString2 = new String("HoneyMoose");
|
||||
|
||||
assertThat(newString1).isNotSameAs(newString2);
|
||||
|
||||
logger.info("newString1 Address: {}", System.identityHashCode(newString1));
|
||||
logger.info("newString2 Address: {}", System.identityHashCode(newString2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenComparingConstantAndNewStrings_thenTheirAddressesAreDifferent() {
|
||||
String constantString = "Baeldung";
|
||||
String newString = new String("Baeldung");
|
||||
String constantString = "HoneyMoose";
|
||||
String newString = new String("HoneyMoose");
|
||||
|
||||
assertThat(constantString).isNotSameAs(newString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenInterningAStringWithIdenticalValueToAnother_thenTheirAddressesAreEqual() {
|
||||
String constantString = "interned Baeldung";
|
||||
String newString = new String("interned Baeldung");
|
||||
String constantString = "interned HoneyMoose";
|
||||
String newString = new String("interned HoneyMoose");
|
||||
|
||||
assertThat(constantString).isNotSameAs(newString);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user