Author SHA1 Message Date
YuCheng Hu 27af9e2f8c Submit all code for code-java-string branch 2022-07-15 13:10:35 -04:00
222 changed files with 3151 additions and 4171 deletions
+1 -9
View File
@@ -9,7 +9,6 @@
<outputRelativeToContentRoot value="true" />
<module name="codebank" />
<module name="core-java-collections-list" />
<module name="spring-data-jpa-repo-2" />
<module name="core-java-streams" />
<module name="assertion-libraries" />
<module name="core-java-io" />
@@ -17,9 +16,8 @@
<module name="core-java-8-2" />
<module name="core-java-numbers" />
<module name="discourse" />
<module name="spring-data-redis" />
<module name="core-java-11-2" />
<module name="core-java-strings" />
<module name="core-java" />
<module name="core-java-11" />
<module name="core-java-collections-list-2" />
<module name="core-java-collections" />
@@ -29,14 +27,8 @@
<module name="junit-5-basics" />
<module name="core-java-collections-3" />
<module name="core-java-collections-2" />
<module name="activejdbc" />
</profile>
</annotationProcessing>
<bytecodeTargetLevel>
<module name="core-java-11-2" target="11" />
<module name="hibernate-annotations" target="1.5" />
<module name="hibernate5" target="11" />
</bytecodeTargetLevel>
</component>
<component name="JavacSettings">
<option name="ADDITIONAL_OPTIONS_OVERRIDE">
-12
View File
@@ -33,20 +33,8 @@
<file url="file://$PROJECT_DIR$/core-java-modules/core-java-streams/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/core-java-modules/core-java-strings/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/core-java-modules/core-java-strings/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/core-java-modules/core-java/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/core-java-modules/core-java/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/core-java-modules/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/core-java-modules/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/persistence-modules/activejdbc/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/persistence-modules/activejdbc/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/persistence-modules/hibernate5/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/persistence-modules/hibernate5/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/persistence-modules/spring-data-jpa-repo-2/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/persistence-modules/spring-data-jpa-repo-2/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/persistence-modules/spring-data-redis/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/persistence-modules/spring-data-redis/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/persistence-modules/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/persistence-modules/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/testing-modules/assertion-libraries/src/main/java" charset="UTF-8" />
-20
View File
@@ -1,31 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="ossez-repo-snapshots" />
<option name="name" value="OSSEZ Private Snapshots" />
<option name="url" value="https://repo.ossez.com/repository/maven-snapshots/" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Central Repository" />
<option name="url" value="https://repo.maven.apache.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="geodb-repo" />
<option name="name" value="GeoDB repository" />
<option name="url" value="http://repo.boundlessgeo.com/main/" />
</remote-repository>
<remote-repository>
<option name="id" value="ossez-repo-releases" />
<option name="name" value="OSSEZ Private Releases" />
<option name="url" value="https://repo.ossez.com/repository/maven-releases/" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Central Repository" />
<option name="url" value="https://repo.ossez.com/repository/maven-public/" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Maven Central repository" />
-7
View File
@@ -1,7 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JpaBuddyIdeaProjectConfig">
<option name="defaultUnitInitialized" value="true" />
<option name="renamerInitialized" value="true" />
</component>
</project>
Generated
-12
View File
@@ -1,12 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="MessDetectorOptionsConfiguration">
<option name="transferred" value="true" />
</component>
<component name="PHPCSFixerOptionsConfiguration">
<option name="transferred" value="true" />
</component>
<component name="PHPCodeSnifferOptionsConfiguration">
<option name="transferred" value="true" />
</component>
</project>
+4 -7
View File
@@ -5,7 +5,7 @@
<br>提供 CWIKI.US 项目中使用的代码
</p>
Java 代码使用的是 JDK 11。
所有的 Java 代码使用的是 JDK 11。
你可以通过单击下面连接后访问我们网站,并且访问我们提供的最新有关 Java 的开发资料。
@@ -25,9 +25,7 @@ Java 代码使用的是 JDK 11。
# 公众平台
我们建议您通过社区论坛来和我们进行沟通,请关注我们公众平台上的账号。
扫描 QR 关注我们的微信公众号和头条号。
我们建议您通过社区论坛来和我们进行沟通,请关注我们公众平台上的账号
## 微信公众号
@@ -35,10 +33,9 @@ Java 代码使用的是 JDK 11。
## 头条号
![](https://cdn.ossez.com/img/cwikius/cwikus-qr-toutiao.png)
我们也在头条号上创建了我们的公众号,请扫描下面的 QR 关注我们的头条号。
## 知乎
请关注我们的知乎:https://www.zhihu.com/people/huyuchengus
![](https://cdn.ossez.com/img/cwikius/cwikus-qr-toutiao.png)
## 快速导航
-1
View File
@@ -4,7 +4,6 @@
## 相关文章
- [J2EE,Java EE,Jakarta EE 命名之间的恩恩怨怨](https://www.ossez.com/t/j2ee-java-ee-jakarta-ee/14382)
- [Understanding the NumberFormatException in Java](https://www.baeldung.com/java-number-format-exception)
@@ -3,6 +3,7 @@
本模块中包含有关 Java 11 核心新增功能的的一些文章
### 相关文章
- [Java 8 开始新增的 Optional 类](https://www.ossez.com/t/java-8-optional/13964)
- [Guide to Java Reflection](http://www.baeldung.com/java-reflection)
- [Guide to Java 8’s Collectors](https://www.baeldung.com/java-8-collectors)
- [New Features in Java 11](https://www.baeldung.com/java-11-new-features)
+1 -1
View File
@@ -18,7 +18,7 @@
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${version.guava}</version>
<version>${guava.version}</version>
</dependency>
<dependency>
<groupId>org.mock-server</groupId>
-1
View File
@@ -14,4 +14,3 @@ This module contains articles about Java 11 core features
- [Negate a Predicate Method Reference with Java 11](https://www.baeldung.com/java-negate-predicate-method-reference)
- [Benchmark JDK Collections vs Eclipse Collections](https://www.baeldung.com/jdk-collections-vs-eclipse-collections)
- [Pre-compile Regex Patterns Into Pattern Objects](https://www.baeldung.com/java-regex-pre-compile)
+1 -1
View File
@@ -20,7 +20,7 @@
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${version.guava}</version>
<version>${guava.version}</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
+2 -2
View File
@@ -2,12 +2,12 @@
本模块中包含有关 Java 8 核心新增功能特性的一些文章。
### 相关文章
### 相关文章
- [Run a Java Application from the Command Line](https://www.baeldung.com/java-run-jar-with-arguments)
- [Java 8 Stream skip() vs limit()](https://www.baeldung.com/java-stream-skip-vs-limit)
- [Guide to Java BiFunction Interface](https://www.baeldung.com/java-bifunction-interface)
- [Interface With Default Methods vs Abstract Class](https://www.baeldung.com/java-interface-default-method-vs-abstract-class)
- [Convert Between Byte Array and UUID in Java](https://www.baeldung.com/java-byte-array-to-uuid)
- [Create a Simple “Rock-Paper-Scissors” Game in Java](https://www.baeldung.com/java-rock-paper-scissors)
- [Java 8 开始新增的 Optional 类](https://www.ossez.com/t/java-8-optional/13964)
- [[<-- Java 8 核心(第 1 部分)]](/core-java-modules/core-java-8)
@@ -29,7 +29,7 @@
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${version.log4j}</version>
<version>${log4j.version}</version>
</dependency>
<!-- test scoped -->
<dependency>
+2 -2
View File
@@ -71,12 +71,12 @@
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${version.log4j}</version>
<version>${log4j.version}</version>
</dependency>
<dependency> <!-- needed to bridge to slf4j for projects that use the log4j APIs directly -->
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
<version>${version.slf4j}</version>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
@@ -0,0 +1,21 @@
package com.ossez.java9.compactstring;
import java.util.List;
import java.util.stream.IntStream;
import static java.util.stream.Collectors.toList;
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());
long totalTime = System.currentTimeMillis() - startTime;
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());
totalTime = System.currentTimeMillis() - startTime;
System.out.println("Created string of length " + appended.length() + " in " + totalTime + " ms.");
}
}
@@ -0,0 +1,21 @@
package com.ossez.localization;
import java.text.ParseException;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
public class App {
/**
* Runs all available formatter
* @throws ParseException
*/
public static void main(String[] args) {
List<Locale> locales = Arrays.asList(new Locale[] { Locale.UK, Locale.ITALY, Locale.FRANCE, Locale.forLanguageTag("pl-PL") });
Localization.run(locales);
JavaSEFormat.run(locales);
ICUFormat.run(locales);
}
}
@@ -0,0 +1,29 @@
package com.ossez.localization;
import com.ibm.icu.text.MessageFormat;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
public class ICUFormat {
public static String getLabel(Locale locale, Object[] data) {
ResourceBundle bundle = ResourceBundle.getBundle("formats", locale);
String format = bundle.getString("label-icu");
MessageFormat formatter = new MessageFormat(format, locale);
return formatter.format(data);
}
public static void run(List<Locale> locales) {
System.out.println("ICU formatter");
locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { "Alice", "female", 0 })));
locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { "Alice", "female", 1 })));
locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { "Alice", "female", 2 })));
locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { "Alice", "female", 3 })));
locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { "Bob", "male", 0 })));
locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { "Bob", "male", 1 })));
locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { "Bob", "male", 2 })));
locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { "Bob", "male", 3 })));
}
}
@@ -0,0 +1,24 @@
package com.ossez.localization;
import java.text.MessageFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
public class JavaSEFormat {
public static String getLabel(Locale locale, Object[] data) {
ResourceBundle bundle = ResourceBundle.getBundle("formats", locale);
final String pattern = bundle.getString("label");
final MessageFormat formatter = new MessageFormat(pattern, locale);
return formatter.format(data);
}
public static void run(List<Locale> locales) {
System.out.println("Java formatter");
final Date date = new Date(System.currentTimeMillis());
locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { date, "Alice", 0 })));
locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { date, "Alice", 2 })));
}
}
@@ -0,0 +1,18 @@
package com.ossez.localization;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
public class Localization {
public static String getLabel(Locale locale) {
final ResourceBundle bundle = ResourceBundle.getBundle("messages", locale);
return bundle.getString("label");
}
public static void run(List<Locale> locales) {
locales.forEach(locale -> System.out.println(getLabel(locale)));
}
}
@@ -0,0 +1,198 @@
package com.ossez.stringperformance;
import com.google.common.base.Splitter;
import org.apache.commons.lang3.StringUtils;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
@BenchmarkMode(Mode.SingleShotTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@Measurement(batchSize = 100000, iterations = 10)
@Warmup(batchSize = 100000, iterations = 10)
@State(Scope.Thread)
public class StringPerformance {
protected String baeldung = "baeldung";
protected String longString = "Hello baeldung, I am a bit longer than other Strings";
protected String formatString = "hello %s, nice to meet you";
protected String formatDigit = "%d";
protected String emptyString = " ";
protected String result = "";
protected int sampleNumber = 100;
protected Pattern spacePattern = Pattern.compile(emptyString);
protected Pattern longPattern = Pattern.compile(longString);
protected List<String> stringSplit = new ArrayList<>();
protected List<String> stringTokenizer = new ArrayList<>();
@Benchmark
public String benchmarkStringDynamicConcat() {
result += baeldung;
return result;
}
@Benchmark
public StringBuilder benchmarkStringBuilder() {
StringBuilder stringBuilder = new StringBuilder(result);
stringBuilder.append(baeldung);
return stringBuilder;
}
@Benchmark
public StringBuffer benchmarkStringBuffer() {
StringBuffer stringBuffer = new StringBuffer(result);
stringBuffer.append(baeldung);
return stringBuffer;
}
@Benchmark
public String benchmarkStringConstructor() {
String result = new String("baeldung");
return result;
}
@Benchmark
public String benchmarkStringLiteral() {
String result = "baeldung";
return result;
}
@Benchmark
public String benchmarkStringFormat_s() {
return String.format(formatString, baeldung);
}
@Benchmark
public String benchmarkStringConcat() {
result = result.concat(baeldung);
return result;
}
@Benchmark
public String benchmarkStringIntern() {
return baeldung.intern();
}
@Benchmark
public String benchmarkStringReplace() {
return longString.replace("average", " average !!!");
}
@Benchmark
public String benchmarkStringUtilsReplace() {
return StringUtils.replace(longString, "average", " average !!!");
}
@Benchmark
public List<String> benchmarkGuavaSplitter() {
return Splitter.on(" ").trimResults()
.omitEmptyStrings()
.splitToList(longString);
}
@Benchmark
public String [] benchmarkStringSplit() {
return longString.split(emptyString);
}
@Benchmark
public String [] benchmarkStringSplitPattern() {
return spacePattern.split(longString, 0);
}
@Benchmark
public List benchmarkStringTokenizer() {
StringTokenizer st = new StringTokenizer(longString);
while (st.hasMoreTokens()) {
stringTokenizer.add(st.nextToken());
}
return stringTokenizer;
}
@Benchmark
public List benchmarkStringIndexOf() {
int pos = 0, end;
while ((end = longString.indexOf(' ', pos)) >= 0) {
stringSplit.add(longString.substring(pos, end));
pos = end + 1;
}
//Add last token of string
stringSplit.add(longString.substring(pos));
return stringSplit;
}
@Benchmark
public String benchmarkIntegerToString() {
return Integer.toString(sampleNumber);
}
@Benchmark
public String benchmarkStringValueOf() {
return String.valueOf(sampleNumber);
}
@Benchmark
public String benchmarkStringConvertPlus() {
return sampleNumber + "";
}
@Benchmark
public String benchmarkStringFormat_d() {
return String.format(formatDigit, sampleNumber);
}
@Benchmark
public boolean benchmarkStringEquals() {
return longString.equals(baeldung);
}
@Benchmark
public boolean benchmarkStringEqualsIgnoreCase() {
return longString.equalsIgnoreCase(baeldung);
}
@Benchmark
public boolean benchmarkStringMatches() {
return longString.matches(baeldung);
}
@Benchmark
public boolean benchmarkPrecompiledMatches() {
return longPattern.matcher(baeldung).matches();
}
@Benchmark
public int benchmarkStringCompareTo() {
return longString.compareTo(baeldung);
}
@Benchmark
public boolean benchmarkStringIsEmpty() {
return longString.isEmpty();
}
@Benchmark
public boolean benchmarkStringLengthZero() {
return longString.length() == 0;
}
public static void main(String[] args) throws Exception {
Options options = new OptionsBuilder()
.include(StringPerformance.class.getSimpleName()).threads(1)
.forks(1).shouldFailOnError(true)
.shouldDoGC(true)
.jvmArgs("-server").build();
new Runner(options).run();
}
}
File diff suppressed because one or more lines are too long
@@ -0,0 +1,2 @@
label=On {0, date, short} {1} has sent you {2, choice, 0#no messages|1#a message|2#two messages|2<{2,number,integer} messages}.
label-icu={0} has sent you {2, plural, =0 {no messages} =1 {a message} other {{2, number, integer} messages}}.
@@ -0,0 +1,2 @@
label={0, date, short}, {1}{2, choice, 0# ne|0<} vous a envoy\u00e9 {2, choice, 0#aucun message|1#un message|2#deux messages|2<{2,number,integer} messages}.
label-icu={0} {2, plural, =0 {ne } other {}}vous a envoy\u00e9 {2, plural, =0 {aucun message} =1 {un message} other {{2, number, integer} messages}}.
@@ -0,0 +1,2 @@
label={0, date, short} {1} ti ha inviato {2, choice, 0#nessun messagio|1#un messaggio|2#due messaggi|2<{2, number, integer} messaggi}.
label-icu={0} {2, plural, =0 {non } other {}}ti ha inviato {2, plural, =0 {nessun messaggio} =1 {un messaggio} other {{2, number, integer} messaggi}}.
@@ -0,0 +1,2 @@
label=W {0, date, short} {1}{2, choice, 0# nie|0<} wys\u0142a\u0142a ci {2, choice, 0#\u017Cadnych wiadomo\u015Bci|1#wiadomo\u015B\u0107|2#dwie wiadomo\u015Bci|2<{2, number, integer} wiadomo\u015Bci}.
label-icu={0} {2, plural, =0 {nie } other {}}{1, select, male {wys\u0142a\u0142} female {wys\u0142a\u0142a} other {wys\u0142a\u0142o}} ci {2, plural, =0 {\u017Cadnej wiadomo\u015Bci} =1 {wiadomo\u015B\u0107} other {{2, number, integer} wiadomo\u015Bci}}.
@@ -0,0 +1 @@
label=Alice has sent you a message.
@@ -0,0 +1 @@
label=Alice vous a envoy\u00e9 un message.
@@ -0,0 +1 @@
label=Alice ti ha inviato un messaggio.
@@ -0,0 +1 @@
label=Alice wys\u0142a\u0142a ci wiadomo\u015B\u0107.
@@ -0,0 +1,4 @@
Get busy living
or
get busy dying.
--Stephen King

Some files were not shown because too many files have changed in this diff Show More