Move articles out of java-strings part4
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
package com.baeldung.isnumeric;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.openjdk.jmh.annotations.Benchmark;
|
||||
import org.openjdk.jmh.annotations.BenchmarkMode;
|
||||
import org.openjdk.jmh.annotations.Mode;
|
||||
import org.openjdk.jmh.annotations.OutputTimeUnit;
|
||||
import org.openjdk.jmh.annotations.Scope;
|
||||
import org.openjdk.jmh.annotations.State;
|
||||
import org.openjdk.jmh.runner.Runner;
|
||||
import org.openjdk.jmh.runner.RunnerException;
|
||||
import org.openjdk.jmh.runner.options.Options;
|
||||
import org.openjdk.jmh.runner.options.OptionsBuilder;
|
||||
|
||||
public class Benchmarking {
|
||||
public static void main(String[] args) throws RunnerException {
|
||||
Options opt = new OptionsBuilder().include(Benchmarking.class.getSimpleName())
|
||||
.forks(1)
|
||||
.build();
|
||||
|
||||
new Runner(opt).run();
|
||||
}
|
||||
|
||||
@State(Scope.Thread)
|
||||
public static class ExecutionPlan {
|
||||
public String number = Integer.toString(Integer.MAX_VALUE);
|
||||
public boolean isNumber = false;
|
||||
public IsNumeric isNumeric = new IsNumeric();
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
public void usingCoreJava(ExecutionPlan plan) {
|
||||
plan.isNumber = plan.isNumeric.usingCoreJava(plan.number);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
public void usingRegularExpressions(ExecutionPlan plan) {
|
||||
plan.isNumber = plan.isNumeric.usingRegularExpressions(plan.number);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
public void usingNumberUtils_isCreatable(ExecutionPlan plan) {
|
||||
plan.isNumber = plan.isNumeric.usingNumberUtils_isCreatable(plan.number);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
public void usingNumberUtils_isParsable(ExecutionPlan plan) {
|
||||
plan.isNumber = plan.isNumeric.usingNumberUtils_isParsable(plan.number);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
public void usingStringUtils_isNumeric(ExecutionPlan plan) {
|
||||
plan.isNumber = plan.isNumeric.usingStringUtils_isNumeric(plan.number);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
public void usingStringUtils_isNumericSpace(ExecutionPlan plan) {
|
||||
plan.isNumber = plan.isNumeric.usingStringUtils_isNumericSpace(plan.number);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.isnumeric;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class CheckIntegerInput {
|
||||
|
||||
public static void main(String[] args) {
|
||||
try (Scanner scanner = new Scanner(System.in)) {
|
||||
System.out.println("Enter an integer : ");
|
||||
|
||||
if (scanner.hasNextInt()) {
|
||||
System.out.println("You entered : " + scanner.nextInt());
|
||||
} else {
|
||||
System.out.println("The input is not an integer");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.baeldung.isnumeric;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.lang3.math.NumberUtils;
|
||||
|
||||
public class IsNumeric {
|
||||
public boolean usingCoreJava(String strNum) {
|
||||
try {
|
||||
Double.parseDouble(strNum);
|
||||
} catch (NumberFormatException | NullPointerException nfe) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean usingRegularExpressions(String strNum) {
|
||||
return strNum.matches("-?\\d+(\\.\\d+)?");
|
||||
}
|
||||
|
||||
public boolean usingNumberUtils_isCreatable(String strNum) {
|
||||
return NumberUtils.isCreatable(strNum);
|
||||
}
|
||||
|
||||
public boolean usingNumberUtils_isParsable(String strNum) {
|
||||
return NumberUtils.isParsable(strNum);
|
||||
}
|
||||
|
||||
public boolean usingStringUtils_isNumeric(String strNum) {
|
||||
return StringUtils.isNumeric(strNum);
|
||||
}
|
||||
|
||||
public boolean usingStringUtils_isNumericSpace(String strNum) {
|
||||
return StringUtils.isNumericSpace(strNum);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.baeldung.isnumeric;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
public class IsNumericDriver {
|
||||
private static Logger LOG = Logger.getLogger(IsNumericDriver.class);
|
||||
|
||||
private static IsNumeric isNumeric = new IsNumeric();
|
||||
|
||||
public static void main(String[] args) {
|
||||
LOG.info("Testing all methods...");
|
||||
|
||||
boolean res = isNumeric.usingCoreJava("1001");
|
||||
LOG.info("Using Core Java : " + res);
|
||||
|
||||
res = isNumeric.usingRegularExpressions("1001");
|
||||
LOG.info("Using Regular Expressions : " + res);
|
||||
|
||||
res = isNumeric.usingNumberUtils_isCreatable("1001");
|
||||
LOG.info("Using NumberUtils.isCreatable : " + res);
|
||||
|
||||
res = isNumeric.usingNumberUtils_isParsable("1001");
|
||||
LOG.info("Using NumberUtils.isParsable : " + res);
|
||||
|
||||
res = isNumeric.usingStringUtils_isNumeric("1001");
|
||||
LOG.info("Using StringUtils.isNumeric : " + res);
|
||||
|
||||
res = isNumeric.usingStringUtils_isNumericSpace("1001");
|
||||
LOG.info("Using StringUtils.isNumericSpace : " + res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package com.baeldung.newline;
|
||||
|
||||
public class AddingNewLineToString {
|
||||
|
||||
public static void main(String[] args) {
|
||||
String line1 = "Humpty Dumpty sat on a wall.";
|
||||
String line2 = "Humpty Dumpty had a great fall.";
|
||||
String rhyme = "";
|
||||
|
||||
System.out.println("***New Line in a String in Java***");
|
||||
//1. Using "\n"
|
||||
System.out.println("1. Using \\n");
|
||||
rhyme = line1 + "\n" + line2;
|
||||
System.out.println(rhyme);
|
||||
|
||||
//2. Using "\r\n"
|
||||
System.out.println("2. Using \\r\\n");
|
||||
rhyme = line1 + "\r\n" + line2;
|
||||
System.out.println(rhyme);
|
||||
|
||||
//3. Using "\r"
|
||||
System.out.println("3. Using \\r");
|
||||
rhyme = line1 + "\r" + line2;
|
||||
System.out.println(rhyme);
|
||||
|
||||
//4. Using "\n\r" Note that this is not same as "\r\n"
|
||||
// Using "\n\r" is equivalent to adding two lines
|
||||
System.out.println("4. Using \\n\\r");
|
||||
rhyme = line1 + "\n\r" + line2;
|
||||
System.out.println(rhyme);
|
||||
|
||||
//5. Using System.lineSeparator()
|
||||
System.out.println("5. Using System.lineSeparator()");
|
||||
rhyme = line1 + System.lineSeparator() + line2;
|
||||
System.out.println(rhyme);
|
||||
|
||||
//6. Using System.getProperty("line.separator")
|
||||
System.out.println("6. Using System.getProperty(\"line.separator\")");
|
||||
rhyme = line1 + System.getProperty("line.separator") + line2;
|
||||
System.out.println(rhyme);
|
||||
|
||||
System.out.println("***HTML to rendered in a browser***");
|
||||
//1. Line break for HTML using <br>
|
||||
System.out.println("1. Line break for HTML using <br>");
|
||||
rhyme = line1 + "<br>" + line2;
|
||||
System.out.println(rhyme);
|
||||
|
||||
//2. Line break for HTML using “ ”
|
||||
System.out.println("2. Line break for HTML using ");
|
||||
rhyme = line1 + " " + line2;
|
||||
System.out.println(rhyme);
|
||||
|
||||
//3. Line break for HTML using “ ”
|
||||
System.out.println("3. Line break for HTML using ");
|
||||
rhyme = line1 + " " + line2;
|
||||
System.out.println(rhyme);
|
||||
|
||||
//4. Line break for HTML using “
 ;”
|
||||
System.out.println("4. Line break for HTML using ");
|
||||
rhyme = line1 + " " + line2;
|
||||
System.out.println(rhyme);
|
||||
|
||||
//5. Line break for HTML using \n”
|
||||
System.out.println("5. Line break for HTML using \\n");
|
||||
rhyme = line1 + "\n" + line2;
|
||||
System.out.println(rhyme);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.baeldung.streamoperations;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class JoinerSplitter {
|
||||
|
||||
public static String join ( String[] arrayOfString ) {
|
||||
return Arrays.asList(arrayOfString)
|
||||
.stream()
|
||||
.map(x -> x)
|
||||
.collect(Collectors.joining(","));
|
||||
}
|
||||
|
||||
public static String joinWithPrefixPostFix ( String[] arrayOfString ) {
|
||||
return Arrays.asList(arrayOfString)
|
||||
.stream()
|
||||
.map(x -> x)
|
||||
.collect(Collectors.joining(",","[","]"));
|
||||
}
|
||||
|
||||
public static List<String> split ( String str ) {
|
||||
return Stream.of(str.split(","))
|
||||
.map (elem -> new String(elem))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public static List<Character> splitToListOfChar ( String str ) {
|
||||
return str.chars()
|
||||
.mapToObj(item -> (char) item)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public static Map<String, String> arrayToMap(String[] arrayOfString) {
|
||||
return Arrays.asList(arrayOfString)
|
||||
.stream()
|
||||
.map(str -> str.split(":"))
|
||||
.collect(Collectors.<String[], String, String>toMap(str -> str[0], str -> str[1]));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package com.baeldung.substringsearch;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.openjdk.jmh.annotations.*;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Based on https://github.com/tedyoung/indexof-contains-benchmark
|
||||
*/
|
||||
@Fork(5)
|
||||
@State(Scope.Benchmark)
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
public class SubstringSearchPerformanceComparison {
|
||||
|
||||
private String message;
|
||||
|
||||
private Pattern pattern;
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
org.openjdk.jmh.Main.main(args);
|
||||
}
|
||||
|
||||
@Setup
|
||||
public void setup() {
|
||||
message = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum";
|
||||
pattern = Pattern.compile("(?<!\\S)" + "eiusmod" + "(?!\\S)");
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public int indexOf() {
|
||||
return message.indexOf("eiusmod");
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public boolean contains() {
|
||||
return message.contains("eiusmod");
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public boolean containsStringUtilsIgnoreCase() {
|
||||
return StringUtils.containsIgnoreCase(message, "eiusmod");
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public boolean searchWithPattern() {
|
||||
return pattern.matcher(message).find();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.tostring;
|
||||
|
||||
public class Customer {
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.tostring;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class CustomerArrayToString extends Customer {
|
||||
private Order[] orders;
|
||||
|
||||
public Order[] getOrders() {
|
||||
return orders;
|
||||
}
|
||||
|
||||
public void setOrders(Order[] orders) {
|
||||
this.orders = orders;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Customer [orders=" + Arrays.toString(orders) + ", getFirstName()=" + getFirstName() + ", getLastName()=" + getLastName() + "]";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.tostring;
|
||||
|
||||
public class CustomerComplexObjectToString extends Customer {
|
||||
private Order order;
|
||||
|
||||
public Order getOrder() {
|
||||
return order;
|
||||
}
|
||||
|
||||
public void setOrder(Order order) {
|
||||
this.order = order;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Customer [order=" + order + ", getFirstName()=" + getFirstName() + ", getLastName()=" + getLastName() + "]";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.tostring;
|
||||
|
||||
public class CustomerPrimitiveToString extends Customer {
|
||||
private long balance;
|
||||
|
||||
public long getBalance() {
|
||||
return balance;
|
||||
}
|
||||
|
||||
public void setBalance(long balance) {
|
||||
this.balance = balance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Customer [balance=" + balance + ", getFirstName()=" + getFirstName() + ", getLastName()=" + getLastName() + "]";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.baeldung.tostring;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
|
||||
|
||||
public class CustomerReflectionToString extends Customer {
|
||||
|
||||
private Integer score;
|
||||
private List<String> orders;
|
||||
private StringBuffer fullname;
|
||||
|
||||
public Integer getScore() {
|
||||
return score;
|
||||
}
|
||||
|
||||
public void setScore(Integer score) {
|
||||
this.score = score;
|
||||
}
|
||||
|
||||
public List<String> getOrders() {
|
||||
return orders;
|
||||
}
|
||||
|
||||
public void setOrders(List<String> orders) {
|
||||
this.orders = orders;
|
||||
}
|
||||
|
||||
public StringBuffer getFullname() {
|
||||
return fullname;
|
||||
}
|
||||
|
||||
public void setFullname(StringBuffer fullname) {
|
||||
this.fullname = fullname;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return ReflectionToStringBuilder.toString(this);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.baeldung.tostring;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class CustomerWrapperCollectionToString extends Customer {
|
||||
private Integer score;
|
||||
private List<String> orders;
|
||||
private StringBuffer fullname;
|
||||
|
||||
public Integer getScore() {
|
||||
return score;
|
||||
}
|
||||
|
||||
public void setScore(Integer score) {
|
||||
this.score = score;
|
||||
}
|
||||
|
||||
public List<String> getOrders() {
|
||||
return orders;
|
||||
}
|
||||
|
||||
public void setOrders(List<String> orders) {
|
||||
this.orders = orders;
|
||||
}
|
||||
|
||||
public StringBuffer getFullname() {
|
||||
return fullname;
|
||||
}
|
||||
|
||||
public void setFullname(StringBuffer fullname) {
|
||||
this.fullname = fullname;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Customer [score=" + score + ", orders=" + orders + ", fullname=" + fullname + ", getFirstName()=" + getFirstName() + ", getLastName()=" + getLastName() + "]";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.baeldung.tostring;
|
||||
|
||||
public class Order {
|
||||
|
||||
private String orderId;
|
||||
private String desc;
|
||||
private long value;
|
||||
private String status;
|
||||
|
||||
public String getOrderId() {
|
||||
return orderId;
|
||||
}
|
||||
|
||||
public void setOrderId(String orderId) {
|
||||
this.orderId = orderId;
|
||||
}
|
||||
|
||||
public String getDesc() {
|
||||
return desc;
|
||||
}
|
||||
|
||||
public void setDesc(String desc) {
|
||||
this.desc = desc;
|
||||
}
|
||||
|
||||
public long getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(long value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public String getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(String status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Order [orderId=" + orderId + ", desc=" + desc + ", value=" + value + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
# Root logger option
|
||||
log4j.rootLogger=DEBUG, stdout
|
||||
|
||||
# Redirect log messages to console
|
||||
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
|
||||
log4j.appender.stdout.Target=System.out
|
||||
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
|
||||
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
|
||||
Reference in New Issue
Block a user