Move articles out of java-strings part5
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.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.baeldung.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.baeldung.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.baeldung.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.baeldung.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,67 @@
|
||||
package com.baeldung.multiline;
|
||||
|
||||
import com.google.common.base.Joiner;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
public class MultiLineString {
|
||||
|
||||
String newLine = System.getProperty("line.separator");
|
||||
|
||||
public String stringConcatenation() {
|
||||
return "Get busy living"
|
||||
.concat(newLine)
|
||||
.concat("or")
|
||||
.concat(newLine)
|
||||
.concat("get busy dying.")
|
||||
.concat(newLine)
|
||||
.concat("--Stephen King");
|
||||
}
|
||||
|
||||
public String stringJoin() {
|
||||
return String.join(newLine,
|
||||
"Get busy living",
|
||||
"or",
|
||||
"get busy dying.",
|
||||
"--Stephen King");
|
||||
}
|
||||
|
||||
public String stringBuilder() {
|
||||
return new StringBuilder()
|
||||
.append("Get busy living")
|
||||
.append(newLine)
|
||||
.append("or")
|
||||
.append(newLine)
|
||||
.append("get busy dying.")
|
||||
.append(newLine)
|
||||
.append("--Stephen King")
|
||||
.toString();
|
||||
}
|
||||
|
||||
public String stringWriter() {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
PrintWriter printWriter = new PrintWriter(stringWriter);
|
||||
printWriter.println("Get busy living");
|
||||
printWriter.println("or");
|
||||
printWriter.println("get busy dying.");
|
||||
printWriter.println("--Stephen King");
|
||||
return stringWriter.toString();
|
||||
}
|
||||
|
||||
public String guavaJoiner() {
|
||||
return Joiner.on(newLine).join(ImmutableList.of("Get busy living",
|
||||
"or",
|
||||
"get busy dying.",
|
||||
"--Stephen King"));
|
||||
}
|
||||
|
||||
public String loadFromFile() throws IOException {
|
||||
return new String(Files.readAllBytes(Paths.get("src/main/resources/stephenking.txt")));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,196 @@
|
||||
package com.baeldung.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;
|
||||
}
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -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,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user