[BAEL-16640] - Fixed conflicts

This commit is contained in:
amit2103
2019-11-07 23:52:43 +05:30
383 changed files with 4590 additions and 1658 deletions
@@ -0,0 +1,17 @@
## Core Java 9
This module contains articles about the improvements to core Java features introduced with Java 9.
### Relevant Articles:
- [New Stream Collectors in Java 9](http://www.baeldung.com/java9-stream-collectors)
- [Java 9 Optional API Additions](https://www.baeldung.com/java-9-optional)
- [Java 9 Convenience Factory Methods for Collections](https://www.baeldung.com/java-9-collections-factory-methods)
- [Java 9 Stream API Improvements](https://www.baeldung.com/java-9-stream-api)
- [Java 9 java.util.Objects Additions](https://www.baeldung.com/java-9-objects-new)
- [Java 9 CompletableFuture API Improvements](https://www.baeldung.com/java-9-completablefuture)
#### Relevant articles not in this module:
- [Java 9 Process API Improvements](https://www.baeldung.com/java-9-process-api) (see the [core-java-os](/core-java-os) module)
@@ -0,0 +1,73 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-9</artifactId>
<version>0.2-SNAPSHOT</version>
<name>core-java-9</name>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../../</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>com.jayway.awaitility</groupId>
<artifactId>awaitility</artifactId>
<version>${awaitility.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>${junit.platform.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>core-java-9</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
</plugins>
</build>
<pluginRepositories>
<pluginRepository>
<id>apache.snapshots</id>
<url>http://repository.apache.org/snapshots/</url>
</pluginRepository>
</pluginRepositories>
<properties>
<!-- testing -->
<assertj.version>3.10.0</assertj.version>
<junit.platform.version>1.2.0</junit.platform.version>
<awaitility.version>1.7.0</awaitility.version>
<maven.compiler.source>1.9</maven.compiler.source>
<maven.compiler.target>1.9</maven.compiler.target>
<guava.version>25.1-jre</guava.version>
</properties>
</project>
@@ -0,0 +1,11 @@
## Core Java 9
This module contains articles about Project Jigsaw and the Java Platform Module System (JPMS), introduced with Java 9.
### Relevant Articles:
- [Introduction to Project Jigsaw](http://www.baeldung.com/project-jigsaw-java-modularity)
- [A Guide to Java 9 Modularity](https://www.baeldung.com/java-9-modularity)
- [Java 9 java.lang.Module API](https://www.baeldung.com/java-9-module-api)
@@ -0,0 +1,35 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-9-jigsaw</artifactId>
<version>0.2-SNAPSHOT</version>
<name>core-java-9</name>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../../</relativePath>
</parent>
<build>
<finalName>core-java-9-jigsaw</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.source>1.9</maven.compiler.source>
<maven.compiler.target>1.9</maven.compiler.target>
</properties>
</project>
@@ -0,0 +1,14 @@
## Core Java 9
This module contains articles about core Java features that have been introduced in Java 9.
### Relevant Articles:
- [Java 9 New Features](https://www.baeldung.com/new-java-9)
- [Java 9 Variable Handles Demystified](http://www.baeldung.com/java-variable-handles)
- [Exploring the New HTTP Client in Java 9 and 11](http://www.baeldung.com/java-9-http-client)
- [Multi-Release Jar Files](https://www.baeldung.com/java-multi-release-jar)
- [Ahead of Time Compilation (AoT)](https://www.baeldung.com/ahead-of-time-compilation)
- [Introduction to Java 9 StackWalking API](https://www.baeldung.com/java-9-stackwalking-api)
- [Java 9 Platform Logging API](https://www.baeldung.com/java-9-logging-api)
- [Java 9 Reactive Streams](https://www.baeldung.com/java-9-reactive-streams)
@@ -0,0 +1,60 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-9-new-features</artifactId>
<version>0.2-SNAPSHOT</version>
<name>core-java-9</name>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../../</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>${junit.platform.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>core-java-9-new-features</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
</plugins>
</build>
<pluginRepositories>
<pluginRepository>
<id>apache.snapshots</id>
<url>http://repository.apache.org/snapshots/</url>
</pluginRepository>
</pluginRepositories>
<properties>
<!-- testing -->
<assertj.version>3.10.0</assertj.version>
<junit.platform.version>1.2.0</junit.platform.version>
<maven.compiler.source>1.9</maven.compiler.source>
<maven.compiler.target>1.9</maven.compiler.target>
</properties>
</project>
-13
View File
@@ -1,13 +0,0 @@
*.class
#folders#
/target
/neoDb*
/data
/src/main/webapp/WEB-INF/classes
*/META-INF/*
# Packaged files #
*.jar
*.war
*.ear
+3 -18
View File
@@ -4,28 +4,13 @@ This module contains articles about Java 9 core features
### Relevant Articles:
- [Java 9 New Features](https://www.baeldung.com/new-java-9)
- [New Stream Collectors in Java 9](http://www.baeldung.com/java9-stream-collectors)
- [Introduction to Project Jigsaw](http://www.baeldung.com/project-jigsaw-java-modularity)
- [Java 9 Variable Handles Demystified](http://www.baeldung.com/java-variable-handles)
- [Exploring the New HTTP Client in Java 9 and 11](http://www.baeldung.com/java-9-http-client)
- [Method Handles in Java](http://www.baeldung.com/java-method-handles)
- [Introduction to Chronicle Queue](http://www.baeldung.com/java-chronicle-queue)
- [Optional orElse Optional](http://www.baeldung.com/java-optional-or-else-optional)
- [Iterate Through a Range of Dates in Java](https://www.baeldung.com/java-iterate-date-range)
- [Initialize a HashMap in Java](https://www.baeldung.com/java-initialize-hashmap)
- [Immutable Set in Java](https://www.baeldung.com/java-immutable-set)
- [Multi-Release Jar Files](https://www.baeldung.com/java-multi-release-jar)
- [Ahead of Time Compilation (AoT)](https://www.baeldung.com/ahead-of-time-compilation)
- [Java 9 Process API Improvements](https://www.baeldung.com/java-9-process-api)
- [Java 9 java.util.Objects Additions](https://www.baeldung.com/java-9-objects-new)
- [Java 9 Reactive Streams](https://www.baeldung.com/java-9-reactive-streams)
- [Java 9 Optional API Additions](https://www.baeldung.com/java-9-optional)
- [Java 9 CompletableFuture API Improvements](https://www.baeldung.com/java-9-completablefuture)
- [Introduction to Java 9 StackWalking API](https://www.baeldung.com/java-9-stackwalking-api)
- [Java 9 Convenience Factory Methods for Collections](https://www.baeldung.com/java-9-collections-factory-methods)
- [Java 9 Stream API Improvements](https://www.baeldung.com/java-9-stream-api)
- [A Guide to Java 9 Modularity](https://www.baeldung.com/java-9-modularity)
- [Java 9 java.lang.Module API](https://www.baeldung.com/java-9-module-api)
- [Java 9 Platform Logging API](https://www.baeldung.com/java-9-logging-api)
- [Filtering a Stream of Optionals in Java](https://www.baeldung.com/java-filter-stream-of-optional)
Note: also contains part of the code for the article
[How to Filter a Collection in Java](https://www.baeldung.com/java-collection-filtering).
@@ -1,13 +0,0 @@
*.class
#folders#
/target
/neoDb*
/data
/src/main/webapp/WEB-INF/classes
*/META-INF/*
# Packaged files #
*.jar
*.war
*.ear
@@ -1,2 +0,0 @@
### Relevant Artiles:
- [Filtering a Stream of Optionals in Java](http://www.baeldung.com/java-filter-stream-of-optional)
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-date-operations</artifactId>
<version>${project.parent.version}</version>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-java</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-java</relativePath>
</parent>
</project>
@@ -0,0 +1,15 @@
package com.baeldung.datetime;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
public class CalendarUtils {
public static Calendar getPlusDays(Date date, int amount) throws ParseException {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_YEAR, amount);
return calendar;
}
}
@@ -0,0 +1,20 @@
package com.baeldung.datetime;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateUtils {
public static Date getNow() {
return new Date();
}
public static Date getDate(long millis) {
return new Date(millis);
}
public static Date getDate(String dateAsString, String pattern) throws ParseException {
return new SimpleDateFormat(pattern).parse(dateAsString);
}
}
@@ -0,0 +1,21 @@
package com.baeldung.datetime.sql;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class DateUtils {
public static Date getNow() {
return new Date(System.currentTimeMillis());
}
public static Date getDate(String dateAsString) {
return Date.valueOf(dateAsString);
}
public static Date getDate(String dateAsString, String pattern) throws ParseException {
java.util.Date customUtilDate = new SimpleDateFormat(pattern).parse(dateAsString);
return new Date(customUtilDate.getTime());
}
}
@@ -0,0 +1,21 @@
package com.baeldung.datetime.sql;
import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class TimeUtils {
public static Time getNow() {
return new Time(System.currentTimeMillis());
}
public static Time getTime(String timeAsString) {
return Time.valueOf(timeAsString);
}
public static Time getTime(String dateAsString, String pattern) throws ParseException {
java.util.Date customUtilDate = new SimpleDateFormat(pattern).parse(dateAsString);
return new Time(customUtilDate.getTime());
}
}
@@ -0,0 +1,21 @@
package com.baeldung.datetime.sql;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class TimestampUtils {
public static Timestamp getNow() {
return new Timestamp(System.currentTimeMillis());
}
public static Timestamp getTimestamp(String timestampAsString) {
return Timestamp.valueOf(timestampAsString);
}
public static Timestamp getTimestamp(String dateAsString, String pattern) throws ParseException {
java.util.Date customUtilDate = new SimpleDateFormat(pattern).parse(dateAsString);
return new Timestamp(customUtilDate.getTime());
}
}
@@ -0,0 +1,21 @@
package com.baeldung.datetime;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import com.baeldung.datetime.CalendarUtils;
import com.baeldung.datetime.DateUtils;
import java.text.ParseException;
import java.util.Date;
public class CalendarUtilsUnitTest {
@Test
public void givenDateAndDaysToAdd_thenCalendarIsCorrectlyReturned() throws ParseException {
Date initialDate = DateUtils.getDate("2020/01/01", "yyyy/MM/dd");
Date expectedDate= DateUtils.getDate("2020/01/11", "yyyy/MM/dd");
assertEquals(expectedDate, CalendarUtils.getPlusDays(initialDate, 10).getTime());
}
}
@@ -0,0 +1,25 @@
package com.baeldung.datetime;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import com.baeldung.datetime.DateUtils;
import java.text.ParseException;
import java.util.Date;
public class DateUtilsUnitTest {
@Test
public void givenTimeMillis_thenDateIsReturned() {
Date now = DateUtils.getNow();
assertEquals(DateUtils.getDate(now.getTime()), now);
}
@Test
public void givenDateAndPattern_thenDateIsCorrectlyReturned() throws ParseException {
long milliseconds = new Date(2020 - 1900, 0, 1).getTime();
assertEquals(DateUtils.getDate(milliseconds), DateUtils.getDate("2020/01/01", "yyyy/MM/dd"));
}
}
@@ -0,0 +1,28 @@
package com.baeldung.datetime.sql;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import com.baeldung.datetime.sql.DateUtils;
import java.text.ParseException;
import java.util.Date;
public class DateUtilsUnitTest {
@Test
public void givenCurrentDate_thenTodayIsReturned() {
assertEquals(DateUtils.getNow(), new Date());
}
@Test(expected = IllegalArgumentException.class)
public void givenDateAsString_whenPatternIsNotRespected_thenExceptionIsThrown() {
DateUtils.getDate("2020 01 01");
}
@Test
public void givenDateAndPattern_thenDateIsCorrectlyReturned() throws ParseException {
assertEquals(DateUtils.getDate("2020-01-01"), DateUtils.getDate("2020/01/01", "yyyy/MM/dd"));
}
}
@@ -0,0 +1,28 @@
package com.baeldung.datetime.sql;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import com.baeldung.datetime.sql.TimeUtils;
import java.text.ParseException;
import java.util.Date;
public class TimeUtilsUnitTest {
@Test
public void givenCurrentTime_thenNowIsReturned() {
assertEquals(TimeUtils.getNow(), new Date());
}
@Test(expected = IllegalArgumentException.class)
public void givenTimeAsString_whenPatternIsNotRespected_thenExceptionIsThrown() {
TimeUtils.getTime("10 11 12");
}
@Test
public void givenTimeAndPattern_thenTimeIsCorrectlyReturned() throws ParseException {
assertEquals(TimeUtils.getTime("10:11:12"), TimeUtils.getTime("10 11 12", "hh mm ss"));
}
}
@@ -0,0 +1,29 @@
package com.baeldung.datetime.sql;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import com.baeldung.datetime.sql.TimestampUtils;
import java.text.ParseException;
import java.util.Date;
public class TimestampUtilsUnitTest {
@Test
public void givenCurrentTimestamp_thenNowIsReturned() {
assertEquals(TimestampUtils.getNow()
.getTime(), new Date().getTime());
}
@Test(expected = IllegalArgumentException.class)
public void givenTimestampAsString_whenPatternIsNotRespected_thenExceptionIsThrown() {
TimestampUtils.getTimestamp("2020/01/01 10:11-12");
}
@Test
public void givenTimestampAndPattern_thenTimestampIsCorrectlyReturned() throws ParseException {
assertEquals(TimestampUtils.getTimestamp("2020-01-01 10:11:12"), TimestampUtils.getTimestamp("2020/01/01 10:11-12", "yyyy/MM/dd hh:mm-ss"));
}
}
@@ -13,4 +13,4 @@ This module contains articles about Object-oriented programming (OOP) in Java
- [A Guide to Inner Interfaces in Java](https://www.baeldung.com/java-inner-interfaces)
- [Java Classes and Objects](https://www.baeldung.com/java-classes-objects)
- [Java Interfaces](https://www.baeldung.com/java-interfaces)
- [[<-- Prev]](/core-java-modules/core-java-lang-oop-2)[[More -->]](/core-java-modules/core-java-lang-oop-4)
- [[<-- Prev]](/core-java-modules/core-java-lang-oop-2)[[More -->]](/core-java-modules/core-java-lang-oop-4)
@@ -6,4 +6,4 @@ This module contains articles about Object-oriented programming (OOP) in Java
- [Static and Dynamic Binding in Java](https://www.baeldung.com/java-static-dynamic-binding)
- [Methods in Java](https://www.baeldung.com/java-methods)
- [Java private Access Modifier](https://www.baeldung.com/java-private-keyword)
- [[<-- Prev]](/core-java-modules/core-java-lang-oop-3)
- [[<-- Prev]](/core-java-modules/core-java-lang-oop-3)
@@ -0,0 +1,223 @@
package com.baeldung.stringapi;
import org.junit.Test;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.IllegalFormatException;
import java.util.regex.PatternSyntaxException;
import static org.junit.Assert.*;
public class StringUnitTest {
@Test
public void whenCallCodePointAt_thenDecimalUnicodeReturned() {
assertEquals(97, "abcd".codePointAt(0));
}
@Test(expected = StringIndexOutOfBoundsException.class)
public void whenPassNonExistingIndex_thenExceptionThrown() {
int a = "abcd".codePointAt(4);
}
@Test
public void whenCallConcat_thenCorrect() {
assertEquals("elephant", "elep".concat("hant"));
}
@Test
public void whenGetBytes_thenCorrect() throws UnsupportedEncodingException {
byte[] byteArray1 = "abcd".getBytes();
byte[] byteArray2 = "efgh".getBytes(StandardCharsets.US_ASCII);
byte[] byteArray3 = "ijkl".getBytes("UTF-8");
byte[] expected1 = new byte[] { 97, 98, 99, 100 };
byte[] expected2 = new byte[] { 101, 102, 103, 104 };
byte[] expected3 = new byte[] { 105, 106, 107, 108 };
assertArrayEquals(expected1, byteArray1);
assertArrayEquals(expected2, byteArray2);
assertArrayEquals(expected3, byteArray3);
}
@Test
public void whenGetBytesUsingASCII_thenCorrect() {
byte[] byteArray = "efgh".getBytes(StandardCharsets.US_ASCII);
byte[] expected = new byte[] { 101, 102, 103, 104 };
assertArrayEquals(expected, byteArray);
}
@Test
public void whenCreateStringUsingByteArray_thenCorrect() {
byte[] array = new byte[] { 97, 98, 99, 100 };
String s = new String(array);
assertEquals("abcd", s);
}
@Test
public void whenCallCharAt_thenCorrect() {
assertEquals('P', "Paul".charAt(0));
}
@Test(expected = IndexOutOfBoundsException.class)
public void whenCharAtOnNonExistingIndex_thenIndexOutOfBoundsExceptionThrown() {
char character = "Paul".charAt(4);
}
@Test
public void whenCallCodePointCount_thenCorrect() {
assertEquals(2, "abcd".codePointCount(0, 2));
}
@Test(expected = IndexOutOfBoundsException.class)
public void whenSecondIndexEqualToLengthOfString_thenIndexOutOfBoundsExceptionThrown() {
char character = "Paul".charAt(4);
}
@Test
public void whenCallContains_thenCorrect() {
String s = "abcd";
assertTrue(s.contains("abc"));
assertFalse(s.contains("cde"));
}
@Test
public void whenCallCopyValueOf_thenStringConstructed() {
char[] array = new char[] { 'a', 'b', 'c', 'd' };
assertEquals("abcd", String.copyValueOf(array));
}
@Test
public void whenCallEndsWith_thenCorrect() {
String s1 = "test";
assertTrue(s1.endsWith("t"));
}
@Test
public void whenFormat_thenCorrect() {
String value = "Baeldung";
String formatted = String.format("Welcome to %s!", value);
assertEquals("Welcome to Baeldung!", formatted);
}
@Test(expected = IllegalFormatException.class)
public void whenInvalidFormatSyntax_thenIllegalFormatExceptionThrown() {
String value = "Baeldung";
String formatted = String.format("Welcome to %x!", value);
}
@Test
public void whenCallIndexOf_thenCorrect() {
assertEquals(1, "foo".indexOf("o"));
}
@Test
public void whenCallIsEmpty_thenCorrect() {
String s1 = "";
assertTrue(s1.isEmpty());
}
@Test
public void whenCallLastIndexOf_thenCorrect() {
assertEquals(2, "foo".lastIndexOf("o"));
assertEquals(2, "foo".lastIndexOf(111));
}
@Test
public void whenCallRegionMatches_thenCorrect() {
assertTrue("welcome to baeldung".regionMatches(false, 11, "baeldung", 0, 8));
}
@Test
public void whenCallStartsWith_thenCorrect() {
assertTrue("foo".startsWith("f"));
}
@Test
public void whenTrim_thenCorrect() {
assertEquals("foo", " foo ".trim());
}
@Test
public void whenSplit_thenCorrect() {
String s = "Welcome to Baeldung";
String[] array = new String[] { "Welcome", "to", "Baeldung" };
assertArrayEquals(array, s.split(" "));
}
@Test(expected = PatternSyntaxException.class)
public void whenPassInvalidParameterToSplit_thenPatternSyntaxExceptionThrown() {
String s = "Welcome*to Baeldung";
String[] result = s.split("*");
}
@Test
public void whenCallSubSequence_thenCorrect() {
String s = "Welcome to Baeldung";
assertEquals("Welcome", s.subSequence(0, 7));
}
@Test
public void whenCallSubstring_thenCorrect() {
String s = "Welcome to Baeldung";
assertEquals("Welcome", s.substring(0, 7));
}
@Test(expected = IndexOutOfBoundsException.class)
public void whenSecondIndexEqualToLengthOfString_thenCorrect() {
String s = "Welcome to Baeldung";
String sub = s.substring(0, 20);
}
@Test
public void whenConvertToUpperCase_thenCorrect() {
String s = "Welcome to Baeldung!";
assertEquals("WELCOME TO BAELDUNG!", s.toUpperCase());
}
@Test
public void whenConvertToLowerCase_thenCorrect() {
String s = "WELCOME to BAELDUNG!";
assertEquals("welcome to baeldung!", s.toLowerCase());
}
@Test
public void whenCallReplace_thenCorrect() {
String s = "I learn Spanish";
assertEquals("I learn French", s.replaceAll("Spanish", "French"));
}
@Test
public void whenIntern_thenCorrect() {
String s1 = "abc";
String s2 = new String("abc");
String s3 = new String("foo");
String s4 = s1.intern();
String s5 = s2.intern();
assertFalse(s3 == s4);
assertTrue(s1 == s5);
}
@Test
public void whenCallValueOf_thenCorrect() {
long l = 200L;
assertEquals("200", String.valueOf(l));
}
}
@@ -0,0 +1,14 @@
## Java Strings
This module contains articles about strings in Java.
### Relevant Articles:
- [Use char[] Array over a String for Manipulating Passwords in Java?](https://www.baeldung.com/java-storing-passwords)
- [Compact Strings in Java 9](https://www.baeldung.com/java-9-compact-string)
- [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)
@@ -0,0 +1,60 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-strings</artifactId>
<version>0.1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>core-java-strings</name>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-java</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-java</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>${jmh-core.version}</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>${jmh-generator.version}</version>
</dependency>
<dependency>
<groupId>com.ibm.icu</groupId>
<artifactId>icu4j</artifactId>
<version>${icu4j.version}</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>core-java-strings</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<properties>
<assertj.version>3.6.1</assertj.version>
<icu4j.version>61.1</icu4j.version>
</properties>
</project>
@@ -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 })));
}
}

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