Merge branch 'eugenp:master' into master

This commit is contained in:
ovidiumihaitacu 2023-10-11 20:59:46 +03:00 committed by GitHub
commit 3e9ea6e299
287 changed files with 5380 additions and 637 deletions

View File

@ -0,0 +1,102 @@
package com.baeldung.algorithms.rotatearray;
/**
* To speed up the rotation, we narrow k rotations to the remainder of k divided by the array length, or k module the array length.
* Therefore, a large rotation number will be translated into the relative smallest rotation.
* All solutions replace the original array, although they might use an extra array to compute the rotation.
*/
public class RotateArray {
private RotateArray() {
throw new IllegalStateException("Rotate array algorithm utility methods class");
}
/**
*
* @param arr array to apply rotation to
* @param k number of rotations
*/
public static void bruteForce(int[] arr, int k) {
checkInvalidInput(arr, k);
k %= arr.length;
int temp;
int previous;
for (int i = 0; i < k; i++) {
previous = arr[arr.length - 1];
for (int j = 0; j < arr.length; j++) {
temp = arr[j];
arr[j] = previous;
previous = temp;
}
}
}
/**
*
* @param arr array to apply rotation to
* @param k number of rotations
*/
public static void withExtraArray(int[] arr, int k) {
checkInvalidInput(arr, k);
int[] extraArray = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
extraArray[(i + k) % arr.length] = arr[i];
}
System.arraycopy(extraArray, 0, arr, 0, arr.length);
}
/**
*
* @param arr array to apply rotation to
* @param k number of rotations
*/
public static void cyclicReplacement(int[] arr, int k) {
checkInvalidInput(arr, k);
k = k % arr.length;
int count = 0;
for (int start = 0; count < arr.length; start++) {
int current = start;
int prev = arr[start];
do {
int next = (current + k) % arr.length;
int temp = arr[next];
arr[next] = prev;
prev = temp;
current = next;
count++;
} while (start != current);
}
}
/**
*
* @param arr array to apply rotation to
* @param k number of rotations
*/
public static void reverse(int[] arr, int k) {
checkInvalidInput(arr, k);
k %= arr.length;
reverse(arr, 0, arr.length - 1);
reverse(arr, 0, k - 1);
reverse(arr, k, arr.length - 1);
}
private static void reverse(int[] nums, int start, int end) {
while (start < end) {
int temp = nums[start];
nums[start] = nums[end];
nums[end] = temp;
start++;
end--;
}
}
private static void checkInvalidInput(int[] arr, int rotation) {
if (rotation < 1 || arr == null)
throw new IllegalArgumentException("Rotation must be greater than zero or array must be not null");
}
}

View File

@ -0,0 +1,124 @@
package com.baeldung.algorithms.rotatearray;
import static com.baeldung.algorithms.rotatearray.RotateArray.bruteForce;
import static com.baeldung.algorithms.rotatearray.RotateArray.cyclicReplacement;
import static com.baeldung.algorithms.rotatearray.RotateArray.reverse;
import static com.baeldung.algorithms.rotatearray.RotateArray.withExtraArray;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Test;
class RotateArrayUnitTest {
private final int[] arr = { 1, 2, 3, 4, 5, 6 };
private final int rotationLtArrayLength = 1;
private final int rotationGtArrayLength = arr.length + 2;
private final int[] ltArrayLengthRotation = { 6, 1, 2, 3, 4, 5 };
private final int[] gtArrayLengthRotation = { 5, 6, 1, 2, 3, 4 };
@Test
void givenInputArray_whenNoRotationOrEmptyArray_thenThrowIllegalArgumentException() {
final int noRotation = 0;
final int someRotation = arr.length - 1;
assertThrows(IllegalArgumentException.class, () -> bruteForce(arr, noRotation));
assertThrows(IllegalArgumentException.class, () -> withExtraArray(arr, noRotation));
assertThrows(IllegalArgumentException.class, () -> cyclicReplacement(arr, noRotation));
assertThrows(IllegalArgumentException.class, () -> reverse(arr, noRotation));
assertThrows(IllegalArgumentException.class, () -> bruteForce(null, someRotation));
assertThrows(IllegalArgumentException.class, () -> withExtraArray(null, someRotation));
assertThrows(IllegalArgumentException.class, () -> cyclicReplacement(null, someRotation));
assertThrows(IllegalArgumentException.class, () -> reverse(null, someRotation));
}
@Test
void givenInputArray_whenUseBruteForceRotationLtArrayLength_thenRotateArrayOk() {
bruteForce(arr, rotationLtArrayLength);
assertArrayEquals(ltArrayLengthRotation, arr);
}
@Test
void givenInputArray_whenUseBruteForceRotationGtArrayLength_thenRotateArrayOk() {
bruteForce(arr, rotationGtArrayLength);
assertArrayEquals(gtArrayLengthRotation, arr);
}
@Test
void givenInputArray_whenUseBruteForceRotationEqArrayLength_thenDoNothing() {
int[] expected = arr.clone();
bruteForce(arr, arr.length);
assertArrayEquals(expected, arr);
}
@Test
void givenInputArray_whenUseExtraArrayRotationLtArrayLength_thenRotateArrayOk() {
withExtraArray(arr, rotationLtArrayLength);
assertArrayEquals(ltArrayLengthRotation, arr);
}
@Test
void givenInputArray_whenUseExtraArrayRotationGtArrayLength_thenRotateArrayOk() {
withExtraArray(arr, rotationGtArrayLength);
assertArrayEquals(gtArrayLengthRotation, arr);
}
@Test
void givenInputArray_whenUseExtraArrayWithRotationEqArrayLength_thenDoNothing() {
int[] clone = arr.clone();
withExtraArray(arr, arr.length);
assertArrayEquals(clone, arr);
}
@Test
void givenInputArray_whenUseCyclicReplacementRotationLtArrayLength_thenRotateArrayOk() {
cyclicReplacement(arr, rotationLtArrayLength);
assertArrayEquals(ltArrayLengthRotation, arr);
}
@Test
void givenInputArray_whenUseCyclicReplacementRotationGtArrayLength_thenRotateArrayOk() {
cyclicReplacement(arr, rotationGtArrayLength);
assertArrayEquals(gtArrayLengthRotation, arr);
}
@Test
void givenInputArray_whenUseCyclicReplacementRotationEqArrayLength_thenDoNothing() {
int[] clone = arr.clone();
cyclicReplacement(arr, arr.length);
assertArrayEquals(clone, arr);
}
@Test
void givenInputArray_whenUseReverseRotationLtArrayLength_thenRotateArrayOk() {
reverse(arr, rotationLtArrayLength);
assertArrayEquals(ltArrayLengthRotation, arr);
}
@Test
void givenInputArray_whenUseReverseRotationGtArrayLength_thenRotateArrayOk() {
reverse(arr, rotationGtArrayLength);
assertArrayEquals(gtArrayLengthRotation, arr);
}
@Test
void givenInputArray_whenUseReverseRotationEqArrayLength_thenDoNothing() {
int[] clone = arr.clone();
reverse(arr, arr.length);
assertArrayEquals(clone, arr);
}
}

View File

