Merge branch 'master' into BAEL-6421-PrintWriter-write-vs-print

This commit is contained in:
Andrei Branza 2024-03-24 16:06:07 +02:00
commit 0d7b2dee7e
620 changed files with 6084 additions and 1305 deletions

View File

@ -4,8 +4,8 @@
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>akka-modules</artifactId>
<name>akka-modules</name>
<packaging>pom</packaging>
<name>akka-modules</name>
<parent>
<artifactId>parent-modules</artifactId>

View File

@ -12,4 +12,6 @@
- [Count the Number of Unique Digits in an Integer using Java](https://www.baeldung.com/java-int-count-unique-digits)
- [Generate Juggler Sequence in Java](https://www.baeldung.com/java-generate-juggler-sequence)
- [Finding the Parent of a Node in a Binary Search Tree with Java](https://www.baeldung.com/java-find-parent-node-binary-search-tree)
- [Check if a Number Is a Happy Number in Java](https://www.baeldung.com/java-happy-sad-number-test)
- [Find the Largest Number Possible After Removing k Digits of a Number](https://www.baeldung.com/java-find-largest-number-remove-k-digits)
- More articles: [[<-- prev]](/algorithms-miscellaneous-6)

View File

@ -0,0 +1,16 @@
<?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>algorithms-miscellaneous-8</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>algorithms-miscellaneous-8</name>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>algorithms-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
</project>

View File

@ -24,17 +24,17 @@ public class VigenereCipherUnitTest {
@Test
void encodeArticleTitle() {
VigenereCipher cipher = new VigenereCipher();
String output = cipher.encode("VEGENERE CIPHER IN JAVA", "BAELDUNG");
String output = cipher.encode("VIGENERE CIPHER IN JAVA", "BAELDUNG");
Assertions.assertEquals("XFLQRZFL EJUTIM WU LBAM", output);
Assertions.assertEquals("XJLQRZFL EJUTIM WU LBAM", output);
}
@Test
void encodeArticleTitleMoreCharacters() {
VigenereCipher cipher = new VigenereCipher("ABCDEFGHIJKLMNOPQRSTUVWXYZ ");
String output = cipher.encode("VEGENERE CIPHER IN JAVA", "BAELDUNG");
String output = cipher.encode("VIGENERE CIPHER IN JAVA", "BAELDUNG");
Assertions.assertEquals("XFLQRZELBDNALZEGKOEVEPO", output);
Assertions.assertEquals("XJLQRZELBDNALZEGKOEVEPO", output);
}
@Test
@ -56,8 +56,21 @@ public class VigenereCipherUnitTest {
@Test
void decodeArticleTitleMoreCharacters() {
VigenereCipher cipher = new VigenereCipher("ABCDEFGHIJKLMNOPQRSTUVWXYZ ");
String output = cipher.decode("XFLQRZELBDNALZEGKOEVEPO", "BAELDUNG");
String output = cipher.decode("XJLQRZELBDNALZEGKOEVEPO", "BAELDUNG");
Assertions.assertEquals("VEGENERE CIPHER IN JAVA", output);
Assertions.assertEquals("VIGENERE CIPHER IN JAVA", output);
}
@Test
void encodeDecodeBaeldung() {
VigenereCipher cipher = new VigenereCipher();
String input = "BAELDUNG";
String key = "HELLO";
String encoded = cipher.encode(input, key);
String decoded = cipher.decode(encoded, key);
Assertions.assertEquals(input, decoded);
}
}

View File

@ -22,6 +22,7 @@
<module>algorithms-miscellaneous-5</module>
<module>algorithms-miscellaneous-6</module>
<module>algorithms-miscellaneous-7</module>
<module>algorithms-miscellaneous-8</module>
<module>algorithms-searching</module>
<module>algorithms-sorting</module>
<module>algorithms-sorting-2</module>
@ -34,4 +35,4 @@
<tradukisto.version>1.0.1</tradukisto.version>
</properties>
</project>
</project>

View File

@ -4,8 +4,8 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>cxf-spring</artifactId>
<name>cxf-spring</name>
<packaging>war</packaging>
<name>cxf-spring</name>
<parent>
<groupId>com.baeldung</groupId>

View File

@ -5,8 +5,9 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>apache-cxf-modules</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>apache-cxf-modules</name>
<packaging>pom</packaging>
<name>apache-cxf-modules</name>
<parent>
<groupId>com.baeldung</groupId>

View File

@ -4,8 +4,8 @@
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>sse-jaxrs</artifactId>
<name>sse-jaxrs</name>
<packaging>pom</packaging>
<name>sse-jaxrs</name>
<parent>
<groupId>com.baeldung</groupId>

View File

@ -4,8 +4,8 @@
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>sse-jaxrs-server</artifactId>
<name>sse-jaxrs-server</name>
<packaging>war</packaging>
<name>sse-jaxrs-server</name>
<parent>
<groupId>com.baeldung</groupId>

View File

@ -5,8 +5,8 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>apache-httpclient4</artifactId>
<version>0.1-SNAPSHOT</version>
<name>apache-httpclient4</name>
<packaging>war</packaging>
<name>apache-httpclient4</name>
<parent>
<groupId>com.baeldung</groupId>
@ -193,7 +193,6 @@
<filtering>true</filtering>
</resource>
</resources>
</build>
<profiles>

10
apache-kafka-3/README.md Normal file
View File

@ -0,0 +1,10 @@
## Apache Kafka
This module contains articles about Apache Kafka.
##### Building the project
You can build the project from the command line using: *mvn clean install*, or in an IDE.
### Relevant Articles:
- [Commit Offsets in Kafka](https://www.baeldung.com/kafka-commit-offsets)

48
apache-kafka-3/pom.xml Normal file
View File

@ -0,0 +1,48 @@
<?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>apache-kafka-3</artifactId>
<name>apache-kafka-3</name>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>${kafka.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${org.slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.databind.version}</version>
</dependency>
</dependencies>
<properties>
<kafka.version>3.6.1</kafka.version>
<jackson.databind.version>2.15.2</jackson.databind.version>
</properties>
<profiles>
<profile>
<id>integration-jdk9-and-above</id>
</profile>
</profiles>
</project>

View File

@ -0,0 +1,23 @@
package com.baeldung.kafka.commitoffset;
import com.baeldung.kafka.commitoffset.config.KafkaConfigProperties;
import java.time.Duration;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
public class AsyncCommit {
public static void main(String[] args) {
KafkaConsumer<Long, String> consumer = new KafkaConsumer<>(KafkaConfigProperties.getProperties());
consumer.subscribe(KafkaConfigProperties.getTopic());
ConsumerRecords<Long, String> messages = consumer.poll(Duration.ofSeconds(10));
for (ConsumerRecord<Long, String> message : messages) {
// processed message
consumer.commitAsync();
}
}
}

View File

@ -0,0 +1,26 @@
package com.baeldung.kafka.commitoffset;
import com.baeldung.kafka.commitoffset.config.KafkaConfigProperties;
import java.time.Duration;
import java.util.Properties;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
public class AutomaticCommit {
public static void main(String[] args) {
Properties properties = KafkaConfigProperties.getProperties();
properties.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, "true");
KafkaConsumer<Long, String> consumer = new KafkaConsumer<>(properties);
consumer.subscribe(KafkaConfigProperties.getTopic());
ConsumerRecords<Long, String> messages = consumer.poll(Duration.ofSeconds(10));
for (ConsumerRecord<Long, String> message : messages) {
// processed message
}
}
}

View File

@ -0,0 +1,34 @@
package com.baeldung.kafka.commitoffset;
import com.baeldung.kafka.commitoffset.config.KafkaConfigProperties;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.clients.consumer.OffsetAndMetadata;
import org.apache.kafka.common.TopicPartition;
public class SpecificOffsetCommit {
public static void main(String[] args) {
KafkaConsumer<Long, String> consumer = new KafkaConsumer<>(KafkaConfigProperties.getProperties());
consumer.subscribe(KafkaConfigProperties.getTopic());
Map<TopicPartition, OffsetAndMetadata> currentOffsets = new HashMap<>();
int messageProcessed = 0;
while (true) {
ConsumerRecords<Long, String> messages = consumer.poll(Duration.ofSeconds(10));
for (ConsumerRecord<Long, String> message : messages) {
// processed message
messageProcessed++;
currentOffsets.put(new TopicPartition(message.topic(), message.partition()), new OffsetAndMetadata(message.offset() + 1));
if (messageProcessed % 50 == 0) {
consumer.commitSync(currentOffsets);
}
}
}
}
}

View File

@ -0,0 +1,23 @@
package com.baeldung.kafka.commitoffset;
import com.baeldung.kafka.commitoffset.config.KafkaConfigProperties;
import java.time.Duration;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
public class SyncCommit {
public static void main(String[] args) {
KafkaConsumer<Long, String> consumer = new KafkaConsumer<>(KafkaConfigProperties.getProperties());
consumer.subscribe(KafkaConfigProperties.getTopic());
ConsumerRecords<Long, String> messages = consumer.poll(Duration.ofSeconds(10));
for (ConsumerRecord<Long, String> message : messages) {
// processed message
consumer.commitSync();
}
}
}

View File

@ -0,0 +1,31 @@
package com.baeldung.kafka.commitoffset.config;
import java.util.ArrayList;
import java.util.Properties;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.common.serialization.StringDeserializer;
/**
* @author amitkumar
*/
public class KafkaConfigProperties {
public static final String MY_TOPIC = "my-topic";
public static Properties getProperties() {
Properties props = new Properties();
props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, "false");
props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
props.put(ConsumerConfig.GROUP_ID_CONFIG, "MyFirstConsumer");
props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
return props;
}
public static ArrayList<String> getTopic() {
ArrayList<String> topics = new ArrayList<>();
topics.add(MY_TOPIC);
return topics;
}
}

View File

@ -0,0 +1,11 @@
<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>

View File

@ -24,14 +24,12 @@
<artifactId>camel-core</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-test-junit5</artifactId>
<version>${camel.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-main</artifactId>

View File

@ -5,8 +5,8 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>apache-spark</artifactId>
<version>1.0-SNAPSHOT</version>
<name>apache-spark</name>
<packaging>jar</packaging>
<name>apache-spark</name>
<parent>
<groupId>com.baeldung</groupId>

View File

@ -5,8 +5,8 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>apache-thrift</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>apache-thrift</name>
<packaging>pom</packaging>
<name>apache-thrift</name>
<parent>
<groupId>com.baeldung</groupId>

View File

@ -5,8 +5,8 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>apache-velocity</artifactId>
<version>0.1-SNAPSHOT</version>
<name>apache-velocity</name>
<packaging>war</packaging>
<name>apache-velocity</name>
<parent>
<groupId>com.baeldung</groupId>

View File

@ -5,8 +5,8 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>aws-dynamodb</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>aws-dynamodb</name>
<packaging>jar</packaging>
<name>aws-dynamodb</name>
<parent>
<groupId>com.baeldung</groupId>

View File

@ -4,8 +4,8 @@
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>lambda-function</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>lambda-function</name>
<version>0.1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>

View File

@ -4,8 +4,8 @@
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>aws-lambda-modules</artifactId>
<name>aws-lambda-modules</name>
<packaging>pom</packaging>
<name>aws-lambda-modules</name>
<parent>
<groupId>com.baeldung</groupId>

View File

@ -3,8 +3,8 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>ShippingFunction</artifactId>
<version>1.0</version>
<name>ShippingFunction</name>
<version>1.0</version>
<packaging>jar</packaging>
<parent>

View File

@ -4,8 +4,8 @@
<modelVersion>4.0.0</modelVersion>
<groupId>helloworld</groupId>
<artifactId>ToDoFunction</artifactId>
<version>1.0</version>
<name>ToDoFunction</name>
<version>1.0</version>
<packaging>jar</packaging>
<parent>

View File

@ -5,8 +5,8 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>aws-miscellaneous</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>aws-miscellaneous</name>
<packaging>jar</packaging>
<name>aws-miscellaneous</name>
<parent>
<groupId>com.baeldung</groupId>

View File

@ -5,8 +5,8 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>aws-s3</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>aws-s3</name>
<packaging>jar</packaging>
<name>aws-s3</name>
<parent>
<groupId>com.baeldung</groupId>
@ -26,7 +26,6 @@
<version>${aws.java.sdk.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
@ -56,7 +55,6 @@
<version>${com.adobe.testing.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>

View File

@ -4,6 +4,7 @@
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>aws-modules</artifactId>
<packaging>pom</packaging>
<name>aws-modules</name>
<parent>
@ -12,16 +13,6 @@
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-dynamodb</artifactId>
<version>1.12.523</version>
<scope>compile</scope>
</dependency>
</dependencies>
<packaging>pom</packaging>
<modules>
<module>aws-app-sync</module>
<module>aws-dynamodb</module>
@ -31,6 +22,15 @@
<module>aws-s3</module>
</modules>
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-dynamodb</artifactId>
<version>1.12.523</version>
<scope>compile</scope>
</dependency>
</dependencies>
<properties>
<aws-java-sdk.version>1.12.331</aws-java-sdk.version>
<aws-java-sdk-v2.version>2.24.9</aws-java-sdk-v2.version>

View File

@ -5,8 +5,8 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>azure</artifactId>
<version>0.1</version>
<name>azure</name>
<packaging>war</packaging>
<name>azure</name>
<description>Demo project for Spring Boot on Azure</description>
<parent>

View File

@ -13,4 +13,5 @@
- [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)
- [Check if a Given Time Lies Between Two Times Regardless of Date](https://www.baeldung.com/java-check-between-two-times)
- [[<-- Prev]](/core-java-modules/core-java-datetime-java8-1)

View File

@ -0,0 +1,45 @@
package com.baeldung.checkiftimebetweentwotimes;
import org.junit.Test;
import java.time.LocalTime;
import java.util.Calendar;
import java.util.Date;
import static org.junit.Assert.assertTrue;
public class CheckIfTimeBetweenTwoTimesUnitTest {
private LocalTime startTime = LocalTime.parse("09:00:00");
private LocalTime endTime = LocalTime.parse("17:00:00");
private LocalTime targetTime = LocalTime.parse("12:30:00");
@Test
public void givenLocalTime_whenUsingIsAfterIsBefore_thenTimeIsBetween() {
assertTrue(!targetTime.isBefore(startTime) && !targetTime.isAfter(endTime));
}
@Test
public void givenLocalTime_whenUsingCompareTo_thenTimeIsBetween() {
assertTrue(targetTime.compareTo(startTime) >= 0 && targetTime.compareTo(endTime) <= 0);
}
@Test
public void givenDate_whenUsingAfterBefore_thenTimeIsBetween() {
Calendar startCalendar = Calendar.getInstance();
startCalendar.set(Calendar.HOUR_OF_DAY, 9);
startCalendar.set(Calendar.MINUTE, 0);
Date startTime = startCalendar.getTime();
Calendar endCalendar = Calendar.getInstance();
endCalendar.set(Calendar.HOUR_OF_DAY, 17);
endCalendar.set(Calendar.MINUTE, 0);
Date endTime = endCalendar.getTime();
Calendar targetCalendar = Calendar.getInstance();
targetCalendar.set(Calendar.HOUR_OF_DAY, 12);
targetCalendar.set(Calendar.MINUTE, 30);
Date targetTime = targetCalendar.getTime();
assertTrue(!targetTime.before(startTime) && !targetTime.after(endTime));
}
}

View File

@ -4,8 +4,8 @@
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-arrays-convert</artifactId>
<name>core-java-arrays-convert</name>
<packaging>jar</packaging>
<name>core-java-arrays-convert</name>
<parent>
<artifactId>core-java-modules</artifactId>
@ -20,4 +20,5 @@
<version>${commons-lang3.version}</version>
</dependency>
</dependencies>
</project>

View File

@ -4,8 +4,8 @@
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-arrays-guides</artifactId>
<name>core-java-arrays-guides</name>
<packaging>jar</packaging>
<name>core-java-arrays-guides</name>
<parent>
<artifactId>core-java-modules</artifactId>

View File

@ -4,8 +4,8 @@
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-arrays-multidimensional</artifactId>
<name>core-java-arrays-multidimensional</name>
<packaging>jar</packaging>
<name>core-java-arrays-multidimensional</name>
<parent>
<artifactId>core-java-modules</artifactId>

View File

@ -2,3 +2,4 @@
- [Find the Middle Element of an Array in Java](https://www.baeldung.com/java-array-middle-item)
- [Find the Equilibrium Indexes of an Array in Java](https://www.baeldung.com/java-equilibrium-index-array)
- [Moves Zeros to the End of an Array in Java](https://www.baeldung.com/java-array-sort-move-zeros-end)
- [Finding the Majority Element of an Array in Java](https://www.baeldung.com/java-array-find-majority-element)

View File

@ -4,8 +4,8 @@
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-arrays-operations-advanced-2</artifactId>
<name>core-java-arrays-operations-advanced-2</name>
<packaging>jar</packaging>
<name>core-java-arrays-operations-advanced-2</name>
<parent>
<artifactId>core-java-modules</artifactId>

View File

@ -4,8 +4,8 @@
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-arrays-operations-advanced</artifactId>
<name>core-java-arrays-operations-advanced</name>
<packaging>jar</packaging>
<name>core-java-arrays-operations-advanced</name>
<parent>
<artifactId>core-java-modules</artifactId>

View File

@ -4,8 +4,8 @@
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-arrays-operations-basic-2</artifactId>
<name>core-java-arrays-operations-basic-2</name>
<packaging>jar</packaging>
<name>core-java-arrays-operations-basic-2</name>
<parent>
<artifactId>core-java-modules</artifactId>

View File

@ -4,8 +4,8 @@
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-arrays-operations-basic</artifactId>
<name>core-java-arrays-operations-basic</name>
<packaging>jar</packaging>
<name>core-java-arrays-operations-basic</name>
<parent>
<artifactId>core-java-modules</artifactId>

View File

@ -4,8 +4,8 @@
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-arrays-sorting</artifactId>
<name>core-java-arrays-sorting</name>
<packaging>jar</packaging>
<name>core-java-arrays-sorting</name>
<parent>
<artifactId>core-java-modules</artifactId>

View File

@ -4,8 +4,8 @@
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-booleans</artifactId>
<name>core-java-booleans</name>
<packaging>jar</packaging>
<name>core-java-booleans</name>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>

View File

@ -4,8 +4,8 @@
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-char</artifactId>
<name>core-java-char</name>
<packaging>jar</packaging>
<name>core-java-char</name>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>

View File

@ -4,8 +4,8 @@
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-collections-2</artifactId>
<name>core-java-collections-2</name>
<packaging>jar</packaging>
<name>core-java-collections-2</name>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>

View File

@ -4,8 +4,8 @@
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-collections-3</artifactId>
<name>core-java-collections-3</name>
<packaging>jar</packaging>
<name>core-java-collections-3</name>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>

View File

@ -4,8 +4,8 @@
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-collections-4</artifactId>
<name>core-java-collections-4</name>
<packaging>jar</packaging>
<name>core-java-collections-4</name>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>

View File

@ -4,8 +4,8 @@
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-collections-5</artifactId>
<name>core-java-collections-5</name>
<packaging>jar</packaging>
<name>core-java-collections-5</name>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>

View File

@ -4,3 +4,4 @@ This module contains articles about the Java ArrayList collection
### Relevant Articles:
- [Create an ArrayList with Multiple Object Types](https://www.baeldung.com/java-arraylist-multiple-object-types)
- [Finding the Peak Elements of a List](https://www.baeldung.com/java-list-find-peak)

View File

@ -3,8 +3,8 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-collections-array-list-2</artifactId>
<name>core-java-collections-array-list-2</name>
<packaging>jar</packaging>
<name>core-java-collections-array-list-2</name>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>

View File

@ -0,0 +1,40 @@
package com.baeldung.peakelements;
import java.util.ArrayList;
import java.util.List;
public class MultiplePeakFinder {
public static List<Integer> findPeaks(int[] arr) {
List<Integer> peaks = new ArrayList<>();
if (arr == null || arr.length == 0) {
return peaks;
}
findPeakElements(arr, 0, arr.length - 1, peaks, arr.length);
return peaks;
}
private static void findPeakElements(int[] arr, int low, int high, List<Integer> peaks, int length) {
if (low > high) {
return;
}
int mid = low + (high - low) / 2;
boolean isPeak = (mid == 0 || arr[mid] > arr[mid - 1]) && (mid == length - 1 || arr[mid] > arr[mid + 1]);
boolean isFirstInSequence = mid > 0 && arr[mid] == arr[mid - 1] && arr[mid] > arr[mid + 1];
if (isPeak || isFirstInSequence) {
if (!peaks.contains(arr[mid])) {
peaks.add(arr[mid]);
}
}
findPeakElements(arr, low, mid - 1, peaks, length);
findPeakElements(arr, mid + 1, high, peaks, length);
}
}

View File

@ -0,0 +1,44 @@
package com.baeldung.peakelements;
import java.util.ArrayList;
import java.util.List;
public class PeakElementFinder {
public List<Integer> findPeakElements(int[] arr) {
int n = arr.length;
List<Integer> peaks = new ArrayList<>();
if (n == 0) {
return peaks;
}
for (int i = 0; i < n; i++) {
if (isPeak(arr, i, n)) {
peaks.add(arr[i]);
}
while (i < n - 1 && arr[i] == arr[i + 1]) {
i++;
}
}
return peaks;
}
private boolean isPeak(int[] arr, int index, int n) {
if (index == 0) {
return n > 1 ? arr[index] >= arr[index + 1] : true;
} else if (index == n - 1) {
return arr[index] >= arr[index - 1];
} else if (arr[index] == arr[index + 1] && arr[index] > arr[index - 1]) {
int i = index;
while (i < n - 1 && arr[i] == arr[i + 1]) {
i++;
}
return i == n - 1 || arr[i] > arr[i + 1];
} else {
return arr[index] >= arr[index - 1] && arr[index] >= arr[index + 1];
}
}
}

View File

@ -0,0 +1,30 @@
package com.baeldung.peakelements;
import java.util.OptionalInt;
public class SinglePeakFinder {
public static OptionalInt findSinglePeak(int[] arr) {
int n = arr.length;
if (n < 2) {
return n == 0 ? OptionalInt.empty() : OptionalInt.of(arr[0]);
}
if (arr[0] >= arr[1]) {
return OptionalInt.of(arr[0]);
}
for (int i = 1; i < n - 1; i++) {
if (arr[i] >= arr[i - 1] && arr[i] >= arr[i + 1]) {
return OptionalInt.of(arr[i]);
}
}
if (arr[n - 1] >= arr[n - 2]) {
return OptionalInt.of(arr[n - 1]);
}
return OptionalInt.empty();
}
}

View File

@ -0,0 +1,23 @@
package com.baeldung.peakelements;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
public class MultiplePeakFinderUnitTest {
@Test
void findPeaks_givenArrayOfIntegers_whenValidInput_thenReturnsCorrectPeaks() {
MultiplePeakFinder finder = new MultiplePeakFinder();
int[] array = {1, 13, 7, 0, 4, 1, 4, 45, 50};
List<Integer> peaks = finder.findPeaks(array);
assertEquals(3, peaks.size());
assertTrue(peaks.contains(4));
assertTrue(peaks.contains(13));
assertTrue(peaks.contains(50));
}
}

View File

@ -0,0 +1,46 @@
package com.baeldung.peakelements;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
public class PeakElementFinderUnitTest {
@Test
void findPeakElement_givenArrayOfIntegers_whenValidInput_thenReturnsCorrectPeak() {
PeakElementFinder finder = new PeakElementFinder();
int[] array = {1, 2, 3, 2, 1};
List<Integer> peaks = finder.findPeakElements(array);
assertEquals(1, peaks.size());
assertTrue(peaks.contains(3));
}
@Test
void findPeakElement_givenArrayOfIntegers_whenNoPeaks_thenReturnsEmptyList() {
PeakElementFinder finder = new PeakElementFinder();
int[] array = {};
List<Integer> peaks = finder.findPeakElements(array);
assertEquals(0, peaks.size());
}
@Test
void findPeakElement_givenArrayOfIntegers_whenPeaksAtExtremes_thenReturnsCorrectPeak() {
PeakElementFinder finder = new PeakElementFinder();
int[] array = {5, 2, 1, 3, 4};
List<Integer> peaks = finder.findPeakElements(array);
assertEquals(2, peaks.size());
assertTrue(peaks.contains(5));
assertTrue(peaks.contains(4));
}
@Test
void findPeakElement_givenArrayOfIntegers_whenPlateaus_thenReturnsCorrectPeak() {
PeakElementFinder finder = new PeakElementFinder();
int[] array = {1, 2, 2, 2, 3, 4, 5};
List<Integer> peaks = finder.findPeakElements(array);
assertEquals(1, peaks.size());
assertTrue(peaks.contains(5));
}
}

View File

@ -0,0 +1,32 @@
package com.baeldung.peakelements;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.OptionalInt;
public class SinglePeakFinderUnitTest {
@Test
void findSinglePeak_givenArrayOfIntegers_whenValidInput_thenReturnsCorrectPeak() {
int[] arr = {0, 10, 2, 4, 5, 1};
OptionalInt peak = SinglePeakFinder.findSinglePeak(arr);
assertTrue(peak.isPresent());
assertEquals(10, peak.getAsInt());
}
@Test
void findSinglePeak_givenEmptyArray_thenReturnsEmptyOptional() {
int[] arr = {};
OptionalInt peak = SinglePeakFinder.findSinglePeak(arr);
assertTrue(peak.isEmpty());
}
@Test
void findSinglePeak_givenEqualElementArray_thenReturnsCorrectPeak() {
int[] arr = {-2, -2, -2, -2, -2};
OptionalInt peak = SinglePeakFinder.findSinglePeak(arr);
assertTrue(peak.isPresent());
assertEquals(-2, peak.getAsInt());
}
}

View File

@ -4,8 +4,8 @@
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-collections-array-list</artifactId>
<name>core-java-collections-array-list</name>
<packaging>jar</packaging>
<name>core-java-collections-array-list</name>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>

View File

@ -4,8 +4,8 @@
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-collections-conversions-2</artifactId>
<name>core-java-collections-conversions-2</name>
<packaging>jar</packaging>
<name>core-java-collections-conversions-2</name>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>

View File

@ -4,8 +4,8 @@
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-collections-conversions-3</artifactId>
<name>core-java-collections-conversions-3</name>
<packaging>jar</packaging>
<name>core-java-collections-conversions-3</name>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>

View File

@ -4,8 +4,8 @@
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-collections-conversions</artifactId>
<name>core-java-collections-conversions</name>
<packaging>jar</packaging>
<name>core-java-collections-conversions</name>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>

View File

@ -4,8 +4,8 @@
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-collections-list-2</artifactId>
<name>core-java-collections-list-2</name>
<packaging>jar</packaging>
<name>core-java-collections-list-2</name>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>

View File

@ -4,8 +4,8 @@
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-collections-list-3</artifactId>
<name>core-java-collections-list-3</name>
<packaging>jar</packaging>
<name>core-java-collections-list-3</name>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>

View File

@ -4,8 +4,8 @@
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-collections-list-4</artifactId>
<name>core-java-collections-list-4</name>
<packaging>jar</packaging>
<name>core-java-collections-list-4</name>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>

View File

@ -4,8 +4,8 @@
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-collections-list-5</artifactId>
<name>core-java-collections-list-5</name>
<packaging>jar</packaging>
<name>core-java-collections-list-5</name>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>

View File

@ -4,8 +4,8 @@
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-collections-list-6</artifactId>
<name>core-java-collections-list-6</name>
<packaging>jar</packaging>
<name>core-java-collections-list-6</name>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>

View File

@ -4,8 +4,8 @@
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-collections-list</artifactId>
<name>core-java-collections-list</name>
<packaging>jar</packaging>
<name>core-java-collections-list</name>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>

View File

@ -4,8 +4,8 @@
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-collections-maps-2</artifactId>
<name>core-java-collections-maps-2</name>
<packaging>jar</packaging>
<name>core-java-collections-maps-2</name>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>

View File

@ -4,8 +4,8 @@
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-collections-maps-3</artifactId>
<name>core-java-collections-maps-3</name>
<packaging>jar</packaging>
<name>core-java-collections-maps-3</name>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>

View File

@ -4,8 +4,8 @@
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-collections-maps-4</artifactId>
<name>core-java-collections-maps-4</name>
<packaging>jar</packaging>
<name>core-java-collections-maps-4</name>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>

View File

@ -4,8 +4,8 @@
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-collections-maps-5</artifactId>
<name>core-java-collections-maps-5</name>
<packaging>jar</packaging>
<name>core-java-collections-maps-5</name>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>

View File

@ -4,8 +4,8 @@
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-collections-maps-6</artifactId>
<name>core-java-collections-maps-6</name>
<packaging>jar</packaging>
<name>core-java-collections-maps-6</name>
<parent>
<artifactId>core-java-modules</artifactId>

View File

@ -4,8 +4,8 @@
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-collections-maps</artifactId>
<name>core-java-collections-maps</name>
<packaging>jar</packaging>
<name>core-java-collections-maps</name>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>

View File

@ -4,8 +4,8 @@
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-collections-set-2</artifactId>
<name>core-java-collections-set-2</name>
<packaging>jar</packaging>
<name>core-java-collections-set-2</name>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>

View File

@ -4,8 +4,8 @@
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-collections-set</artifactId>
<name>core-java-collections-set</name>
<packaging>jar</packaging>
<name>core-java-collections-set</name>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>

View File

@ -13,4 +13,5 @@ This module contains articles about Java collections
- [Guide to the Java Queue Interface](https://www.baeldung.com/java-queue)
- [An Introduction to Synchronized Java Collections](https://www.baeldung.com/java-synchronized-collections)
- [Convert an Array of Primitives to a List](https://www.baeldung.com/java-primitive-array-to-list)
- [Adding Elements to a Collection During Iteration](https://www.baeldung.com/java-add-elements-collection)
- More articles: [[next -->]](/core-java-modules/core-java-collections-2)

View File

@ -4,8 +4,8 @@
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-collections</artifactId>
<name>core-java-collections</name>
<packaging>jar</packaging>
<name>core-java-collections</name>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>

View File

@ -5,8 +5,8 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-compiler</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>core-java-compiler</name>
<packaging>jar</packaging>
<name>core-java-compiler</name>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>

View File

@ -4,8 +4,8 @@
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-concurrency-2</artifactId>
<name>core-java-concurrency-2</name>
<packaging>jar</packaging>
<name>core-java-concurrency-2</name>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>

View File

@ -4,8 +4,8 @@
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-concurrency-advanced-2</artifactId>
<name>core-java-concurrency-advanced-2</name>
<packaging>jar</packaging>
<name>core-java-concurrency-advanced-2</name>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>

View File

@ -4,8 +4,8 @@
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-concurrency-advanced-3</artifactId>
<name>core-java-concurrency-advanced-3</name>
<packaging>jar</packaging>
<name>core-java-concurrency-advanced-3</name>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>

View File

@ -4,8 +4,8 @@
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-concurrency-advanced-4</artifactId>
<name>core-java-concurrency-advanced-4</name>
<packaging>jar</packaging>
<name>core-java-concurrency-advanced-4</name>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>

View File

@ -4,3 +4,4 @@
- [Working with Exceptions in Java CompletableFuture](https://www.baeldung.com/java-exceptions-completablefuture)
- [CountDownLatch vs. Semaphore](https://www.baeldung.com/java-countdownlatch-vs-semaphore)
- [Callbacks in ListenableFuture and CompletableFuture](https://www.baeldung.com/java-callbacks-listenablefuture-completablefuture)
- [Guide to ExecutorService vs. CompletableFuture](https://www.baeldung.com/java-executorservice-vs-completablefuture)

View File

@ -2,11 +2,10 @@
<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-concurrency-advanced-5</artifactId>
<name>core-java-concurrency-advanced-5</name>
<packaging>jar</packaging>
<name>core-java-concurrency-advanced-5</name>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>
@ -29,8 +28,8 @@
</build>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>9</maven.compiler.source>
<maven.compiler.target>9</maven.compiler.target>
</properties>
</project>

View File

@ -0,0 +1,64 @@
package com.baeldung.executorservicevscompletablefuture;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.TimeUnit;
public class CompletableFutureDemo {
public static void completableFutureMethod() {
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
return 42;
});
System.out.println(future.join());
}
public static void chainingTaskExample() {
CompletableFuture<Integer> firstTask = CompletableFuture.supplyAsync(() -> {
return 42;
});
CompletableFuture<String> secondTask = firstTask.thenApply(result -> {
return "Result based on Task 1: " + result;
});
System.out.println(secondTask.join());
}
public static void exceptionHandlingExample() {
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
// Simulate a task that might throw an exception
if (true) {
throw new RuntimeException("Something went wrong!");
}
return "Success";
})
.exceptionally(ex -> {
System.err.println("Error in task: " + ex.getMessage());
// Can optionally return a default value
return "Error occurred";
});
future.thenAccept(result -> System.out.println("Result: " + result));
}
public static void timeoutExample() {
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
System.err.println("Task execution timed out!");
}
return "Task completed";
});
CompletableFuture<String> timeoutFuture = future.completeOnTimeout("Timed out!", 2, TimeUnit.SECONDS);
String result = timeoutFuture.join();
System.out.println("Result: " + result);
}
public static void main(String[] args) {
timeoutExample();
}
}

View File

@ -0,0 +1,98 @@
package com.baeldung.executorservicevscompletablefuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
public class ExecutorServiceDemo {
public static void executorServiceMethod() throws ExecutionException, InterruptedException {
ExecutorService executor = Executors.newFixedThreadPool(3);
Future<Integer> future = executor.submit(() -> {
return 42;
});
System.out.println(future.get());
}
public static void chainingTaskExample() {
ExecutorService executor = Executors.newFixedThreadPool(2);
Future<Integer> firstTask = executor.submit(() -> {return 42;});
Future<String> secondTask = executor.submit(() -> {
try {
Integer result = firstTask.get();
return "Result based on Task 1: " + result;
} catch (InterruptedException | ExecutionException e) {
// Handle exception
System.err.println("Error occured: " + e.getMessage());
}
return null;
});
executor.shutdown();
try {
// Wait for the second task to complete and retrieve the result
String result = secondTask.get();
System.out.println(result); // Output: Result based on Task 1: 42
} catch (InterruptedException | ExecutionException e) {
// Handle exception
System.err.println("Error occured: " + e.getMessage());
}
}
public static void exceptionHandlingExample() {
ExecutorService executor = Executors.newFixedThreadPool(2);
Future<String> future = executor.submit(() -> {
// Simulate a task that might throw an exception
if (true) {
throw new RuntimeException("Something went wrong!");
}
return "Success";
});
try {
// This might block the main thread if the task throws an exception
String result = future.get();
System.out.println("Result: " + result);
} catch (InterruptedException | ExecutionException e) {
// Handle exceptions thrown by the task or during retrieval
System.err.println("Error occured: " + e.getMessage());
} finally {
executor.shutdown();
}
}
public static void timeoutExample() {
ExecutorService executor = Executors.newFixedThreadPool(2);
Future<String> future = executor.submit(() -> {
try {
System.out.println("Start");
Thread.sleep(5000);
System.out.println("End");
} catch (InterruptedException e) {
System.err.println("Error occured: " + e.getMessage());
}
return "Task completed";
});
try {
String result = future.get(2, TimeUnit.SECONDS);
System.out.println("Result: " + result);
} catch (TimeoutException e) {
System.err.println("Task execution timed out!");
future.cancel(true);
} catch (Exception e) {
System.err.println("Error occured: " + e.getMessage());
} finally {
executor.shutdown();
}
}
public static void main(String[] args) throws ExecutionException, InterruptedException {
timeoutExample();
}
}

View File

@ -4,8 +4,8 @@
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-concurrency-advanced</artifactId>
<name>core-java-concurrency-advanced</name>
<packaging>jar</packaging>
<name>core-java-concurrency-advanced</name>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>

View File

@ -4,8 +4,8 @@
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-concurrency-basic-2</artifactId>
<name>core-java-concurrency-basic-2</name>
<packaging>jar</packaging>
<name>core-java-concurrency-basic-2</name>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>

View File

@ -4,8 +4,8 @@
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-concurrency-basic-3</artifactId>
<name>core-java-concurrency-basic-3</name>
<packaging>jar</packaging>
<name>core-java-concurrency-basic-3</name>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>

View File

@ -4,8 +4,8 @@
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-concurrency-basic</artifactId>
<name>core-java-concurrency-basic</name>
<packaging>jar</packaging>
<name>core-java-concurrency-basic</name>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>

View File

@ -3,8 +3,8 @@
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-concurrency-collections-2</artifactId>
<name>core-java-concurrency-collections-2</name>
<packaging>jar</packaging>
<name>core-java-concurrency-collections-2</name>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>

View File

@ -4,8 +4,8 @@
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-concurrency-collections</artifactId>
<name>core-java-concurrency-collections</name>
<packaging>jar</packaging>
<name>core-java-concurrency-collections</name>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>

View File

@ -3,8 +3,8 @@
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-concurrency-simple</artifactId>
<name>core-java-concurrency-simple</name>
<packaging>jar</packaging>
<name>core-java-concurrency-simple</name>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>

View File

@ -5,8 +5,8 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-conditionals</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>core-java-conditionals</name>
<packaging>jar</packaging>
<name>core-java-conditionals</name>
<parent>
<groupId>com.baeldung</groupId>

View File

@ -4,8 +4,8 @@
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-console</artifactId>
<name>core-java-console</name>
<packaging>jar</packaging>
<name>core-java-console</name>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>

View File

@ -4,8 +4,8 @@
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-1</artifactId>
<name>core-java-date-operations-1</name>
<packaging>jar</packaging>
<name>core-java-date-operations-1</name>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>

View File

@ -4,8 +4,8 @@
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-2</artifactId>
<name>core-java-date-operations-2</name>
<packaging>jar</packaging>
<name>core-java-date-operations-2</name>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>

View File

@ -4,8 +4,8 @@
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-3</artifactId>
<name>core-java-date-operations-3</name>
<packaging>jar</packaging>
<name>core-java-date-operations-3</name>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>

View File

@ -5,3 +5,4 @@ This module contains articles about date operations in Java.
- [Calculate Number of Weekdays Between Two Dates in Java](https://www.baeldung.com/java-count-weekdays-between-two-dates)
- [Convert Long to Date in Java](https://www.baeldung.com/java-long-date-conversion)
- [Convert Date to Unix Timestamp in Java](https://www.baeldung.com/java-convert-date-unix-timestamp)
- [Checking if a Date Object Equals Yesterday](https://www.baeldung.com/java-date-check-yesterday)

View File

@ -4,8 +4,15 @@
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-4</artifactId>
<packaging>jar</packaging>
<name>core-java-date-operations-4</name>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>
<artifactId>core-java-modules</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<build>
<plugins>
<plugin>
@ -18,13 +25,6 @@
</plugin>
</plugins>
</build>
<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>

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