[JAVA-31850] Split-or-move-core-java-lang-6 (#16484)

* [JAVA-31850] Split-or-move-core-java-lang-6

* [JAVA-31850] Split-or-move-core-java-lang-6: Fix missing dependency version of core-java-lang-7
This commit is contained in:
vunamtien
2024-04-27 04:51:19 +07:00
committed by GitHub
parent fe759e41e4
commit b17f9029a1
7 changed files with 13 additions and 6 deletions
@@ -5,3 +5,7 @@ This module contains articles about core features in the Java language
### Relevant Articles:
- [Set an Environment Variable at Runtime in Java](https://www.baeldung.com/java-set-environment-variable-runtime)
- [Get a Random Element From a Set in Java](https://www.baeldung.com/java-set-draw-sample)
- [Compress and Uncompress Byte Array Using Deflater/Inflater](https://www.baeldung.com/java-compress-uncompress-byte-array)
[[<-- Prev]](/core-java-modules/core-java-lang-6)
+8 -4
View File
@@ -1,9 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<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-lang-6</artifactId>
<artifactId>core-java-lang-7</artifactId>
<packaging>jar</packaging>
<name>core-java-lang-7</name>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>
@@ -61,6 +63,8 @@
<properties>
<junit.pioneer.version>2.2.0</junit.pioneer.version>
<testcontaienr.version>1.19.3</testcontaienr.version>
<mapstruct.version>1.6.0.Beta1</mapstruct.version>
<jmh.version>1.37</jmh.version>
</properties>
</project>
@@ -0,0 +1,75 @@
package com.baeldung.compressbytes;
import java.io.ByteArrayOutputStream;
import java.util.zip.DataFormatException;
import java.util.zip.Deflater;
import java.util.zip.Inflater;
public class CompressByteArrayUtil {
public static byte[] compress(byte[] input) {
Deflater deflater = new Deflater();
deflater.setInput(input);
deflater.finish();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
while (!deflater.finished()) {
int compressedSize = deflater.deflate(buffer);
outputStream.write(buffer, 0, compressedSize);
}
return outputStream.toByteArray();
}
public static byte[] compressWithCustomLevel(byte[] input, int level) {
Deflater deflater = new Deflater();
deflater.setInput(input);
deflater.setLevel(level);
deflater.finish();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
while (!deflater.finished()) {
int compressedSize = deflater.deflate(buffer);
outputStream.write(buffer, 0, compressedSize);
}
return outputStream.toByteArray();
}
public static byte[] decompress(byte[] input) throws DataFormatException {
Inflater inflater = new Inflater();
inflater.setInput(input);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
while (!inflater.finished()) {
int decompressedSize = inflater.inflate(buffer);
outputStream.write(buffer, 0, decompressedSize);
}
return outputStream.toByteArray();
}
public static void main(String[] args) throws Exception {
String inputString = "Baeldung helps developers explore the Java ecosystem and simply be better engineers. " +
"We publish to-the-point guides and courses, with a strong focus on building web applications, Spring, " +
"Spring Security, and RESTful APIs";
byte[] input = inputString.getBytes();
// Compression
byte[] compressedData = compress(input);
// Decompression
byte[] decompressedData = decompress(compressedData);
System.out.println("Original: " + input.length + " bytes");
System.out.println("Compressed: " + compressedData.length + " bytes");
System.out.println("Decompressed: " + decompressedData.length + " bytes");
}
}
@@ -0,0 +1,50 @@
package com.baeldung.randominset;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.ThreadLocalRandom;
public class RandomInSetUtil {
public static <T> T getByRandomClass(Set<T> set) {
if (set == null || set.isEmpty()) {
throw new IllegalArgumentException("The Set cannot be empty.");
}
int randomIndex = new Random().nextInt(set.size());
int i = 0;
for (T element : set) {
if (i == randomIndex) {
return element;
}
i++;
}
throw new IllegalStateException("Something went wrong while picking a random element.");
}
public static <T> T getByThreadLocalRandom(Set<T> set) {
if (set == null || set.isEmpty()) {
throw new IllegalArgumentException("The Set cannot be empty.");
}
int randomIndex = ThreadLocalRandom.current().nextInt(set.size());
int i = 0;
for (T element : set) {
if (i == randomIndex) {
return element;
}
i++;
}
throw new IllegalStateException("Something went wrong while picking a random element.");
}
public static void main(String[] args) {
Set<String> animals = new HashSet<>();
animals.add("Lion");
animals.add("Elephant");
animals.add("Giraffe");
String randomAnimal = getByThreadLocalRandom(animals);
System.out.println("Randomly picked animal: " + randomAnimal);
}
}
@@ -0,0 +1,39 @@
package com.baeldung.compressbytes;
import org.junit.jupiter.api.Test;
import java.util.zip.DataFormatException;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class CompressByteArrayUnitTest {
private static final String INPUT_STRING = "Building a REST API is not a trivial task from the high-level RESTful " +
"constraints down to the nitty-gritty of making everything work and work well." +
"Spring has made REST a first-class citizen and the platform has been maturing in leaps and bounds." +
"With this guide, my aim is to organize the mountains of information that are available on the subject and " +
"guide you through properly building an API." +
"The guide starts with the basics bootstrapping the REST API, the Spring MVC Configuration, and basic customization.";
@Test
void givenInputString_whenCompressWithDefaultLevel_thenDecompressWithSameSize() throws DataFormatException {
byte[] input = INPUT_STRING.getBytes();
byte[] compressedData = CompressByteArrayUtil.compress(input);
byte[] decompressedData = CompressByteArrayUtil.decompress(compressedData);
System.out.println("Original: " + input.length + " bytes");
System.out.println("Compressed: " + compressedData.length + " bytes");
System.out.println("Decompressed: " + decompressedData.length + " bytes");
assertEquals(input.length, decompressedData.length);
}
@Test
void givenInputString_whenCompressWithCustomLevel_thenDecompressWithSameSize() throws DataFormatException {
byte[] input = INPUT_STRING.getBytes();
byte[] compressedData = CompressByteArrayUtil.compressWithCustomLevel(input, 1);
byte[] decompressedData = CompressByteArrayUtil.decompress(compressedData);
System.out.println("Original: " + input.length + " bytes");
System.out.println("Compressed: " + compressedData.length + " bytes");
System.out.println("Decompressed: " + decompressedData.length + " bytes");
assertEquals(input.length, decompressedData.length);
}
}