@ -13,3 +13,4 @@ You can build the project from the command line using: *mvn clean install*, or i
- [Read Data From the Beginning Using Kafka Consumer API](https://www.baeldung.com/java-kafka-consumer-api-read)
- [Get Partition Count for a Topic in Kafka](https://www.baeldung.com/java-kafka-partition-count-topic)
- [bootstrap-server in Kafka Configuration](https://www.baeldung.com/java-kafka-bootstrap-server)
- [Introduction to Apache Kafka](https://www.baeldung.com/apache-kafka)

View File

@ -0,0 +1,103 @@
package com.baeldung.kafka.multipletopics;
import static org.assertj.core.api.Assertions.assertThat;
import java.time.Duration;
import java.util.Arrays;
import java.util.Properties;
import java.util.concurrent.ExecutionException;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.apache.kafka.common.serialization.StringSerializer;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.KafkaContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.DockerImageName;
// This live test needs a Docker Daemon running so that a kafka container can be created
@Testcontainers
public class MultipleTopicsLiveTest {
private final Logger log = LoggerFactory.getLogger(MultipleTopicsLiveTest.class);
private static final String CARD_PAYMENTS_TOPIC = "card-payments";
private static final String BANK_TRANSFERS_TOPIC = "bank-transfers";
private static KafkaProducer<String, String> producer;
private static KafkaConsumer<String, String> consumer;
@Container
private static final KafkaContainer KAFKA_CONTAINER = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:latest"));
@BeforeAll
static void setup() {
KAFKA_CONTAINER.addExposedPort(9092);
producer = new KafkaProducer<>(getProducerProperties());
consumer = new KafkaConsumer<>(getConsumerProperties());
}
@AfterAll
static void destroy() {
KAFKA_CONTAINER.stop();
}
private static Properties getProducerProperties() {
Properties producerProperties = new Properties();
producerProperties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, KAFKA_CONTAINER.getBootstrapServers());
producerProperties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
producerProperties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
return producerProperties;
}
private static Properties getConsumerProperties() {
Properties consumerProperties = new Properties();
consumerProperties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, KAFKA_CONTAINER.getBootstrapServers());
consumerProperties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
consumerProperties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
consumerProperties.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
consumerProperties.put(ConsumerConfig.GROUP_ID_CONFIG, "payments");
return consumerProperties;
}
@Test
void whenSendingMessagesOnTwoTopics_thenConsumerReceivesMessages() throws Exception {
publishMessages();
consumer.subscribe(Arrays.asList(CARD_PAYMENTS_TOPIC, BANK_TRANSFERS_TOPIC));
int eventsProcessed = 0;
for (ConsumerRecord<String, String> record : consumer.poll(Duration.ofSeconds(10))) {
log.info("Event on topic={}, payload={}", record.topic(), record.value());
eventsProcessed++;
}
assertThat(eventsProcessed).isEqualTo(2);
}
private void publishMessages() throws ExecutionException, InterruptedException {
ProducerRecord<String, String> cardPayment = new ProducerRecord<>(CARD_PAYMENTS_TOPIC, createCardPayment());
producer.send(cardPayment).get();
ProducerRecord<String, String> bankTransfer = new ProducerRecord<>(BANK_TRANSFERS_TOPIC, createBankTransfer());
producer.send(bankTransfer).get();
}
private String createCardPayment() {
return "{\"paymentReference\":\"A184028KM0013790\", \"type\":\"card\", \"amount\":\"275\", \"currency\":\"GBP\"}";
}
private String createBankTransfer() {
return "{\"paymentReference\":\"19ae2-18mk73-009\", \"type\":\"bank\", \"amount\":\"150\", \"currency\":\"EUR\"}";
}
}

3
apache-poi-3/README.md Normal file
View File

@ -0,0 +1,3 @@
## Relevant Articles
- [How To Convert Excel Data Into List Of Java Objects](https://www.baeldung.com/java-convert-excel-data-into-list)
- [Expand Columns with Apache POI](https://www.baeldung.com/java-apache-poi-expand-columns)

View File

@ -5,3 +5,4 @@
- [Collecting Stream Elements into a List in Java](https://www.baeldung.com/java-stream-to-list-collecting)
- [New Features in Java 16](https://www.baeldung.com/java-16-new-features)
- [Guide to Java 8 groupingBy Collector](https://www.baeldung.com/java-groupingby-collector)
- [Value-Based Classes in Java](https://www.baeldung.com/java-value-based-classes)

View File

@ -30,6 +30,11 @@ public final class Point {
return new Point(x, y, z);
}
@Override
public String toString() {
return "Point{" + "x=" + x + ", y=" + y + ", z=" + z + '}';
}
@Override
public boolean equals(Object other) {
if (other == null || getClass() != other.getClass())

View File

@ -1,14 +1,17 @@
package com.baeldung.value_based_class;
import java.util.ArrayList;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;
public class ValueBasedClassUnitTest {
@Test
public void givenAutoboxedAndPrimitive_whenCompared_thenReturnEquals() {
int primitive_a = 125;
Integer obj_a = 125; // this is autoboxed
Assert.assertSame(primitive_a, obj_a);
List<Integer> list = new ArrayList<>();
list.add(1); // this is autoboxed
Assert.assertEquals(list.get(0), Integer.valueOf(1));
}
@Test

View File

View File

@ -0,0 +1,55 @@
<?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-18</artifactId>
<name>core-java-18</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../../</relativePath>
</parent>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<release>${maven.compiler.release}</release>
<compilerArgs>--enable-preview</compilerArgs>
<source>${maven.compiler.source.version}</source>
<target>${maven.compiler.target.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.plugin.version}</version>
<configuration>
<forkCount>1</forkCount>
</configuration>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-api</artifactId>
<version>${surefire.plugin.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.source.version>18</maven.compiler.source.version>
<maven.compiler.target.version>18</maven.compiler.target.version>
<maven.compiler.release>18</maven.compiler.release>
<surefire.plugin.version>3.0.0-M5</surefire.plugin.version>
</properties>
</project>

View File

@ -0,0 +1,27 @@
package com.baeldung.finalization_closeable_cleaner;
import java.io.FileInputStream;
import java.io.IOException;
public class FinalizationExamples {
FileInputStream fis = null;
public void readFileOperationWithFinalization() throws IOException {
try {
fis = new FileInputStream("input.txt");
// perform operation on the file
System.out.println(fis.readAllBytes().length);
} finally {
if (fis != null)
fis.close();
}
}
public void readFileOperationWithTryWith() throws IOException {
try (FileInputStream fis = new FileInputStream("input.txt")) {
// perform operations
System.out.println(fis.readAllBytes().length);
}
}
}

View File

@ -0,0 +1,48 @@
package com.baeldung.finalization_closeable_cleaner;
import java.lang.ref.Cleaner;
public class MyCleanerResourceClass implements AutoCloseable {
private static Resource resource;
private static final Cleaner cleaner = Cleaner.create();
private final Cleaner.Cleanable cleanable;
public MyCleanerResourceClass() {
resource = new Resource();
this.cleanable = cleaner.register(this, new CleaningState());
}
public void useResource() {
// using the resource here
resource.use();
}
@Override
public void close() {
// perform actions to close all underlying resources
this.cleanable.clean();
}
static class CleaningState implements Runnable {
CleaningState() {
// constructor
}
@Override
public void run() {
// some cleanup action
System.out.println("Cleanup done");
}
}
static class Resource {
void use() {
System.out.println("Using the resource");
}
void close() {
System.out.println("Cleanup done");
}
}
}

View File

@ -0,0 +1,25 @@
package com.baeldung.finalization_closeable_cleaner;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class MyCloseableResourceClass implements AutoCloseable {
private final FileInputStream fis;
public MyCloseableResourceClass() throws FileNotFoundException {
this.fis = new FileInputStream("src/main/resources/file.txt");
}
public int getByteLength() throws IOException {
System.out.println("Some operation");
return this.fis.readAllBytes().length;
}
@Override
public void close() throws IOException {
System.out.println("Finalized object");
this.fis.close();
}
}

View File

@ -0,0 +1,24 @@
package com.baeldung.finalization_closeable_cleaner;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class MyFinalizableResourceClass {
private FileInputStream fis;
public MyFinalizableResourceClass() throws FileNotFoundException {
this.fis = new FileInputStream("src/main/resources/file.txt");
}
public int getByteLength() throws IOException {
System.out.println("Some operation");
return this.fis.readAllBytes().length;
}
@Override
protected void finalize() throws Throwable {
System.out.println("Finalized object");
this.fis.close();
}
}

View File

@ -0,0 +1 @@
This is a test file.

View File

@ -0,0 +1,36 @@
package com.baeldung.finalization_closeable_cleaner;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import java.io.IOException;
import org.junit.Assert;
import org.junit.Test;
public class FinalizationCloseableCleanerUnitTest {
@Test
public void givenMyFinalizationResource_whenUsingFinalize_thenShouldClean() {
assertDoesNotThrow(() -> {
MyFinalizableResourceClass mfr = new MyFinalizableResourceClass();
mfr.getByteLength();
});
}
@Test
public void givenMyCleanerResource_whenUsingCleanerAPI_thenShouldClean() {
assertDoesNotThrow(() -> {
try (MyCleanerResourceClass myCleanerResourceClass = new MyCleanerResourceClass()) {
myCleanerResourceClass.useResource();
}
});
}
@Test
public void givenCloseableResource_whenUsingTryWith_thenShouldClose() throws IOException {
int length = 0;
try (MyCloseableResourceClass mcr = new MyCloseableResourceClass()) {
length = mcr.getByteLength();
}
Assert.assertEquals(20, length);
}
}

View File

@ -1,2 +1,5 @@
## Relevant Articles
- [Sequenced Collections in Java 21](https://www.baeldung.com/java-21-sequenced-collections)
- [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)

View File

@ -0,0 +1,3 @@
void main() {
System.out.println("Hello, World!");
}

View File

@ -0,0 +1,7 @@
package com.baeldung.unnamedclasses;
public class HelloWorldChild extends HelloWorldSuper {
void main() {
System.out.println("Hello, World!");
}
}

View File

@ -0,0 +1,7 @@
package com.baeldung.unnamedclasses;
public class HelloWorldSuper {
public static void main(String[] args) {
System.out.println("Hello from the superclass");
}
}

View File

@ -0,0 +1,6 @@
private String getMessage() {
return "Hello, World!";
}
void main() {
System.out.println(getMessage());
}

View File

@ -4,3 +4,4 @@ This module contains articles about Java 9 streams
### Relevant Articles:
- [How to Break from Java Stream forEach](https://www.baeldung.com/java-break-stream-foreach)
- [Creating Stream of Regex Matches](https://www.baeldung.com/java-stream-regex-matches)

View File

@ -0,0 +1,67 @@
package com.baeldung.array.conversions;
import java.util.Arrays;
public class CharArrayToIntArrayUtils {
static int[] usingGetNumericValueMethod(char[] chars) {
if (chars == null) {
return null;
}
int[] ints = new int[chars.length];
for (int i = 0; i < chars.length; i++) {
ints[i] = Character.getNumericValue(chars[i]);
}
return ints;
}
static int[] usingDigitMethod(char[] chars) {
if (chars == null) {
return null;
}
int[] ints = new int[chars.length];
for (int i = 0; i < chars.length; i++) {
ints[i] = Character.digit(chars[i], 10);
}
return ints;
}
static int[] usingStreamApiMethod(char[] chars) {
if (chars == null) {
return null;
}
return new String(chars).chars()
.map(c -> c - 48)
.toArray();
}
static int[] usingParseIntMethod(char[] chars) {
if (chars == null) {
return null;
}
int[] ints = new int[chars.length];
for (int i = 0; i < chars.length; i++) {
ints[i] = Integer.parseInt(String.valueOf(chars[i]));
}
return ints;
}
static int[] usingArraysSetAllMethod(char[] chars) {
if (chars == null) {
return null;
}
int[] ints = new int[chars.length];
Arrays.setAll(ints, i -> Character.getNumericValue(chars[i]));
return ints;
}
}

View File

@ -0,0 +1,54 @@
package com.baeldung.array.conversions;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import org.junit.jupiter.api.Test;
class CharArrayToIntArrayUtilsUnitTest {
@Test
void givenCharArray_whenUsingGetNumericValueMethod_shouldGetIntArray() {
int[] expected = { 2, 3, 4, 5 };
char[] chars = { '2', '3', '4', '5' };
int[] result = CharArrayToIntArrayUtils.usingGetNumericValueMethod(chars);
assertArrayEquals(expected, result);
}
@Test
void givenCharArray_whenUsingDigitMethod_shouldGetIntArray() {
int[] expected = { 1, 2, 3, 6 };
char[] chars = { '1', '2', '3', '6' };
int[] result = CharArrayToIntArrayUtils.usingDigitMethod(chars);
assertArrayEquals(expected, result);
}
@Test
void givenCharArray_whenUsingStreamApi_shouldGetIntArray() {
int[] expected = { 9, 8, 7, 6 };
char[] chars = { '9', '8', '7', '6' };
int[] result = CharArrayToIntArrayUtils.usingStreamApiMethod(chars);
assertArrayEquals(expected, result);
}
@Test
void givenCharArray_whenUsingParseIntMethod_shouldGetIntArray() {
int[] expected = { 9, 8, 7, 6 };
char[] chars = { '9', '8', '7', '6' };
int[] result = CharArrayToIntArrayUtils.usingParseIntMethod(chars);
assertArrayEquals(expected, result);
}
@Test
void givenCharArray_whenUsingArraysSetAllMethod_shouldGetIntArray() {
int[] expected = { 4, 9, 2, 3 };
char[] chars = { '4', '9', '2', '3' };
int[] result = CharArrayToIntArrayUtils.usingArraysSetAllMethod(chars);
assertArrayEquals(expected, result);
}
}

View File

@ -9,3 +9,4 @@ This module contains complete guides about arrays in Java
- [Guide to ArrayStoreException](https://www.baeldung.com/java-arraystoreexception)
- [Creating a Generic Array in Java](https://www.baeldung.com/java-generic-array)
- [Maximum Size of Java Arrays](https://www.baeldung.com/java-arrays-max-size)
- [Merge Two Arrays and Remove Duplicates in Java](https://www.baeldung.com/java-merge-two-arrays-delete-duplicates)

View File

@ -0,0 +1,2 @@
## Relevant Articles
- [Find the Middle Element of an Array in Java](https://www.baeldung.com/java-array-middle-item)

View File

@ -14,4 +14,3 @@ This module contains articles about advanced operations on arrays in Java. They
- [Slicing Arrays in Java](https://www.baeldung.com/java-slicing-arrays)
- [Combining Two or More Byte Arrays](https://www.baeldung.com/java-concatenate-byte-arrays)
- [Calculating the Sum of Two Arrays in Java](https://www.baeldung.com/java-sum-arrays-element-wise)
- [Find the Middle Element of an Array in Java](https://www.baeldung.com/java-array-middle-item)

View File

@ -5,3 +5,4 @@ This module contains articles about Java Character Class
### Relevant Articles:
- [Character#isAlphabetic vs. Character#isLetter](https://www.baeldung.com/java-character-isletter-isalphabetic)
- [Difference Between Javas “char” and “String”](https://www.baeldung.com/java-char-vs-string)
- [Increment Character in Java](https://www.baeldung.com/java-char-sequence)

View File

@ -5,4 +5,6 @@
### Relevant Articles:
- [Introduction to Roaring Bitmap](https://www.baeldung.com/java-roaring-bitmap-intro)
- [Creating Custom Iterator in Java](https://www.baeldung.com/java-creating-custom-iterator)
- [Difference Between Arrays.sort() and Collections.sort()](https://www.baeldung.com/java-arrays-collections-sort-methods)
- [Skipping the First Iteration in Java](https://www.baeldung.com/java-skip-first-iteration)
- More articles: [[<-- prev]](/core-java-modules/core-java-collections-4)

View File

@ -0,0 +1,6 @@
## Core Java Collections ArrayList
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)

View File

@ -0,0 +1,32 @@
<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 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>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>
<artifactId>core-java-modules</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${maven-compiler-plugin.source}</source>
<target>${maven-compiler-plugin.target}</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<maven-compiler-plugin.source>17</maven-compiler-plugin.source>
<maven-compiler-plugin.target>17</maven-compiler-plugin.target>
</properties>
</project>

View File

@ -0,0 +1,51 @@
package com.baeldung.list.multipleobjecttypes;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.function.Predicate;
public class AlternativeMultipeTypeList {
public static void main(String[] args) {
// List of Parent Class
ArrayList<Number> myList = new ArrayList<>();
myList.add(1.2);
myList.add(2);
myList.add(-3.5);
// List of Interface type
ArrayList<Map> diffMapList = new ArrayList<>();
diffMapList.add(new HashMap<>());
diffMapList.add(new TreeMap<>());
diffMapList.add(new LinkedHashMap<>());
// List of Custom Object
ArrayList<CustomObject> objList = new ArrayList<>();
objList.add(new CustomObject("String"));
objList.add(new CustomObject(2));
// List via Functional Interface
List<Object> dataList = new ArrayList<>();
Predicate<Object> myPredicate = inputData -> (inputData instanceof String || inputData instanceof Integer);
UserFunctionalInterface myInterface = (listObj, data) -> {
if (myPredicate.test(data))
listObj.add(data);
else
System.out.println("Skipping input as data not allowed for class: " + data.getClass()
.getSimpleName());
return listObj;
};
myInterface.addToList(dataList, Integer.valueOf(2));
myInterface.addToList(dataList, Double.valueOf(3.33));
myInterface.addToList(dataList, "String Value");
myInterface.printList(dataList);
}
}

View File

@ -0,0 +1,22 @@
package com.baeldung.list.multipleobjecttypes;
public class CustomObject {
String classData;
Integer intData;
CustomObject(String classData) {
this.classData = classData;
}
CustomObject(Integer intData) {
this.intData = intData;
}
public String getClassData() {
return this.classData;
}
public Integer getIntData() {
return this.intData;
}
}

View File

@ -0,0 +1,40 @@
package com.baeldung.list.multipleobjecttypes;
import java.math.BigInteger;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MultipleObjectTypeArrayList {
public static void main(String[] args) {
ArrayList<Object> multiTypeList = new ArrayList<>();
multiTypeList.add(Integer.valueOf(10));
multiTypeList.add(Double.valueOf(11.5));
multiTypeList.add("String Data");
multiTypeList.add(Arrays.asList(1, 2, 3));
multiTypeList.add(new CustomObject("Class Data"));
multiTypeList.add(BigInteger.valueOf(123456789));
multiTypeList.add(LocalDate.of(2023, 9, 19));
for (Object dataObj : multiTypeList) {
if (dataObj instanceof Integer intData)
System.out.println("Integer Data : " + intData);
else if (dataObj instanceof Double doubleData)
System.out.println("Double Data : " + doubleData);
else if (dataObj instanceof String stringData)
System.out.println("String Data : " + stringData);
else if (dataObj instanceof List<?> intList)
System.out.println("List Data : " + intList);
else if (dataObj instanceof CustomObject customObj)
System.out.println("CustomObject Data : " + customObj.getClassData());
else if (dataObj instanceof BigInteger bigIntData)
System.out.println("BigInteger Data : " + bigIntData);
else if (dataObj instanceof LocalDate localDate)
System.out.println("LocalDate Data : " + localDate.toString());
}
}
}

View File

@ -0,0 +1,18 @@
package com.baeldung.list.multipleobjecttypes;
import java.util.List;
@FunctionalInterface
public interface UserFunctionalInterface {
List<Object> addToList(List<Object> list, Object data);
default void printList(List<Object> dataList) {
for (Object data : dataList) {
if (data instanceof String stringData)
System.out.println("String Data: " + stringData);
if (data instanceof Integer intData)
System.out.println("Integer Data: " + intData);
}
}
}

View File

@ -3,3 +3,4 @@
This module contains articles about conversions among Collection types in Java.
### Relevant Articles:
- [Converting HashMap Values to an ArrayList in Java](https://www.baeldung.com/java-hashmap-arraylist)

View File

@ -0,0 +1,2 @@
## Relevant Articles
- [Check if a List Contains a String Element While Ignoring Case](https://www.baeldung.com/java-list-search-case-insensitive)

View File

@ -1,76 +0,0 @@
package java.com.baeldung.objecttomap;
import com.google.gson.Gson;
import org.junit.Assert;
import org.junit.Test;
import wiremock.com.fasterxml.jackson.core.type.TypeReference;
import wiremock.com.fasterxml.jackson.databind.ObjectMapper;
import wiremock.com.google.common.reflect.TypeToken;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
public class ObjectToMapUnitTest {
Employee employee = new Employee("John", 3000.0);
@Test
public void givenJavaObject_whenUsingReflection_thenConvertToMap() throws IllegalAccessException {
Map<String, Object> map = convertUsingReflection(employee);
Assert.assertEquals(employee.getName(), map.get("name"));
Assert.assertEquals(employee.getSalary(), map.get("salary"));
}
private Map<String, Object> convertUsingReflection(Object object) throws IllegalAccessException {
Map<String, Object> map = new HashMap<>();
Field[] fields = object.getClass().getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
map.put(field.getName(), field.get(object));
}
return map;
}
@Test
public void givenJavaObject_whenUsingJackson_thenConvertToMap() {
ObjectMapper objectMapper = new ObjectMapper();
Map<String, Object> map = objectMapper.convertValue(employee, new TypeReference<Map<String, Object>>() {});
Assert.assertEquals(employee.getName(), map.get("name"));
Assert.assertEquals(employee.getSalary(), map.get("salary"));
}
@Test
public void givenJavaObject_whenUsingGson_thenConvertToMap() {
Gson gson = new Gson();
String json = gson.toJson(employee);
Map<String, Object> map = gson.fromJson(json, new TypeToken<Map<String, Object>>() {}.getType());
Assert.assertEquals(employee.getName(), map.get("name"));
Assert.assertEquals(employee.getSalary(), map.get("salary"));
}
private static class Employee {
private String name;
private Double salary;
public Employee(String name, Double salary) {
this.name = name;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getSalary() {
return salary;
}
public void setSalary(Double age) {
this.salary = salary;
}
}
}

View File

@ -0,0 +1,128 @@
package com.baeldung.objecttomap;
import static org.junit.Assert.assertEquals;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import org.junit.Test;
import java.lang.reflect.Field;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.stream.Collectors;
public class ObjectToMapUnitTest {
Employee employee = new Employee("John", 3000.0);
@Test
public void givenJavaObject_whenUsingReflection_thenConvertToMap() throws IllegalAccessException {
Map<String, Object> map = convertUsingReflection(employee);
assertEquals(employee.getName(), map.get("name"));
assertEquals(employee.getSalary(), map.get("salary"));
}
private Map<String, Object> convertUsingReflection(Object object) throws IllegalAccessException {
Map<String, Object> map = new HashMap<>();
Field[] fields = object.getClass().getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
map.put(field.getName(), field.get(object));
}
return map;
}
@Test
public void givenJavaObject_whenUsingJackson_thenConvertToMap() {
ObjectMapper objectMapper = new ObjectMapper();
Map<String, Object> map = objectMapper.convertValue(employee, new TypeReference<Map<String, Object>>() {});
assertEquals(employee.getName(), map.get("name"));
assertEquals(employee.getSalary(), map.get("salary"));
}
@Test
public void givenJavaObject_whenUsingGson_thenConvertToMap() {
Gson gson = new Gson();
String json = gson.toJson(employee);
Map<String, Object> map = gson.fromJson(json, new TypeToken<Map<String, Object>>() {}.getType());
assertEquals(employee.getName(), map.get("name"));
assertEquals(employee.getSalary(), map.get("salary"));
}
@Test
public void given_UnsortedMap_whenSortingByValueDescending_thenValuesAreInDescendingOrder() {
Map<String, Integer> unsortedMap = new HashMap<>();
unsortedMap.put("one", 1);
unsortedMap.put("three", 3);
unsortedMap.put("five", 5);
unsortedMap.put("two", 2);
unsortedMap.put("four", 4);
Map<String, Integer> sortedMap = sortMapByValueDescending(unsortedMap);
assertEquals(5, sortedMap.size());
final Iterator<Integer> iterator = sortedMap.values().iterator();
assertEquals(5, (int) iterator.next());
assertEquals(4, (int) iterator.next());
assertEquals(3, (int) iterator.next());
assertEquals(2, (int) iterator.next());
assertEquals(1, (int) iterator.next());
}
@Test
public void given_UnsortedMap_whenUsingTreeMap_thenKeysAreInDescendingOrder() {
SortedMap<String, Integer> treeMap = new TreeMap<>(Comparator.reverseOrder());
treeMap.put("one", 1);
treeMap.put("three", 3);
treeMap.put("five", 5);
treeMap.put("two", 2);
treeMap.put("four", 4);
assertEquals(5, treeMap.size());
final Iterator<String> iterator = treeMap.keySet().iterator();
assertEquals("two", iterator.next());
assertEquals("three", iterator.next());
assertEquals("one", iterator.next());
assertEquals("four", iterator.next());
assertEquals("five", iterator.next());
}
private static class Employee {
private String name;
private Double salary;
public Employee(String name, Double salary) {
this.name = name;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getSalary() {
return salary;
}
public void setSalary(Double age) {
this.salary = salary;
}
}
public static <K, V extends Comparable<? super V>> Map<K, V> sortMapByValueDescending(Map<K, V> map) {
return map.entrySet().stream()
.sorted(Map.Entry.<K, V>comparingByValue().reversed())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
}
}

View File

@ -0,0 +1 @@
## Relevant Articles

View File

@ -0,0 +1,78 @@
<?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-collections-maps-7</artifactId>
<name>core-java-collections-maps-7</name>
<packaging>jar</packaging>
<parent>
<artifactId>core-java-modules</artifactId>
<groupId>com.baeldung.core-java-modules</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<properties>
<spring.version>5.2.5.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.4</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>1.36</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.9</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20230227</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,60 @@
package com.baeldung.map;
import java.util.HashMap;
import java.util.Map;
public class ConvertHashMapStringToHashMapObjectUsingtoString {
public String name;
public int age;
public ConvertHashMapStringToHashMapObjectUsingtoString(String name, int age) {
this.name = name;
this.age = age;
}
public static ConvertHashMapStringToHashMapObjectUsingtoString deserializeCustomObject(String valueString) {
if (valueString.startsWith("{") && valueString.endsWith("}")) {
valueString = valueString.substring(1, valueString.length() - 1);
String[] parts = valueString.split(",");
String name = null;
int age = -1;
for (String part : parts) {
String[] keyValue = part.split("=");
if (keyValue.length == 2) {
String key = keyValue[0].trim();
String val = keyValue[1].trim();
if (key.equals("name")) {
name = val;
} else if (key.equals("age")) {
age = Integer.parseInt(val);
}
}
}
if (name != null && age >= 0) {
return new ConvertHashMapStringToHashMapObjectUsingtoString(name, age);
}
}
return new ConvertHashMapStringToHashMapObjectUsingtoString("", -1);
}
public static void main(String[] args) {
String hashMapString = "{key1={name=John, age=30}, key2={name=Alice, age=25}}";
String keyValuePairs = hashMapString.replaceAll("[{}\\s]", "");
String[] pairs = keyValuePairs.split(",");
Map<String, ConvertHashMapStringToHashMapObjectUsingtoString> actualHashMap = new HashMap<>();
for (String pair : pairs) {
String[] keyValue = pair.split("=");
if (keyValue.length == 2) {
String key = keyValue[0];
ConvertHashMapStringToHashMapObjectUsingtoString value = deserializeCustomObject(keyValue[1]);
actualHashMap.put(key, value);
}
}
System.out.println(actualHashMap);
}
@Override
public String toString() {
return "{name=" + name + ", age=" + age + "}";
}
}

View File

@ -0,0 +1,31 @@
package com.baeldung.map;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
class ConvertHashMapStringToHashMapObjectUsingtoStringUnitTest {
@Test
void givenValidCustomObject_whenSerializing_thenSerializedStringIsCorrect() {
ConvertHashMapStringToHashMapObjectUsingtoString customObject = new ConvertHashMapStringToHashMapObjectUsingtoString("John", 30);
String expectedSerializedString = "{name=John, age=30}";
assertEquals(expectedSerializedString, customObject.toString());
}
@Test
void givenValidSerializedString_whenDeserializing_thenCustomObjectIsCorrect() {
String serializedString = "{name=Alice, age=25}";
ConvertHashMapStringToHashMapObjectUsingtoString customObject = ConvertHashMapStringToHashMapObjectUsingtoString.deserializeCustomObject(serializedString);
assertEquals("Alice", customObject.name);
assertEquals(25, customObject.age);
}
@Test
void givenInvalidSerializedString_whenDeserializing_thenDefaultCustomObjectIsCreated() {
String invalidSerializedString = "{invalidString}";
ConvertHashMapStringToHashMapObjectUsingtoString customObject = ConvertHashMapStringToHashMapObjectUsingtoString.deserializeCustomObject(invalidSerializedString);
assertEquals("", customObject.name);
assertEquals(-1, customObject.age);
}
}

View File

@ -12,10 +12,10 @@ public class EpochTimeToLocalDateTimeConverterUnitTest {
@Test
public void testConvertEpochTimeToLocalDateTime() {
long epochTimeMillis = 1624962431000L; // Example epoch time in milliseconds
LocalDateTime expectedDateTime = LocalDateTime.of(2021, 6, 29, 12, 13, 51);
LocalDateTime expectedDateTime = LocalDateTime.of(2021, 6, 29, 10, 27, 11);
Instant instant = Instant.ofEpochMilli(epochTimeMillis);
ZoneId zoneId = ZoneId.systemDefault();
ZoneId zoneId = ZoneId.of("UTC");
LocalDateTime actualDateTime = instant.atZone(zoneId).toLocalDateTime();
assertEquals(expectedDateTime, actualDateTime);

View File

@ -3,4 +3,4 @@
### Relevant Articles:
- [Introduction to Javadoc](http://www.baeldung.com/javadoc)
- [Code Snippets in Java API Documentation](https://www.baeldung.com/java-doc-code-snippets)

View File

@ -0,0 +1,2 @@
test-link*
0.*

View File

@ -0,0 +1,8 @@
## Core Java IO
This module contains articles about core Java input and output (IO)
### Relevant Articles:
- [Get File Extension From MIME Type in Java](https://www.baeldung.com/java-mime-type-file-extension)
- [[<-- Prev]](/core-java-modules/core-java-io-4)

View File

@ -0,0 +1,66 @@
<?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-io-5</artifactId>
<name>core-java-io-5</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>
<!-- Mime Type Resolution Libraries -->
<dependency>
<groupId>org.apache.tika</groupId>
<artifactId>tika-core</artifactId>
<version>${tika.version}</version>
</dependency>
<dependency>
<groupId>net.sf.jmimemagic</groupId>
<artifactId>jmimemagic</artifactId>
<version>${jmime-magic.version}</version>
</dependency>
<dependency>
<groupId>org.jodd</groupId>
<artifactId>jodd-util</artifactId>
<version>${jodd-util.version}</version>
</dependency>
<dependency>
<groupId>com.j256.simplemagic</groupId>
<artifactId>simplemagic</artifactId>
<version>${simplemagic.version}</version>
</dependency>
</dependencies>
<build>
<finalName>core-java-io-5</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<!-- Mime Type Libraries -->
<tika.version>2.8.0</tika.version>
<jmime-magic.version>0.1.5</jmime-magic.version>
<jodd-util.version>6.2.1</jodd-util.version>
<simplemagic.version>1.17</simplemagic.version>
</properties>
</project>

View File

@ -11,10 +11,8 @@ import java.util.Map;
import java.util.Set;
import org.apache.tika.mime.MimeTypeException;
import org.junit.Test;
import com.j256.simplemagic.ContentType;
import org.junit.Test;
public class ExtensionFromMimeTypeUnitTest {
private static final String IMAGE_JPEG_MIME_TYPE = "image/jpeg";
@ -37,14 +35,14 @@ public class ExtensionFromMimeTypeUnitTest {
}
@Test
public void whenUsingMimetypesFileTypeMap_thenGetFileExtension() {
public void whenUsingSimpleMagic_thenGetFileExtension() {
List<String> expectedExtensions = Arrays.asList("jpeg", "jpg", "jpe");
String[] detectedExtensions = ContentType.fromMimeType(IMAGE_JPEG_MIME_TYPE).getFileExtensions();
assertThat(detectedExtensions).containsExactlyElementsOf(expectedExtensions);
}
@Test
public void whenUsingCustomLogic_thenGetFileExtension() {
public void whenUsingCustomMap_thenGetFileExtension() {
Map<String, Set<String>> mimeExtensionsMap = new HashMap<>();
List<String> expectedExtensions = Arrays.asList(".jpg", ".jpe", ".jpeg");
addMimeExtensions(mimeExtensionsMap, "image/jpeg", ".jpg");

View File

@ -0,0 +1,72 @@
package com.baeldung.rmlinebreaks;
import org.junit.jupiter.api.Test;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
public class RemoveLinebreaksUnitTest {
private Path file1Path() throws Exception {
return Paths.get(this.getClass().getClassLoader().getResource("multiple-line-1.txt").toURI());
}
private Path file2Path() throws Exception {
return Paths.get(this.getClass().getClassLoader().getResource("multiple-line-2.txt").toURI());
}
@Test
void whenRemovingLineSeparatorFromFile1_thenGetTheExpectedResult() throws Exception {
String content = Files.readString(file1Path(), StandardCharsets.UTF_8);
String result = content.replace(System.getProperty("line.separator"), "");
assertEquals("A, B, C, D, E, F", result);
}
@Test
void whenRemovingLineSeparatorFromFile2_thenNotGetTheExpectedResult() throws Exception {
String content = Files.readString(file2Path(), StandardCharsets.UTF_8);
String result = content.replace(System.getProperty("line.separator"), "");
assertNotEquals("A, B, C, D, E, F", result); // <-- NOT equals assertion!
}
@Test
void whenRemovingAllLinebreaks_thenGetTheExpectedResult() throws Exception {
String content1 = Files.readString(file1Path(), StandardCharsets.UTF_8);
// file contains CRLF
String content2 = Files.readString(file2Path(), StandardCharsets.UTF_8);
String result1 = content1.replace("\r", "").replace("\n", "");
String result2 = content2.replace("\r", "").replace("\n", "");
assertEquals("A, B, C, D, E, F", result1);
assertEquals("A, B, C, D, E, F", result2);
String resultReplaceAll = content2.replaceAll("[\\n\\r]", "");
assertEquals("A, B, C, D, E, F", resultReplaceAll);
}
@Test
void whenUsingReadAllLinesAndJoin_thenGetExpectedResult() throws Exception {
List<String> lines1 = Files.readAllLines(file1Path(), StandardCharsets.UTF_8);
// file contains CRLF
List<String> lines2 = Files.readAllLines(file2Path(), StandardCharsets.UTF_8);
String result1 = String.join("", lines1);
String result2 = String.join("", lines2);
assertEquals("A, B, C, D, E, F", result1);
assertEquals("A, B, C, D, E, F", result2);
}
}

View File

@ -0,0 +1,6 @@
A,
B,
C,
D,
E,
F

View File

@ -0,0 +1,4 @@
A, B,
C,
D, E,
F

View File

@ -7,3 +7,4 @@ This module contains articles about core Java input/output(IO) APIs.
- [Get the Desktop Path in Java](https://www.baeldung.com/java-desktop-path)
- [Check if a File Is Empty in Java](https://www.baeldung.com/java-check-file-empty)
- [Converting Relative to Absolute Paths in Java](https://www.baeldung.com/java-from-relative-to-absolute-paths)
- [Detect EOF in Java](https://www.baeldung.com/java-file-detect-end-of-file)

View File

@ -43,17 +43,6 @@
<version>${angus-activation.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jodd</groupId>
<artifactId>jodd-util</artifactId>
<version>${jodd-util.version}</version>
</dependency>
<dependency>
<groupId>com.j256.simplemagic</groupId>
<artifactId>simplemagic</artifactId>
<version>${simplemagic.version}</version>
</dependency>
</dependencies>
<build>
@ -153,8 +142,6 @@
<fscontext.version>4.4.2</fscontext.version>
<jakarta-activation-api.version>2.1.2</jakarta-activation-api.version>
<angus-activation.version>2.0.1</angus-activation.version>
<jodd-util.version>6.2.1</jodd-util.version>
<simplemagic.version>1.17</simplemagic.version>
</properties>
</project>

View File

@ -2,10 +2,15 @@ package com.baeldung.networking.url;
import static java.util.stream.Collectors.toList;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;
import org.apache.http.client.utils.URIBuilder;
@ -149,4 +154,24 @@ public class UrlUnitTest {
assertEquals("http://baeldung.com:9090/articles?topic=java&version=8", url.toString());
}
@Test
public void givenURI_whenConvertingToURL_thenCorrect() throws IOException, URISyntaxException {
String aURIString = "http://courses.baeldung.com";
URI uri = new URI(aURIString);
URL url = uri.toURL();
assertNotNull(url);
assertEquals(aURIString, url.toString());
}
@Test
public void givenPath_whenConvertingToURIAndThenURL_thenCorrect() throws IOException, URISyntaxException {
String finalPath = "file:/D:/baeldung/java-url";
Path path = Paths.get("/baeldung/java-url");
URI uri = path.toUri();
URL url = uri.toURL();
assertNotNull(url);
// Adapt the finalPath value to match your own path
// assertEquals(finalPath, url.toString());
}
}

View File

@ -1,6 +1,8 @@
package com.baeldung.integerToBinary;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class IntegerToBinaryUnitTest {
@ -24,4 +26,18 @@ public class IntegerToBinaryUnitTest {
String binaryString = Integer.toString(n, 2);
assertEquals("111", binaryString);
}
@Test
public void givenAnInteger_whenFormatAndReplaceCalled_thenZeroPaddedBinaryString() {
int n = 7;
String binaryString = String.format("%8s", Integer.toBinaryString(n)).replace(" ", "0");
assertEquals("00000111", binaryString);
}
@Test
public void givenAnInteger_whenUsingApacheStringUtils_thenZeroPaddedBinaryString() {
int n = 7;
String binaryString = StringUtils.leftPad(Integer.toBinaryString(n), 8, "0");
assertEquals("00000111", binaryString);
}
}

View File

@ -4,4 +4,5 @@
- [Integer.class vs Integer.TYPE vs int.class](https://www.baeldung.com/java-integer-class-vs-type-vs-int)
- [Does Java Read Integers in Little Endian or Big Endian?](https://www.baeldung.com/java-integers-little-big-endian)
- [How to Split an Integer Number Into Digits in Java](https://www.baeldung.com/java-integer-individual-digits)
- [Java Double vs. BigDecimal](https://www.baeldung.com/java-double-vs-bigdecimal)
- More articles: [[<-- prev]](../core-java-numbers-5)

View File

@ -25,8 +25,12 @@
<version>${commons-codec}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
</dependencies>
<build>
<finalName>core-java-numbers-6</finalName>
<resources>
@ -39,5 +43,6 @@
<properties>
<commons-codec>1.15</commons-codec>
<guava.version>32.1.2-jre</guava.version>
</properties>
</project>

View File

@ -0,0 +1,8 @@
package com.baeldung.bigintegerroot;
public class BenchmarkRunner {
public static void main(String[] args) throws Exception {
org.openjdk.jmh.Main.main(args);
}
}

View File

@ -0,0 +1,10 @@
package com.baeldung.bigintegerroot;
public class BigIntegerHolder {
private BigIntegerHolder() {
}
public static final String BIG_NUMBER = "179769313000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
public static final String VERY_BIG_NUMBER = "32473927492374927934284792374923749237498723048012834324739274923749279342847923749237492374987230480128343247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834324739274923749279342847923749237492374987230480128343247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834324739274923749279342847923749237492374987230480128343247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834324739274923749279342847923749237492374987230480128343247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834324739274923749279342847923749237492374987230480128343247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834324739274923749279342847923749237492374987230480128343247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834324739274923749279342847923749237492374987230480128343247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834324739274923749279342847923749237492374987230480128343247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834324739274923749279342847923749237492374987230480128343247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834324739274923749279342847923749237492374987230480128343247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834324739274923749279342847923749237492374987230480128343247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834";
public static final String INSANELY_BIG_NUMBER = "3247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834324739274923749279342847923749237492374987230480128343247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834324739274923749279342847923749237492374987230480128343247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834324739274923749279342847923749237492374987230480128343247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834324739274923749279342847923749237492374987230480128343247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834324739274923749279342847923749237492374987230480128343247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834324739274923749279342847923749237492374987230480128343247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834324739274923749279342847923749237492374987230480128343247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834324739274923749279342847923749237492374987230480128343247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834324739274923749279342847923749237492374987230480128343247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834324739274923749279342847923749237492374987230480128343247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834324739274923749279342847923749237492374987230480128343247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834324739274923749279342847923749237492374987230480128343247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834324739274923749279342847923749237492374987230480128343247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834324739274923749279342847923749237492374987230480128343247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834324739274923749279342847923749237492374987230480128343247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834324739274923749279342847923749237492374987230480128343247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834324739274923749279342847923749237492374987230480128343247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834324739274923749279342847923749237492374987230480128343247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834324739274923749279342847923749237492374987230480128343247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834324739274923749279342847923749237492374987230480128343247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834324739274923749279342847923749237492374987230480128343247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834324739274923749279342847923749237492374987230480128343247392749237492793428479237492374923749872304801283432473927492374927934284792374923749237498723048012834";
}

View File

@ -0,0 +1,60 @@
package com.baeldung.bigintegerroot;
import static com.baeldung.bigintegerroot.BigIntegerHolder.*;
import com.baeldung.bigintegerroot.algorithms.Newton;
import com.baeldung.bigintegerroot.algorithms.NewtonPlus;
import com.google.common.math.BigIntegerMath;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.infra.Blackhole;
@Warmup(iterations = 1)
@Measurement(iterations = 1, time = 1, timeUnit = TimeUnit.MINUTES)
@Fork(1)
@State(Scope.Benchmark)
public class BigIntegerSquareRootBenchmark {
@Param({BIG_NUMBER, VERY_BIG_NUMBER, INSANELY_BIG_NUMBER})
public String number;
@Benchmark
@BenchmarkMode(Mode.Throughput)
public void calculateRootWithJava(Blackhole blackhole) {
final BigInteger integer = new BigInteger(number);
final BigInteger root = integer.sqrt();
blackhole.consume(root);
}
@Benchmark
@BenchmarkMode(Mode.Throughput)
public void calculateRootWithGuava(Blackhole blackhole) {
final BigInteger integer = new BigInteger(number);
final BigInteger root = BigIntegerMath.sqrt(integer, RoundingMode.DOWN);
blackhole.consume(root);
}
@Benchmark
@BenchmarkMode(Mode.Throughput)
public void calculateRootWithNewtonPlus(Blackhole blackhole) {
final BigInteger integer = new BigInteger(number);
final BigInteger root = NewtonPlus.sqrt(integer);
blackhole.consume(root);
}
@Benchmark
@BenchmarkMode(Mode.Throughput)
public void calculateRootWithNewton(Blackhole blackhole) {
final BigInteger integer = new BigInteger(number);
final BigInteger root = Newton.sqrt(integer);
blackhole.consume(root);
}
}

View File

@ -0,0 +1,29 @@
package com.baeldung.bigintegerroot.algorithms;
import java.math.BigInteger;
public class Newton {
private Newton() {
}
public static BigInteger sqrt(BigInteger n) {
// Initial approximation
BigInteger x = n.divide(BigInteger.TWO);
// Tolerance level (small positive integer)
BigInteger tolerance = BigInteger.ONE;
while (true) {
// x_new = 0.5 * (x + n / x)
BigInteger xNew = x.add(n.divide(x)).divide(BigInteger.TWO);
// Check for convergence within tolerance
if (x.subtract(xNew).abs().compareTo(tolerance) <= 0) {
return xNew;
}
x = xNew;
}
}
}

View File

@ -0,0 +1,108 @@
package com.baeldung.bigintegerroot.algorithms;
import java.math.BigInteger;
public class NewtonPlus {
private NewtonPlus() {
}
// A fast square root by Ryan Scott White.
public static BigInteger sqrt(BigInteger x) {
if (x.compareTo(BigInteger.valueOf(144838757784765629L)) < 0) {
long xAsLong = x.longValue();
long vInt = (long)Math.sqrt(xAsLong);
if (vInt * vInt > xAsLong)
vInt--;
return BigInteger.valueOf(vInt); }
double xAsDub = x.doubleValue();
BigInteger val;
if (xAsDub < 2.1267e37) // 2.12e37 largest here
// since sqrt(long.max*long.max) > long.max
{
long vInt = (long)Math.sqrt(xAsDub);
val = BigInteger.valueOf
((vInt + x.divide(BigInteger.valueOf(vInt)).longValue()) >> 1);
}
else if (xAsDub < 4.3322e127) {
// Convert a double to a BigInteger
long bits = Double.doubleToLongBits(Math.sqrt(xAsDub));
int exp = ((int) (bits >> 52) & 0x7ff) - 1075;
val = BigInteger.valueOf((bits & ((1L << 52)) - 1) | (1L << 52)).shiftLeft(exp);
val = x.divide(val).add(val).shiftRight(1);
if (xAsDub > 2e63) {
val = x.divide(val).add(val).shiftRight(1); }
}
else // handle large numbers over 4.3322e127
{
int xLen = x.bitLength();
int wantedPrecision = ((xLen + 1) / 2);
int xLenMod = xLen + (xLen & 1) + 1;
//////// Do the first Sqrt on Hardware ////////
long tempX = x.shiftRight(xLenMod - 63).longValue();
double tempSqrt1 = Math.sqrt(tempX);
long valLong = Double.doubleToLongBits(tempSqrt1) & 0x1fffffffffffffL;
if (valLong == 0)
valLong = 1L << 53;
//////// Classic Newton Iterations ////////
val = BigInteger.valueOf(valLong).shiftLeft(53 - 1)
.add((x.shiftRight(xLenMod -
(3 * 53))).divide(BigInteger.valueOf(valLong)));
int size = 106;
for (; size < 256; size <<= 1) {
val = val.shiftLeft(size - 1).add(x.shiftRight
(xLenMod - (3*size)).divide(val));}
if (xAsDub > 4e254) { // 4e254 = 1<<845.77
int numOfNewtonSteps = 31 -
Integer.numberOfLeadingZeros(wantedPrecision / size)+1;
////// Apply Starting Size ////////
int wantedSize = (wantedPrecision >> numOfNewtonSteps) + 2;
int needToShiftBy = size - wantedSize;
val = val.shiftRight(needToShiftBy);
size = wantedSize;
do {
//////// Newton Plus Iteration ////////
int shiftX = xLenMod - (3 * size);
BigInteger valSqrd = val.multiply(val).shiftLeft(size - 1);
BigInteger valSU = x.shiftRight(shiftX).subtract(valSqrd);
val = val.shiftLeft(size).add(valSU.divide(val));
size *= 2;
} while (size < wantedPrecision);
}
val = val.shiftRight(size - wantedPrecision);
}
// Detect a round ups. This function can be further optimized - see article.
// For a ~7% speed bump the following line can be removed but round-ups will occur.
if (val.multiply(val).compareTo(x) > 0)
val = val.subtract(BigInteger.ONE);
// Enabling the below will guarantee an error is stopped for larger numbers.
// Note: As of this writing, there are no known errors.
BigInteger tmp = val.multiply(val);
if (tmp.compareTo(x) > 0) {
System.out.println("val^2(" + val.multiply(val).toString()
+ ") ≥ x(" + x.toString()+")");
System.console().readLine();
//throw new Exception("Sqrt function had internal error - value too high");
}
if (tmp.add(val.shiftLeft(1)).add(BigInteger.ONE).compareTo(x) <= 0) {
System.out.println("(val+1)^2("
+ val.add(BigInteger.ONE).multiply(val.add(BigInteger.ONE)).toString()
+ ") ≥ x(" + x.toString() + ")");
System.console().readLine();
//throw new Exception("Sqrt function had internal error - value too low");
}
return val;
}
}

View File

@ -0,0 +1,39 @@
package com.baeldung.bigintegerroot;
import static org.junit.jupiter.api.Assertions.assertTrue;
import com.baeldung.bigintegerroot.algorithms.Newton;
import com.baeldung.bigintegerroot.algorithms.NewtonPlus;
import com.google.common.math.BigIntegerMath;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.util.stream.Stream;
import org.apache.commons.math3.util.Pair;
import org.junit.jupiter.api.condition.EnabledForJreRange;
import org.junit.jupiter.api.condition.JRE;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
class BigIntegerSquareRootUnitTest {
@ParameterizedTest
@ValueSource(strings = {
BigIntegerHolder.BIG_NUMBER,
BigIntegerHolder.VERY_BIG_NUMBER,
BigIntegerHolder.VERY_BIG_NUMBER
})
void squareRootTest(String number) {
final BigInteger bigInteger = new BigInteger(number);
final BigInteger javaRoot = bigInteger.sqrt();
final BigInteger guavaRoot = BigIntegerMath.sqrt(bigInteger, RoundingMode.DOWN);
final BigInteger newtonRoot = Newton.sqrt(bigInteger);
final BigInteger newtonPlusRoot = NewtonPlus.sqrt(bigInteger);
assertTrue(Stream.of(
new Pair<>(javaRoot, guavaRoot),
new Pair<>(guavaRoot, newtonRoot),
new Pair<>(newtonRoot, newtonPlusRoot)
).allMatch(pair -> pair.getFirst().equals(pair.getSecond())));
}
}

View File

@ -0,0 +1,28 @@
package com.baeldung.javadoublevsbigdecimal;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import java.math.BigDecimal;
import java.math.RoundingMode;
import org.junit.jupiter.api.Test;
public class BigDecimalConversionUnitTest {
@Test
void whenConvertingDoubleToBigDecimal_thenConversionIsCorrect() {
double doubleValue = 123.456;
BigDecimal bigDecimalValue = BigDecimal.valueOf(doubleValue);
BigDecimal expected = new BigDecimal("123.456").setScale(3, RoundingMode.HALF_UP);
assertEquals(expected, bigDecimalValue.setScale(3, RoundingMode.HALF_UP));
}
@Test
void givenDecimalPlacesGreaterThan15_whenConvertingBigDecimalToDouble_thenPrecisionIsLost() {
BigDecimal bigDecimalValue = new BigDecimal("789.1234567890123456");
double doubleValue = bigDecimalValue.doubleValue();
BigDecimal convertedBackToBigDecimal = BigDecimal.valueOf(doubleValue);
assertNotEquals(bigDecimalValue, convertedBackToBigDecimal);
}
}

View File

@ -0,0 +1,42 @@
package com.baeldung.javadoublevsbigdecimal;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.math.BigDecimal;
import java.math.RoundingMode;
import org.junit.jupiter.api.Test;
public class BigDecimalUnitTest {
private BigDecimal bigDecimal1 = new BigDecimal("124567890.0987654321");
private BigDecimal bigDecimal2 = new BigDecimal("987654321.123456789");
@Test
public void givenTwoBigDecimals_whenAdd_thenCorrect() {
BigDecimal expected = new BigDecimal("1112222211.2222222211");
BigDecimal actual = bigDecimal1.add(bigDecimal2);
assertEquals(expected, actual);
}
@Test
public void givenTwoBigDecimals_whenMultiply_thenCorrect() {
BigDecimal expected = new BigDecimal("123030014929277547.5030955772112635269");
BigDecimal actual = bigDecimal1.multiply(bigDecimal2);
assertEquals(expected, actual);
}
@Test
public void givenTwoBigDecimals_whenSubtract_thenCorrect() {
BigDecimal expected = new BigDecimal("-863086431.0246913569");
BigDecimal actual = bigDecimal1.subtract(bigDecimal2);
assertEquals(expected, actual);
}
@Test
public void givenTwoBigDecimals_whenDivide_thenCorrect() {
BigDecimal expected = new BigDecimal("0.13");
BigDecimal actual = bigDecimal1.divide(bigDecimal2, 2, RoundingMode.HALF_UP);
assertEquals(expected, actual);
}
}

View File

@ -0,0 +1,15 @@
package com.baeldung.javadoublevsbigdecimal;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
public class JavaDoubleUnitTest {
@Test
public void givenDoubleLiteral_whenAssigningToDoubleVariable_thenValueIsNotExactlyEqual() {
double doubleValue = 0.1;
double epsilon = 0.0000000000000001;
assertEquals(0.1, doubleValue, epsilon);
}
}

View File

@ -18,6 +18,18 @@
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>

View File

@ -0,0 +1,15 @@
package com.baeldung.floatdoubleconversions;
public class FloatAndDoubleConversions {
public static void main(String args[]){
float vatRate = 14.432511f;
System.out.println("vatRate:"+vatRate);
Float localTaxRate = 20.12434f;
System.out.println("localTaxRate:"+localTaxRate);
double shootingAverage = 56.00000000000001;
System.out.println("shootingAverage:"+shootingAverage);
Double assistAverage = 81.123000000045;
System.out.println("assistAverage:"+assistAverage);
}
}

View File

@ -0,0 +1,29 @@
package com.baeldung.floatdoubleconversions;
import org.junit.Assert;
import org.junit.Test;
public class FloatDoubleConversionsTest {
@Test
public void whenDoubleType_thenFloatTypeSuccess(){
double interestRatesYearly = 13.333333333333334;
float interest = (float) interestRatesYearly;
Assert.assertEquals(13.333333f, interest, 0.000004f);
Double monthlyRates = 2.111111111111112;
float rates = monthlyRates.floatValue();
Assert.assertEquals(2.1111112f, rates, 0.00000013);
}
@Test
public void whenFloatType_thenDoubleTypeSuccess(){
float gradeAverage =2.05f;
double average = gradeAverage;
Assert.assertEquals(2.05, average, 0.06);
Float monthlyRates = 2.1111112f;
Double rates = monthlyRates.doubleValue();
Assert.assertEquals(2.11111112, rates, 0.0000002);//true
}
}

View File

@ -0,0 +1,49 @@
package com.baeldung.floattobigdecimal;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import java.math.BigDecimal;
import org.junit.jupiter.api.Test;
class FloatToBigDecimalUnitTest {
@Test
public void whenFloatComparedWithDifferentValues_thenCouldMatch() {
assertNotEquals(1.1f, 1.09f);
assertEquals(1.1f, 1.09999999f);
}
@Test
public void whenCreatedFromFloat_thenMatchesInternallyStoredValue() {
float floatToConvert = 1.10000002384185791015625f;
BigDecimal bdFromFloat = new BigDecimal(floatToConvert);
assertEquals("1.10000002384185791015625", bdFromFloat.toString());
}
@Test
public void whenCreatedFromString_thenPreservesTheOriginal() {
BigDecimal bdFromString = new BigDecimal("1.1");
assertEquals("1.1", bdFromString.toString());
}
@Test
public void whenCreatedFromFloatConvertedToString_thenFloatInternalValueGetsTruncated() {
String floatValue = Float.toString(1.10000002384185791015625f);
BigDecimal bdFromString = new BigDecimal(floatValue);
assertEquals("1.1", floatValue);
assertEquals("1.1", bdFromString.toString());
}
@Test
public void whenCreatedByValueOf_thenFloatValueGetsTruncated() {
assertEquals("1.100000023841858", BigDecimal.valueOf(1.10000002384185791015625f).toString());
}
@Test
public void whenDoubleConvertsFloatToString_thenFloatValueGetsTruncated() {
assertEquals("1.100000023841858", Double.toString(1.10000002384185791015625f));
}
}

View File

@ -14,5 +14,6 @@ This module contains articles about working with the operating system (OS) in Ja
- [How to Run a Shell Command in Java](http://www.baeldung.com/run-shell-command-in-java)
- [Taking Screenshots Using Java](https://www.baeldung.com/java-taking-screenshots)
- [Java Sound API Capturing Microphone](https://www.baeldung.com/java-sound-api-capture-mic)
- [How to Detect the Username Using Java](https://www.baeldung.com/java-get-username)
This module uses Java 9, so make sure to have the JDK 9 installed to run it.

View File

@ -9,4 +9,5 @@
- [Regular Expression: \z vs \Z Anchors in Java](https://www.baeldung.com/java-regular-expression-z-vs-z-anchors)
- [Extract Text Between Square Brackets](https://www.baeldung.com/java-get-content-between-square-brackets)
- [Get the Indexes of Regex Pattern Matches in Java](https://www.baeldung.com/java-indexes-regex-pattern-matches)
- [Check if a String is Strictly Alphanumeric With Java](https://www.baeldung.com/java-check-string-contains-only-letters-numbers)
- More articles: [[<-- prev]](/core-java-modules/core-java-regex)

View File

@ -15,10 +15,8 @@ public class SimpleClient {
SocketFactory factory = SSLSocketFactory.getDefault();
try (Socket connection = factory.createSocket(host, port)) {
((SSLSocket) connection).setEnabledCipherSuites(
new String[] { "TLS_DHE_DSS_WITH_AES_256_CBC_SHA256"});
((SSLSocket) connection).setEnabledProtocols(
new String[] { "TLSv1.2"});
((SSLSocket) connection).setEnabledCipherSuites(new String[] { "TLS_DHE_DSS_WITH_AES_256_CBC_SHA256" });
((SSLSocket) connection).setEnabledProtocols(new String[] { "TLSv1.2" });
SSLParameters sslParams = new SSLParameters();
sslParams.setEndpointIdentificationAlgorithm("HTTPS");
((SSLSocket) connection).setSSLParameters(sslParams);
@ -28,6 +26,7 @@ public class SimpleClient {
}
public static void main(String[] args) throws IOException {
System.setProperty("javax.net.debug", "ssl:handshake");
System.out.println(startClient("localhost", 8443));
}
}

View File

@ -14,11 +14,10 @@ public class SimpleServer {
ServerSocketFactory factory = SSLServerSocketFactory.getDefault();
try (ServerSocket listener = factory.createServerSocket(port)) {
System.setProperty("javax.net.debug", "ssl:handshake");
((SSLServerSocket) listener).setNeedClientAuth(true);
((SSLServerSocket) listener).setEnabledCipherSuites(
new String[] { "TLS_DHE_DSS_WITH_AES_256_CBC_SHA256"});
((SSLServerSocket) listener).setEnabledProtocols(
new String[] { "TLSv1.2"});
((SSLServerSocket) listener).setEnabledCipherSuites(new String[] { "TLS_DHE_DSS_WITH_AES_256_CBC_SHA256" });
((SSLServerSocket) listener).setEnabledProtocols(new String[] { "TLSv1.2" });
while (true) {
try (Socket socket = listener.accept()) {
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
@ -29,6 +28,7 @@ public class SimpleServer {
}
public static void main(String[] args) throws IOException {
System.setProperty("javax.net.debug", "ssl:handshake");
startServer(8443);
}
}

View File

@ -43,6 +43,11 @@
<artifactId>vavr</artifactId>
<version>${vavr.version}</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
</dependencies>
<build>
@ -72,6 +77,7 @@
<maven.compiler.source>12</maven.compiler.source>
<maven.compiler.target>12</maven.compiler.target>
<vavr.version>0.10.2</vavr.version>
<guava.version>32.1.2-jre</guava.version>
</properties>
</project>

View File

@ -0,0 +1,90 @@
package com.baeldung.streams.partitioning;
import java.util.ArrayList;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import com.google.common.collect.Iterables;
public class PartitionStream {
public static <T> Stream<List<T>> partitionList(List<T> source, int batchSize) {
if (batchSize <= 0) {
throw new IllegalArgumentException(String.format("Expected the batchSize to be greater than ZERO, actual value was: %s", batchSize));
}
if (source.isEmpty()) {
return Stream.empty();
}
int nrOfFullBatches = (source.size() - 1) / batchSize;
return IntStream.rangeClosed(0, nrOfFullBatches)
.mapToObj(batch -> {
int startIndex = batch * batchSize;
int endIndex = (batch == nrOfFullBatches) ? source.size() : (batch + 1) * batchSize;
return source.subList(startIndex, endIndex);
});
}
public static <T> Iterable<List<T>> partitionUsingGuava(Stream<T> source, int batchSize) {
return Iterables.partition(source::iterator, batchSize);
}
public static <T> List<List<T>> partitionStream(Stream<T> source, int batchSize) {
return source.collect(partitionBySize(batchSize, Collectors.toList()));
}
public static <T, A, R> Collector<T, ?, R> partitionBySize(int batchSize, Collector<List<T>, A, R> downstream) {
Supplier<Accumulator<T, A>> supplier = () -> new Accumulator<>(
batchSize,
downstream.supplier().get(),
downstream.accumulator()::accept
);
BiConsumer<Accumulator<T, A>, T> accumulator = (acc, value) -> acc.add(value);
BinaryOperator<Accumulator<T, A>> combiner = (acc1, acc2) -> acc1.combine(acc2, downstream.combiner());
Function<Accumulator<T, A>, R> finisher = acc -> {
if (!acc.values.isEmpty()) {
downstream.accumulator().accept(acc.downstreamAccumulator, acc.values);
}
return downstream.finisher().apply(acc.downstreamAccumulator);
};
return Collector.of(supplier, accumulator, combiner, finisher, Collector.Characteristics.UNORDERED);
}
static class Accumulator<T, A> {
private final List<T> values = new ArrayList<>();
private final int batchSize;
private A downstreamAccumulator;
private final BiConsumer<A, List<T>> batchFullListener;
Accumulator(int batchSize, A accumulator, BiConsumer<A, List<T>> onBatchFull) {
this.batchSize = batchSize;
this.downstreamAccumulator = accumulator;
this.batchFullListener = onBatchFull;
}
void add(T value) {
values.add(value);
if (values.size() == batchSize) {
batchFullListener.accept(downstreamAccumulator, new ArrayList<>(values));
values.clear();
}
}
Accumulator<T, A> combine(Accumulator<T, A> other, BinaryOperator<A> accumulatorCombiner) {
this.downstreamAccumulator = accumulatorCombiner.apply(downstreamAccumulator, other.downstreamAccumulator);
other.values.forEach(this::add);
return this;
}
}
}

View File

@ -0,0 +1,82 @@
package com.baeldung.partitioning;
import static com.baeldung.streams.partitioning.PartitionStream.partitionList;
import static com.baeldung.streams.partitioning.PartitionStream.partitionStream;
import static com.baeldung.streams.partitioning.PartitionStream.partitionUsingGuava;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.atIndex;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
public class PartitionStreamsUnitTest {
@Test
void whenPartitionList_thenReturnThreeSubLists() {
List<Integer> source = List.of(1, 2, 3, 4, 5, 6, 7, 8);
Stream<List<Integer>> result = partitionList(source, 3);
assertThat(result)
.containsExactlyInAnyOrder(
List.of(1, 2, 3),
List.of(4, 5, 6),
List.of(7, 8)
);
}
@Test
void whenPartitionEmptyList_thenReturnEmptyStream() {
Stream<List<Integer>> result = partitionList(Collections.emptyList(), 3);
assertThat(result).isEmpty();
}
@Test
void whenPartitionListWithNegativeBatchSize_thenThrowException() {
assertThatThrownBy(() -> partitionList(List.of(1,2,3), -1))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Expected the batchSize to be greater than ZERO, actual value was: -1");
}
@Test
void whenPartitionParallelStream_thenReturnThreeSubLists() {
Stream<Integer> source = Stream.of(1, 2, 3, 4, 5, 6, 7, 8).parallel();
List<List<Integer>> result = partitionStream(source, 3);
assertThat(result)
.hasSize(3)
.satisfies(batch -> assertThat(batch).hasSize(3), atIndex(0))
.satisfies(batch -> assertThat(batch).hasSize(3), atIndex(1))
.satisfies(batch -> assertThat(batch).hasSize(2), atIndex(2));
}
@Test
void whenPartitionEmptyParallelStream_thenReturnEmptyList() {
Stream<Integer> source = Stream.<Integer>empty().parallel();
List<List<Integer>> result = partitionStream(source, 3);
assertThat(result).isEmpty();
}
@Test
void whenPartitionParallelStreamWithGuava_thenReturnThreeSubLists() {
Stream<Integer> source = Stream.of(1, 2, 3, 4, 5, 6, 7, 8).parallel();
Iterable<List<Integer>> result = partitionUsingGuava(source, 3);
assertThat(result)
.map(ArrayList::new)
.hasSize(3)
.satisfies(batch -> assertThat(batch).asList().hasSize(3), atIndex(0))
.satisfies(batch -> assertThat(batch).asList().hasSize(3), atIndex(1))
.satisfies(batch -> assertThat(batch).asList().hasSize(2), atIndex(2));
}
}

View File

@ -12,3 +12,4 @@ This module contains articles about string-related algorithms.
- [Find the First Embedded Occurrence of an Integer in a Java String](https://www.baeldung.com/java-string-find-embedded-integer)
- [Find the Most Frequent Characters in a String](https://www.baeldung.com/java-string-find-most-frequent-characters)
- [Checking If a String Is a Repeated Substring](https://www.baeldung.com/java-repeated-substring)
- [Check if Letter Is Emoji With Java](https://www.baeldung.com/java-check-letter-emoji)

View File

@ -1,11 +1,11 @@
package com.baeldung.charsequence;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class CharSequenceVsStringUnitTest {
@Test
@ -44,4 +44,43 @@ public class CharSequenceVsStringUnitTest {
assertEquals(firstAddressOfTest, secondAddressOfTest);
}
@Test
public void givenCharSequenceAsString_whenConvertingUsingCasting_thenCorrect() {
String expected = "baeldung";
CharSequence charSequence = "baeldung";
String explicitCastedString = (String) charSequence;
assertEquals(expected, charSequence);
assertEquals(expected, explicitCastedString);
}
@Test(expected = ClassCastException.class)
public void givenCharSequenceAsStringBuiler_whenConvertingUsingCasting_thenThrowException() {
CharSequence charSequence = new StringBuilder("baeldung");
String castedString = (String) charSequence;
}
@Test
public void givenCharSequence_whenConvertingUsingToString_thenCorrect() {
String expected = "baeldung";
CharSequence charSequence1 = "baeldung";
CharSequence charSequence2 = new StringBuilder("baeldung");
assertEquals(expected, charSequence1.toString());
assertEquals(expected, charSequence2.toString());
}
@Test
public void givenCharSequence_whenConvertingUsingValueOf_thenCorrect() {
String expected = "baeldung";
CharSequence charSequence1 = "baeldung";
CharSequence charSequence2 = new StringBuilder("baeldung");
CharSequence charSequence3 = null;
assertEquals(expected, String.valueOf(charSequence1));
assertEquals(expected, String.valueOf(charSequence2));
assertEquals("null", String.valueOf(charSequence3));
}
}

View File

@ -11,4 +11,3 @@
- [Check if the First Letter of a String Is a Number](https://www.baeldung.com/java-check-if-string-starts-with-number)
- [Print “” Quotes Around a String in Java](https://www.baeldung.com/java-string-print-quotes)
- [Remove Punctuation From a String in Java](https://www.baeldung.com/java-remove-punctuation-from-string)
- [Replacing Single Quote with \ in Java String](https://www.baeldung.com/java-replacing-single-quote-string)

View File

@ -10,3 +10,5 @@
- [Check if a String Contains Non-Alphanumeric Characters](https://www.baeldung.com/java-string-test-special-characters)
- [Check if a String Has All Unique Characters in Java](https://www.baeldung.com/java-check-string-all-unique-chars)
- [Performance Comparison Between Different Java String Concatenation Methods](https://www.baeldung.com/java-string-concatenation-methods)
- [Replacing Single Quote with \ in Java String](https://www.baeldung.com/java-replacing-single-quote-string)

View File

@ -28,13 +28,14 @@
<!--<module>core-java-streams-2</module> -->
<!--<module>core-java-sun</module> -->
<module>core-java-9-improvements</module>
<module>core-java-collections-array-list</module>
<module>core-java-9-streams</module>
<module>core-java-9</module>
<module>core-java-10</module>
<module>core-java-11</module>
<module>core-java-11-2</module>
<module>core-java-11-3</module>
<module>core-java-collections-array-list</module>
<module>core-java-collections-array-list-2</module>
<module>core-java-collections-list-4</module>
<module>core-java-collections-list-5</module>
<module>core-java-collections-maps-4</module>
@ -82,6 +83,7 @@
<module>core-java-collections-maps</module>
<module>core-java-collections-maps-2</module>
<module>core-java-collections-maps-3</module>
<module>core-java-collections-maps-7</module>
<module>core-java-compiler</module>
<module>core-java-concurrency-2</module>
<module>core-java-concurrency-advanced</module>
@ -109,6 +111,7 @@
<module>core-java-io-2</module>
<module>core-java-io-3</module>
<module>core-java-io-4</module>
<module>core-java-io-5</module>
<module>core-java-io-apis</module>
<module>core-java-io-apis-2</module>
<module>core-java-io-conversions</module>

View File

@ -12,6 +12,7 @@
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>2.33</version>
<relativePath/>
</parent>
<dependencies>

View File

@ -76,6 +76,11 @@
<artifactId>jersey-apache-connector</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>${jersey.version}</version>
</dependency>
</dependencies>
<build>

View File

@ -0,0 +1,61 @@
package com.baeldung.jersey.server.form;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;
import org.glassfish.jersey.media.multipart.FormDataParam;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
@Path("form")
public class FormExampleResource
{
@GET
@Path("/example1")
@Produces({MediaType.TEXT_HTML})
public InputStream getExample1() throws Exception
{
File f = new File("src/main/resources/html/example1.html");
return new FileInputStream(f);
}
@GET
@Path("/example2")
@Produces({MediaType.TEXT_HTML})
public InputStream getExample2() throws Exception
{
File f = new File("src/main/resources/html/example2.html");
return new FileInputStream(f);
}
@POST
@Path("/example1")
public String example1(@FormParam("first_name") String firstName,
@FormParam("last_name") String lastName,
@FormParam("age") String age)
{
return "Got: First = " + firstName + ", Last = " + lastName + ", Age = " + age;
}
@POST
@Path("/example2")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public String example2(@FormDataParam("first_name") String firstName,
@FormDataParam("last_name") String lastName,
@FormDataParam("age") String age,
@FormDataParam("photo") InputStream photo)
throws Exception
{
int len;
int size = 1024;
byte[] buf;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
buf = new byte[size];
while ((len = photo.read(buf, 0, size)) != -1)
bos.write(buf, 0, len);
buf = bos.toByteArray();
return "Got: First = " + firstName + ", Last = " + lastName + ", Age = " + age + ", Photo (# of bytes) = " + buf.length;
}
}

View File

@ -0,0 +1,16 @@
<html>
<head>
<title>Example 1 using @FormParam</title>
</head>
<body>
<form method="post" action="/form/example1">
<label for="first_name">First Name</label>
<input id="first_name" name="first_name" type="text">
<label for="last_name">Last Name</label>
<input id="last_name" name="last_name" type="text">
<label for="age">Age</label>
<input id="age" name="age" type="text">
<input type="submit">
</form>
</body>
</html>

View File

@ -0,0 +1,18 @@
<html>
<head>
<title>Example 2 using @FormDataParam</title>
</head>
<body>
<form method="post" action="/form/example2" enctype="multipart/form-data">
<label for="first_name">First Name</label>
<input id="first_name" name="first_name" type="text">
<label for="last_name">Last Name</label>
<input id="last_name" name="last_name" type="text">
<label for="age">Age</label>
<input id="age" name="age" type="text">
<label for="photo">Profile Photo</label>
<input id="photo" name="photo" type="file">
<input type="submit">
</form>
</body>
</html>

View File

@ -28,12 +28,18 @@
<artifactId>commons-vfs2</artifactId>
<version>${commons-vfs2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>${apache-commons-text.version}</version>
</dependency>
</dependencies>
<properties>
<commons-compress.version>1.23.0</commons-compress.version>
<ant.version>1.10.13</ant.version>
<commons-vfs2.version>2.9.0</commons-vfs2.version>
<apache-commons-text.version>1.10.0</apache-commons-text.version>
</properties>
</project>

View File

@ -0,0 +1,29 @@
package com.baeldung.commons.convertunicode;
import org.apache.commons.text.StringEscapeUtils;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class UnicodeConverterUtil {
public static String decodeWithApacheCommons(String input) {
return StringEscapeUtils.unescapeJava(input);
}
public static String decodeWithPlainJava(String input) {
Pattern pattern = Pattern.compile("\\\\u[0-9a-fA-F]{4}");
Matcher matcher = pattern.matcher(input);
StringBuilder decodedString = new StringBuilder();
while (matcher.find()) {
String unicodeSequence = matcher.group();
char unicodeChar = (char) Integer.parseInt(unicodeSequence.substring(2), 16);
matcher.appendReplacement(decodedString, Character.toString(unicodeChar));
}
matcher.appendTail(decodedString);
return decodedString.toString();
}
}

View File

@ -0,0 +1,39 @@
package com.baeldung.commons.convertunicode;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class UnicodeConverterUnitTest {
@Test
public void whenInputHaveUnicodeSequences_ThenDecode() {
String encodedString = "\\u0048\\u0065\\u006C\\u006C\\u006F World";
String expectedDecodedString = "Hello World";
assertEquals(expectedDecodedString, UnicodeConverterUtil.decodeWithApacheCommons(encodedString));
assertEquals(expectedDecodedString, UnicodeConverterUtil.decodeWithPlainJava(encodedString));
}
@Test
public void whenInputHaveNoUnicodeSequences_ThenDoNothing() {
String inputString = "Hello World";
assertEquals(inputString, UnicodeConverterUtil.decodeWithApacheCommons(inputString));
assertEquals(inputString, UnicodeConverterUtil.decodeWithPlainJava(inputString));
}
@Test
public void whenInputHaveUnicodeSequencesInMiddle_ThenDecode() {
String encodedString = "This is a test \\u0069\\u006E the middle.";
String expectedDecodedString = "This is a test in the middle.";
assertEquals(expectedDecodedString, UnicodeConverterUtil.decodeWithApacheCommons(encodedString));
assertEquals(expectedDecodedString, UnicodeConverterUtil.decodeWithPlainJava(encodedString));
}
@Test
public void whenInputHaveMultipleUnicodeSequences_ThenDecode() {
String encodedString = "Unicode: \\u0048\\u0065\\u006C\\u006C\\u006F \\u0057\\u006F\\u0072\\u006C\\u0064";
String expectedDecodedString = "Unicode: Hello World";
assertEquals(expectedDecodedString, UnicodeConverterUtil.decodeWithApacheCommons(encodedString));
assertEquals(expectedDecodedString, UnicodeConverterUtil.decodeWithPlainJava(encodedString));
}
}

View File

@ -5,6 +5,18 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>libraries-io</artifactId>
<name>libraries-io</name>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
</plugins>
</build>
<parent>
<groupId>com.baeldung</groupId>
@ -34,6 +46,11 @@
<artifactId>zip4j</artifactId>
<version>${zip4j.version}</version>
</dependency>
<dependency>
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>${opencsv.version}</version>
</dependency>
</dependencies>
<properties>
@ -42,6 +59,10 @@
<sshj.version>0.27.0</sshj.version>
<vfs.version>2.4</vfs.version>
<zip4j.version>2.9.0</zip4j.version>
<opencsv.version>5.7.1</opencsv.version>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>

View File

@ -0,0 +1,4 @@
package com.baeldung.java.io.pojotocsv;
public record Application(String id, String name, Integer age, String created_at) {
}

View File

@ -0,0 +1,8 @@
package com.baeldung.java.io.pojotocsv;
import com.opencsv.bean.CsvBindByName;
import com.opencsv.bean.CsvBindByPosition;
public record ApplicationWithAnnotation(@CsvBindByName(column = "id", required = true) @CsvBindByPosition(position = 1) String id, @CsvBindByName(column = "name", required = true) @CsvBindByPosition(position = 0) String name,
@CsvBindByName(column = "age", required = true) @CsvBindByPosition(position = 2) Integer age, @CsvBindByName(column = "position", required = true) @CsvBindByPosition(position = 3) String created_at) {
}

View File

@ -0,0 +1,55 @@
package com.baeldung.java.io.pojotocsv;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import com.opencsv.CSVWriter;
import com.opencsv.bean.StatefulBeanToCsvBuilder;
import com.opencsv.exceptions.CsvDataTypeMismatchException;
import com.opencsv.exceptions.CsvRequiredFieldEmptyException;
public class BeanToCsv {
public void beanToCSVWithDefault(List<Application> applications) throws Exception {
try (FileWriter writer = new FileWriter("src/main/resources/application.csv")) {
var builder = new StatefulBeanToCsvBuilder<Application>(writer).withQuotechar(CSVWriter.NO_QUOTE_CHARACTER)
.withSeparator(',')
.build();
builder.write(applications);
}
}
public void beanToCSVWithCustomHeaderStrategy(List<Application> applications) throws IOException, CsvRequiredFieldEmptyException, CsvDataTypeMismatchException {
try (FileWriter writer = new FileWriter("src/main/resources/application2.csv")) {
var mappingStrategy = new CustomHeaderStrategy<Application>();
mappingStrategy.setType(Application.class);
var builder = new StatefulBeanToCsvBuilder<Application>(writer).withQuotechar(CSVWriter.NO_QUOTE_CHARACTER)
.withMappingStrategy(mappingStrategy)
.build();
builder.write(applications);
}
}
public void beanToCSVWithCustomPositionStrategy(List<ApplicationWithAnnotation> applications) throws Exception {
try (FileWriter writer = new FileWriter("src/main/resources/application3.csv")) {
var builder = new StatefulBeanToCsvBuilder<ApplicationWithAnnotation>(writer).withQuotechar(CSVWriter.NO_QUOTE_CHARACTER)
.build();
builder.write(applications);
}
}
public void beanToCSVWithCustomHeaderAndPositionStrategy(List<ApplicationWithAnnotation> applications) throws IOException, CsvRequiredFieldEmptyException, CsvDataTypeMismatchException {
try (FileWriter writer = new FileWriter("src/main/resources/application4.csv")) {
var mappingStrategy = new CustomColumnPositionStrategy<ApplicationWithAnnotation>();
mappingStrategy.setType(ApplicationWithAnnotation.class);
var builder = new StatefulBeanToCsvBuilder<ApplicationWithAnnotation>(writer).withQuotechar(CSVWriter.NO_QUOTE_CHARACTER)
.withMappingStrategy(mappingStrategy)
.build();
builder.write(applications);
}
}
}

View File

@ -0,0 +1,12 @@
package com.baeldung.java.io.pojotocsv;
import com.opencsv.bean.ColumnPositionMappingStrategy;
import com.opencsv.exceptions.CsvRequiredFieldEmptyException;
public class CustomColumnPositionStrategy<T> extends ColumnPositionMappingStrategy<T> {
@Override
public String[] generateHeader(T bean) throws CsvRequiredFieldEmptyException {
super.generateHeader(bean);
return super.getColumnMapping();
}
}

View File

@ -0,0 +1,16 @@
package com.baeldung.java.io.pojotocsv;
import java.util.Arrays;
import com.opencsv.bean.HeaderColumnNameMappingStrategy;
import com.opencsv.exceptions.CsvRequiredFieldEmptyException;
public class CustomHeaderStrategy<T> extends HeaderColumnNameMappingStrategy<T> {
@Override
public String[] generateHeader(T bean) throws CsvRequiredFieldEmptyException {
String[] header = super.generateHeader(bean);
return Arrays.stream(header)
.map(String::toLowerCase)
.toArray(String[]::new);
}
}

View File

@ -0,0 +1,4 @@
AGE,CREATED_AT,ID,NAME
34,2023-08-11,123,Sam
44,2023-02-11,456,Tam
54,2023-03-11,890,Jam
1 AGE CREATED_AT ID NAME
2 34 2023-08-11 123 Sam
3 44 2023-02-11 456 Tam
4 54 2023-03-11 890 Jam

View File

@ -0,0 +1,4 @@
age,created_at,id,name
34,2023-08-11,123,Sam
44,2023-02-11,456,Tam
54,2023-03-11,890,Jam
1 age created_at id name
2 34 2023-08-11 123 Sam
3 44 2023-02-11 456 Tam
4 54 2023-03-11 890 Jam

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