Merge branch 'eugenp:master' into master
This commit is contained in:
@@ -46,7 +46,7 @@
|
||||
<maven.compiler.source.version>11</maven.compiler.source.version>
|
||||
<maven.compiler.target.version>11</maven.compiler.target.version>
|
||||
<jackson.version>2.16.0</jackson.version>
|
||||
<gson.version>2.10</gson.version>
|
||||
<gson.version>2.10.1</gson.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -3,3 +3,4 @@
|
||||
- [String Templates in Java 21](https://www.baeldung.com/java-21-string-templates)
|
||||
- [Unnamed Classes and Instance Main Methods in Java 21](https://www.baeldung.com/java-21-unnamed-class-instance-main)
|
||||
- [Unnamed Patterns and Variables in Java 21](https://www.baeldung.com/java-unnamed-patterns-variables)
|
||||
- [JFR View Command in Java 21](https://www.baeldung.com/java-flight-recorder-view)
|
||||
|
||||
@@ -11,4 +11,6 @@
|
||||
- [Retrieving Unix Time in Java](https://www.baeldung.com/java-retrieve-unix-time)
|
||||
- [Calculate Months Between Two Dates in Java](https://www.baeldung.com/java-months-difference-two-dates)
|
||||
- [Format LocalDate to ISO 8601 With T and Z](https://www.baeldung.com/java-format-localdate-iso-8601-t-z)
|
||||
- [Check if Two Date Ranges Overlap](https://www.baeldung.com/java-check-two-date-ranges-overlap)
|
||||
- [Difference between ZoneOffset.UTC and ZoneId.of(“UTC”)](https://www.baeldung.com/java-zoneoffset-utc-zoneid-of)
|
||||
- [[<-- Prev]](/core-java-modules/core-java-datetime-java8-1)
|
||||
|
||||
@@ -12,3 +12,4 @@ This module contains articles about arrays conversion in Java
|
||||
- [Convert an ArrayList of String to a String Array in Java](https://www.baeldung.com/java-convert-string-arraylist-array)
|
||||
- [Convert Char Array to Int Array in Java](https://www.baeldung.com/java-convert-char-int-array)
|
||||
- [How to Convert Byte Array to Char Array](https://www.baeldung.com/java-convert-byte-array-char)
|
||||
- [Convert byte[] to Byte[] and Vice Versa in Java](https://www.baeldung.com/java-byte-array-wrapper-primitive-type-convert)
|
||||
|
||||
@@ -7,10 +7,6 @@
|
||||
<name>core-java-arrays-guides</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<properties>
|
||||
<system-stubs.jupiter.version>2.1.5</system-stubs.jupiter.version>
|
||||
</properties>
|
||||
|
||||
<parent>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
@@ -34,7 +30,10 @@
|
||||
<version>${system-stubs.jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<system-stubs.jupiter.version>2.1.5</system-stubs.jupiter.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
||||
@@ -13,4 +13,5 @@
|
||||
- [Time Complexity of Java Collections Sort in Java](https://www.baeldung.com/java-time-complexity-collections-sort)
|
||||
- [Check if List Contains at Least One Enum](https://www.baeldung.com/java-list-check-enum-presence)
|
||||
- [Comparison of for Loops and Iterators](https://www.baeldung.com/java-for-loops-vs-iterators)
|
||||
- [PriorityQueue iterator() Method in Java](https://www.baeldung.com/java-priorityqueue-iterator)
|
||||
- More articles: [[<-- prev]](/core-java-modules/core-java-collections-4)
|
||||
|
||||
@@ -5,18 +5,6 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>core-java-collections-5</artifactId>
|
||||
<name>core-java-collections-5</name>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>9</source>
|
||||
<target>9</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
@@ -61,6 +49,19 @@
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>9</source>
|
||||
<target>9</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<junit.version>5.9.2</junit.version>
|
||||
<roaringbitmap.version>0.9.38</roaringbitmap.version>
|
||||
|
||||
@@ -55,7 +55,6 @@
|
||||
<version>${org.json.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
|
||||
@@ -3,3 +3,4 @@
|
||||
- [Removing the Last Node in a Linked List](https://www.baeldung.com/java-linked-list-remove-last-element)
|
||||
- [Call a Method on Each Element of a List in Java](https://www.baeldung.com/java-call-method-each-list-item)
|
||||
- [Sorting One List Based on Another List in Java](https://www.baeldung.com/java-sorting-one-list-using-another)
|
||||
- [Reset ListIterator to First Element of the List in Java](https://www.baeldung.com/java-reset-listiterator)
|
||||
|
||||
@@ -27,7 +27,7 @@
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>2.8.9</version>
|
||||
<version>2.10.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.json</groupId>
|
||||
|
||||
@@ -41,7 +41,7 @@
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<gson.version>2.8.5</gson.version>
|
||||
<gson.version>2.10.1</gson.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.countdownlatchvssemaphore;
|
||||
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
|
||||
public class CountDownLatchDemo {
|
||||
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
// Create a CountDownLatch with an initial count equal to the number of tasks to be completed
|
||||
int numberOfTasks = 3;
|
||||
CountDownLatch latch = new CountDownLatch(numberOfTasks);
|
||||
|
||||
// Simulate completion of tasks by worker threads
|
||||
for (int i = 1; i <= numberOfTasks; i++) {
|
||||
new Thread(() -> {
|
||||
System.out.println("Task completed by Thread " + Thread.currentThread()
|
||||
.getId());
|
||||
|
||||
// Decrement the latch count to signal completion of a task
|
||||
latch.countDown();
|
||||
}).start();
|
||||
}
|
||||
|
||||
// Main thread waits until all tasks are completed
|
||||
latch.await();
|
||||
System.out.println("All tasks completed. Main thread proceeds.");
|
||||
|
||||
// Attempting to reset will have no effect
|
||||
latch.countDown();
|
||||
// Latch is already at zero, await() returns immediately
|
||||
latch.await(); // This line won't block
|
||||
System.out.println("Latch is already at zero and cannot be reset.");
|
||||
}
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
package com.baeldung.countdownlatchvssemaphore;
|
||||
|
||||
import java.util.concurrent.Semaphore;
|
||||
|
||||
public class SemaphoreDemo {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// Create a Semaphore with a fixed number of permits
|
||||
int NUM_PERMITS = 3;
|
||||
Semaphore semaphore = new Semaphore(NUM_PERMITS);
|
||||
|
||||
// Simulate resource access by worker threads
|
||||
for (int i = 1; i <= 5; i++) {
|
||||
new Thread(() -> {
|
||||
try {
|
||||
// Acquire a permit to access the resource
|
||||
semaphore.acquire();
|
||||
System.out.println("Thread " + Thread.currentThread().getId() + " accessing resource.");
|
||||
|
||||
// Simulate resource usage
|
||||
Thread.sleep(2000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
// Release the permit after resource access is complete
|
||||
semaphore.release();
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
|
||||
// Simulate resetting the Semaphore by releasing additional permits after a delay
|
||||
try {
|
||||
Thread.sleep(5000);
|
||||
|
||||
// Resetting the semaphore permits to the initial count
|
||||
semaphore.release(NUM_PERMITS);
|
||||
System.out.println("Semaphore permits reset to initial count.");
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,3 +8,4 @@
|
||||
- [System.console() vs. System.out](https://www.baeldung.com/java-system-console-vs-system-out)
|
||||
- [How to Log to the Console in Color](https://www.baeldung.com/java-log-console-in-color)
|
||||
- [Create Table Using ASCII in a Console in Java](https://www.baeldung.com/java-console-ascii-make-table)
|
||||
- [Printing Message on Console without Using main() Method in Java](https://www.baeldung.com/java-no-main-print-message-console)
|
||||
|
||||
@@ -2,4 +2,5 @@
|
||||
This module contains articles about date operations in Java.
|
||||
|
||||
### Relevant Articles:
|
||||
- [Calculate Number of Weekdays Between Two Dates in Java](https://www.baeldung.com/java-count-weekdays-between-two-dates)
|
||||
|
||||
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
package com.baeldung.stringdatetoxmlgregoriancalendar;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.LocalDate;
|
||||
import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
|
||||
import javax.xml.datatype.DatatypeConfigurationException;
|
||||
import javax.xml.datatype.DatatypeFactory;
|
||||
import javax.xml.datatype.XMLGregorianCalendar;
|
||||
|
||||
import org.joda.time.DateTime;
|
||||
import org.joda.time.format.DateTimeFormat;
|
||||
|
||||
public class StringDateToXMLGregorianCalendarConverter {
|
||||
public static XMLGregorianCalendar usingDatatypeFactoryForDate(String dateAsString) throws DatatypeConfigurationException {
|
||||
return DatatypeFactory.newInstance().newXMLGregorianCalendar(dateAsString);
|
||||
}
|
||||
|
||||
public static XMLGregorianCalendar usingLocalDate(String dateAsString) throws DatatypeConfigurationException {
|
||||
LocalDate localDate = LocalDate.parse(dateAsString);
|
||||
return DatatypeFactory.newInstance().newXMLGregorianCalendar(localDate.toString());
|
||||
}
|
||||
|
||||
public static XMLGregorianCalendar usingSimpleDateFormat(String dateTimeAsString) throws DatatypeConfigurationException, ParseException {
|
||||
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
|
||||
Date date = simpleDateFormat.parse(dateTimeAsString);
|
||||
return DatatypeFactory.newInstance().newXMLGregorianCalendar(simpleDateFormat.format(date));
|
||||
}
|
||||
|
||||
public static XMLGregorianCalendar usingGregorianCalendar(String dateTimeAsString) throws DatatypeConfigurationException, ParseException {
|
||||
GregorianCalendar calendar = new GregorianCalendar();
|
||||
calendar.setTime(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").parse(dateTimeAsString));
|
||||
return DatatypeFactory.newInstance().newXMLGregorianCalendar(calendar);
|
||||
}
|
||||
|
||||
public static XMLGregorianCalendar usingJodaTime(String dateTimeAsString) throws DatatypeConfigurationException {
|
||||
DateTime dateTime = DateTime.parse(dateTimeAsString, DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss"));
|
||||
return DatatypeFactory.newInstance().newXMLGregorianCalendar(dateTime.toGregorianCalendar());
|
||||
}
|
||||
}
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
package com.baeldung.stringdatetoxmlgregoriancalendar;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.text.ParseException;
|
||||
import javax.xml.datatype.DatatypeConfigurationException;
|
||||
import javax.xml.datatype.XMLGregorianCalendar;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class StringDateToXMLGregorianCalendarConverterUnitTest {
|
||||
private static final String dateAsString = "2014-04-24";
|
||||
private static final String dateTimeAsString = "2014-04-24T15:45:30";
|
||||
|
||||
@Test
|
||||
void givenStringDate_whenUsingDatatypeFactory_thenConvertToXMLGregorianCalendar() throws DatatypeConfigurationException {
|
||||
XMLGregorianCalendar xmlGregorianCalendar = StringDateToXMLGregorianCalendarConverter.usingDatatypeFactoryForDate(dateAsString);
|
||||
assertEquals(24,xmlGregorianCalendar.getDay());
|
||||
assertEquals(4,xmlGregorianCalendar.getMonth());
|
||||
assertEquals(2014,xmlGregorianCalendar.getYear());
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenStringDateTime_whenUsingApacheCommonsLang3_thenConvertToXMLGregorianCalendar() throws DatatypeConfigurationException {
|
||||
XMLGregorianCalendar xmlGregorianCalendar = StringDateToXMLGregorianCalendarConverter.usingLocalDate(dateAsString);
|
||||
assertEquals(24,xmlGregorianCalendar.getDay());
|
||||
assertEquals(4,xmlGregorianCalendar.getMonth());
|
||||
assertEquals(2014,xmlGregorianCalendar.getYear());
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenStringDateTime_whenUsingSimpleDateFormat_thenConvertToXMLGregorianCalendar() throws DatatypeConfigurationException, ParseException {
|
||||
XMLGregorianCalendar xmlGregorianCalendar = StringDateToXMLGregorianCalendarConverter.usingSimpleDateFormat(dateTimeAsString);
|
||||
assertEquals(24,xmlGregorianCalendar.getDay());
|
||||
assertEquals(4,xmlGregorianCalendar.getMonth());
|
||||
assertEquals(2014,xmlGregorianCalendar.getYear());
|
||||
assertEquals(15,xmlGregorianCalendar.getHour());
|
||||
assertEquals(45,xmlGregorianCalendar.getMinute());
|
||||
assertEquals(30,xmlGregorianCalendar.getSecond());
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenStringDateTime_whenUsingGregorianCalendar_thenConvertToXMLGregorianCalendar() throws DatatypeConfigurationException, ParseException {
|
||||
XMLGregorianCalendar xmlGregorianCalendar = StringDateToXMLGregorianCalendarConverter.usingGregorianCalendar(dateTimeAsString);
|
||||
assertEquals(24,xmlGregorianCalendar.getDay());
|
||||
assertEquals(4,xmlGregorianCalendar.getMonth());
|
||||
assertEquals(2014,xmlGregorianCalendar.getYear());
|
||||
assertEquals(15,xmlGregorianCalendar.getHour());
|
||||
assertEquals(45,xmlGregorianCalendar.getMinute());
|
||||
assertEquals(30,xmlGregorianCalendar.getSecond());
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenStringDateTime_whenUsingJodaTime_thenConvertToXMLGregorianCalendar() throws DatatypeConfigurationException {
|
||||
XMLGregorianCalendar xmlGregorianCalendar = StringDateToXMLGregorianCalendarConverter.usingJodaTime(dateTimeAsString);
|
||||
assertEquals(24,xmlGregorianCalendar.getDay());
|
||||
assertEquals(4,xmlGregorianCalendar.getMonth());
|
||||
assertEquals(2014,xmlGregorianCalendar.getYear());
|
||||
assertEquals(15,xmlGregorianCalendar.getHour());
|
||||
assertEquals(45,xmlGregorianCalendar.getMinute());
|
||||
assertEquals(30,xmlGregorianCalendar.getSecond());
|
||||
}
|
||||
}
|
||||
@@ -5,3 +5,4 @@ This module contains articles about parsing and formatting Java date and time ob
|
||||
### Relevant Articles:
|
||||
- [Convert String to Instant](https://www.baeldung.com/java-string-to-instant)
|
||||
- [Sort Date Strings in Java](https://www.baeldung.com/java-sort-date-strings)
|
||||
- [Using Current Time as Filename in Java](https://www.baeldung.com/java-current-time-filename)
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
Hello, world!
|
||||
+85
@@ -0,0 +1,85 @@
|
||||
package com.baeldung.readwritethread;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
|
||||
public class ReadWriteBlockingQueue {
|
||||
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
|
||||
BlockingQueue<String> queue = new LinkedBlockingQueue<>();
|
||||
String readFileName = "src/main/resources/read_file.txt";
|
||||
String writeFileName = "src/main/resources/write_file.txt";
|
||||
|
||||
Thread producerThread = new Thread(new FileProducer(queue, readFileName));
|
||||
Thread consumerThread1 = new Thread(new FileConsumer(queue, writeFileName));
|
||||
|
||||
producerThread.start();
|
||||
Thread.sleep(100); // Give producer a head start
|
||||
consumerThread1.start();
|
||||
try {
|
||||
producerThread.join();
|
||||
consumerThread1.join();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
class FileProducer implements Runnable {
|
||||
|
||||
private final BlockingQueue<String> queue;
|
||||
private final String inputFileName;
|
||||
|
||||
public FileProducer(BlockingQueue<String> queue, String inputFileName) {
|
||||
this.queue = queue;
|
||||
this.inputFileName = inputFileName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try (BufferedReader reader = new BufferedReader(new FileReader(inputFileName))) {
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
queue.offer(line);
|
||||
System.out.println("Producer added line: " + line);
|
||||
System.out.println("Queue size: " + queue.size());
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class FileConsumer implements Runnable {
|
||||
|
||||
private final BlockingQueue<String> queue;
|
||||
private final String outputFileName;
|
||||
|
||||
public FileConsumer(BlockingQueue queue, String outputFileName) {
|
||||
this.queue = queue;
|
||||
this.outputFileName = outputFileName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try (BufferedWriter writer = new BufferedWriter(new FileWriter(outputFileName))) {
|
||||
String line;
|
||||
while ((line = queue.poll()) != null) {
|
||||
writer.write(line);
|
||||
writer.newLine();
|
||||
System.out.println(Thread.currentThread()
|
||||
.getId() + " - Consumer processed line: " + line);
|
||||
System.out.println("Queue size: " + queue.size());
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
package com.baeldung.readwritethread;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
|
||||
public class ReadWriteThread {
|
||||
|
||||
public static void readFile(String filePath) {
|
||||
Thread thread = new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath))) {
|
||||
String line;
|
||||
while ((line = bufferedReader.readLine()) != null) {
|
||||
System.out.println(line);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
thread.start();
|
||||
}
|
||||
|
||||
public static void writeFile(String filePath, String content) {
|
||||
Thread thread = new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try (FileWriter fileWriter = new FileWriter(filePath)) {
|
||||
fileWriter.write("Hello, world!");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
thread.start();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
String file = "src/main/resources/text.txt";
|
||||
|
||||
writeFile(file, "Hello, world!");
|
||||
|
||||
readFile(file);
|
||||
|
||||
// Sleep for a while to allow the threads to complete
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
Hello,
|
||||
Baeldung!
|
||||
Nice to meet you!
|
||||
My name is
|
||||
Wynn.
|
||||
@@ -0,0 +1 @@
|
||||
Hello, world!
|
||||
@@ -0,0 +1,5 @@
|
||||
Hello,
|
||||
Baeldung!
|
||||
Nice to meet you!
|
||||
My name is
|
||||
Wynn.
|
||||
@@ -82,6 +82,7 @@
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>core-java-io-apis-2</finalName>
|
||||
<resources>
|
||||
@@ -91,6 +92,7 @@
|
||||
</resource>
|
||||
</resources>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<junit-jupiter-version>5.9.3</junit-jupiter-version>
|
||||
</properties>
|
||||
|
||||
@@ -3,14 +3,14 @@
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
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-lang-6</artifactId>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>core-java-lang-6</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<!-- https://mvnrepository.com/artifact/org.mapstruct/mapstruct -->
|
||||
<dependency>
|
||||
|
||||
@@ -14,4 +14,7 @@
|
||||
- [Clamp Function in Java](https://www.baeldung.com/java-clamp-function)
|
||||
- [Creating a Magic Square in Java](https://www.baeldung.com/java-magic-square)
|
||||
- [Check if a Point Is Between Two Points Drawn on a Straight Line in Java](https://www.baeldung.com/java-check-point-straight-line)
|
||||
- [Validate if a String Is a Valid Geo Coordinate](https://www.baeldung.com/java-geo-coordinates-validation)
|
||||
- [Rotate a Vertex Around a Certain Point in Java](https://www.baeldung.com/java-rotate-vertex-around-point)
|
||||
- [Calculating the Power of Any Number in Java Without Using Math pow() Method](https://www.baeldung.com/java-calculating-the-power-without-math-pow)
|
||||
- More articles: [[<-- Prev]](/core-java-modules/core-java-lang-math-2)
|
||||
|
||||
@@ -5,6 +5,14 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>core-java-lang-math</artifactId>
|
||||
<name>core-java-lang-math</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
@@ -13,13 +21,6 @@
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<build>
|
||||
<finalName>core-java-lang-math</finalName>
|
||||
|
||||
@@ -32,7 +32,7 @@
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<gson.version>2.8.2</gson.version>
|
||||
<gson.version>2.10.1</gson.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+3
-3
@@ -22,7 +22,7 @@ public class URLNormalizationUnitTest {
|
||||
String normalizedUri = originalUrl.split("\\?")[0];
|
||||
assertEquals(expectedNormalizedUrl, normalizedUri);
|
||||
} else {
|
||||
throw new IllegalArgumentException("Invalid URL: " + originalUrl);
|
||||
fail(originalUrl);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,7 +35,7 @@ public class URLNormalizationUnitTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenOriginalUrl_whenUsingRegularExpression_thenNormalizedUrl() throws URISyntaxException, UnsupportedEncodingException {
|
||||
public void givenOriginalUrl_whenUsingRegularExpression_thenNormalizedUrl() {
|
||||
String regex = "^(https?://[^/]+/[^?#]+)";
|
||||
Pattern pattern = Pattern.compile(regex);
|
||||
Matcher matcher = pattern.matcher(originalUrl);
|
||||
@@ -44,7 +44,7 @@ public class URLNormalizationUnitTest {
|
||||
String normalizedUrl = matcher.group(1);
|
||||
assertEquals(expectedNormalizedUrl, normalizedUrl);
|
||||
} else {
|
||||
throw new IllegalArgumentException("Invalid URL: " + originalUrl);
|
||||
fail(originalUrl);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,6 +31,7 @@
|
||||
<version>${guava.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>core-java-numbers-6</finalName>
|
||||
<resources>
|
||||
@@ -44,4 +45,5 @@
|
||||
<properties>
|
||||
<commons-codec>1.16.0</commons-codec>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -1,3 +1,4 @@
|
||||
## Relevant Articles
|
||||
- [Check if a double Is an Integer in Java](https://www.baeldung.com/java-check-double-integer)
|
||||
- [Print a Double Value Without Scientific Notation in Java](https://www.baeldung.com/java-print-double-number-no-scientific-notation)
|
||||
- [Check if a Float Value is Equivalent to an Integer Value in Java](https://www.baeldung.com/java-float-integer-equal)
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
<version>${guava.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>core-java-numbers-7</finalName>
|
||||
<resources>
|
||||
@@ -34,4 +35,5 @@
|
||||
</resource>
|
||||
</resources>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
@@ -2,14 +2,15 @@
|
||||
<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-records</artifactId>
|
||||
|
||||
<parent>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>core-java-records</artifactId>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
|
||||
@@ -7,4 +7,5 @@ This module contains articles about core Java Security
|
||||
- [Extract CN From X509 Certificate in Java](https://www.baeldung.com/java-extract-common-name-x509-certificate)
|
||||
- [Check Certificate Name and Alias in Keystore File](https://www.baeldung.com/java-keystore-check-certificate-name-alias)
|
||||
- [Using a Custom TrustStore in Java](https://www.baeldung.com/java-custom-truststore)
|
||||
- [Enable Java SSL Debug Logging](https://www.baeldung.com/java-ssl-debug-logging)
|
||||
- More articles: [[<-- prev]](/core-java-modules/core-java-security-3)
|
||||
|
||||
+93
@@ -0,0 +1,93 @@
|
||||
package com.baeldung.string.runlength;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class RunLengthEncodingUnitTest {
|
||||
private static final String INPUT = "WWWWWWWWWWWWBAAACCDEEEEE";
|
||||
private static final String RLE = "12W1B3A2C1D5E";
|
||||
|
||||
String runLengthEncode(String input) {
|
||||
StringBuilder result = new StringBuilder();
|
||||
int count = 1;
|
||||
char[] chars = input.toCharArray();
|
||||
for (int i = 0; i < chars.length; i++) {
|
||||
char c = chars[i];
|
||||
if (i + 1 < chars.length && c == chars[i + 1]) {
|
||||
count++;
|
||||
} else {
|
||||
result.append(count).append(c);
|
||||
count = 1;
|
||||
}
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
|
||||
String runLengthDecode(String rle) {
|
||||
StringBuilder result = new StringBuilder();
|
||||
char[] chars = rle.toCharArray();
|
||||
|
||||
int count = 0;
|
||||
for (char c : chars) {
|
||||
if (Character.isDigit(c)) {
|
||||
count = 10 * count + Character.getNumericValue(c);
|
||||
} else {
|
||||
result.append(String.join("", Collections.nCopies(count, String.valueOf(c))));
|
||||
count = 0;
|
||||
}
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
String runLengthEncodeByRegEx(String input) {
|
||||
String[] arr = input.split("(?<=(\\D))(?!\\1)");
|
||||
StringBuilder result = new StringBuilder();
|
||||
for (String run : arr) {
|
||||
result.append(run.length()).append(run.charAt(0));
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
String runLengthDecodeByRegEx(String rle) {
|
||||
if (rle.isEmpty()) {
|
||||
return "";
|
||||
}
|
||||
String[] arr = rle.split("(?<=\\D)|(?=\\D+)");
|
||||
if (arr.length % 2 != 0) {
|
||||
throw new IllegalArgumentException("Not a RLE string");
|
||||
}
|
||||
StringBuilder result = new StringBuilder();
|
||||
|
||||
for (int i = 1; i <= arr.length; i += 2) {
|
||||
int count = Integer.parseInt(arr[i - 1]);
|
||||
String c = arr[i];
|
||||
|
||||
result.append(String.join("", Collections.nCopies(count, c)));
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenInvokingRunLengthEncode_thenGetExpectedResult() {
|
||||
assertEquals(RLE, runLengthEncode(INPUT));
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenInvokingRunLengthDecode_thenGetExpectedResult() {
|
||||
assertEquals(INPUT, runLengthDecode(RLE));
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenInvokingRunLengthEncodeByRegEx_thenGetExpectedResult() {
|
||||
assertEquals(RLE, runLengthEncodeByRegEx(INPUT));
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenInvokingRunLengthDecodeByRegEx_thenGetExpectedResult() {
|
||||
assertEquals(INPUT, runLengthDecodeByRegEx(RLE));
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,7 @@
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
|
||||
@@ -12,3 +12,5 @@
|
||||
- [Check If a Java StringBuilder Object Contains a Character](https://www.baeldung.com/java-check-stringbuilder-object-contains-character)
|
||||
- [Comparing One String With Multiple Values in One Expression in Java](https://www.baeldung.com/java-compare-string-multiple-values-one-expression)
|
||||
- [UTF-8 Validation in Java](https://www.baeldung.com/java-utf-8-validation)
|
||||
- [Simple Morse Code Translation in Java](https://www.baeldung.com/java-morse-code-english-translate)
|
||||
- [How to Determine if a String Contains Invalid Encoded Characters](https://www.baeldung.com/java-check-string-contains-invalid-encoded-characters)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<?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">
|
||||
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-swing</artifactId>
|
||||
<name>core-java-swing</name>
|
||||
@@ -12,6 +12,7 @@
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>20</maven.compiler.source>
|
||||
<maven.compiler.target>20</maven.compiler.target>
|
||||
|
||||
Reference in New Issue
Block a user