Merge branch 'eugenp:master' into master

This commit is contained in:
Wynn Teo
2024-02-11 21:05:11 +08:00
committed by GitHub
93 changed files with 993 additions and 217 deletions
@@ -6,4 +6,5 @@
- [Calculate Distance Between Two Coordinates in Java](https://www.baeldung.com/java-find-distance-between-points)
- [Rotate Arrays in Java](https://www.baeldung.com/java-rotate-arrays)
- [Find Missing Number From a Given Array in Java](https://www.baeldung.com/java-array-find-missing-number)
- [Calculate Weighted Mean in Java](https://www.baeldung.com/java-compute-weighted-average)
- More articles: [[<-- prev]](/algorithms-miscellaneous-6)
@@ -0,0 +1,61 @@
package com.baeldung.algorithms.firstnonrepeating;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class FirstNonRepeatingElement {
public static Integer findFirstNonRepeatingUsingForLoop(List<Integer> list) {
for (int i = 0; i < list.size(); i++) {
int current = list.get(i);
boolean isRepeating = false;
for (int j = 0; j < list.size(); j++) {
if (i != j && current == list.get(j)) {
isRepeating = true;
break;
}
}
if (!isRepeating) {
return current;
}
}
return null;
}
public static Integer findFirstNonRepeatedElementUsingIndex(List<Integer> list) {
for (int i = 0; i < list.size(); i++) {
if (list.indexOf(list.get(i)) == list.lastIndexOf(list.get(i))) {
return list.get(i);
}
}
return null;
}
public static Integer findFirstNonRepeatingUsingHashMap(List<Integer> list) {
Map<Integer, Integer> counts = new HashMap<>();
for (int num : list) {
counts.put(num, counts.getOrDefault(num, 0) + 1);
}
for (int num : list) {
if (counts.get(num) == 1) {
return num;
}
}
return null;
}
public static Integer findFirstNonRepeatingUsingArray(List<Integer> list) {
int maxElement = Collections.max(list);
int[] frequency = new int[maxElement + 1];
for (int num : list) {
frequency[num]++;
}
for (int num : list) {
if (frequency[num] == 1) {
return num;
}
}
return null;
}
}
@@ -0,0 +1,43 @@
package com.baeldung.algorithms.firstnonrepeating;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class FirstNonRepeatingElementUnitTest {
private List<Integer> list;
@BeforeEach
void setUp() {
list = Arrays.asList(1, 2, 3, 2, 1, 4, 5, 4);
}
@Test
void whenUsingForLoop_thenReturnFirstNonRepeatingElement() {
int result = FirstNonRepeatingElement.findFirstNonRepeatingUsingForLoop(list);
assertEquals(3, result);
}
@Test
void whenUsingIndexOf_thenReturnFirstNonRepeatingElement() {
int result = FirstNonRepeatingElement.findFirstNonRepeatedElementUsingIndex(list);
assertEquals(3, result);
}
@Test
void whenUsingHashMap_thenReturnFirstNonRepeatingElement() {
int result = FirstNonRepeatingElement.findFirstNonRepeatingUsingHashMap(list);
assertEquals(3, result);
}
@Test
void whenUsingArray_thenReturnFirstNonRepeatingElement() {
int result = FirstNonRepeatingElement.findFirstNonRepeatingUsingArray(list);
assertEquals(3, result);
}
}
@@ -0,0 +1,39 @@
package com.baeldung.movezerototheend;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
public class MoveZeroesToTheEndOfAnArrayUnitTest {
private static final int[] EXPECTED = new int[] { 42, 2, 3, 4, 0, 0 };
@Test
void whenCreatingANewArrayAndCopyingValues_thenGetTheExpectedResult() {
int[] array = new int[] { 42, 2, 0, 3, 4, 0 };
int[] result = new int[array.length];
int idx = 0;
for (int n : array) {
if (n != 0) {
result[idx++] = n;
}
}
assertArrayEquals(EXPECTED, result);
}
@Test
void whenMovingZeroInTheOriginalArray_thenGetTheExpectedResult() {
int[] array = new int[] { 42, 2, 0, 3, 4, 0 };
int idx = 0;
for (int n : array) {
if (n != 0) {
array[idx++] = n;
}
}
while (idx < array.length) {
array[idx++] = 0;
}
assertArrayEquals(EXPECTED, array);
}
}
@@ -2,3 +2,4 @@
### Relevant Articles:
- [Why wait() Requires Synchronization?](https://www.baeldung.com/java-wait-necessary-synchronization)
- [Working with Exceptions in Java CompletableFuture](https://www.baeldung.com/java-exceptions-completablefuture)
- [CountDownLatch vs. Semaphore](https://www.baeldung.com/java-countdownlatch-vs-semaphore)
@@ -0,0 +1,65 @@
package com.baeldung.datetounixtimestamp;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;
import org.joda.time.DateTime;
import org.junit.jupiter.api.Test;
class DateToUnixTimeStampUnitTest {
@Test
void givenDate_whenUsingInstantClass_thenConvertToUnixTimeStamp() {
Instant givenDate = Instant.parse("2020-09-08T12:16:40Z");
assertEquals(1599567400L, givenDate.getEpochSecond());
}
@Test
void givenDate_whenUsingLocalDateTimeClass_thenConvertToUnixTimeStamp() {
LocalDateTime givenDate = LocalDateTime.of(2023, 10, 19, 22, 45);
assertEquals(1697755500L, givenDate.toEpochSecond(ZoneOffset.UTC));
}
@Test
void givenDate_whenUsingDateClass_thenConvertToUnixTimeStamp() throws ParseException {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date givenDate = dateFormat.parse("2023-10-15 22:00:00");
assertEquals(1697407200L, givenDate.getTime() / 1000);
}
@Test
void givenDate_whenUsingCalendarClass_thenConvertToUnixTimeStamp() {
Calendar calendar = new GregorianCalendar(2023, Calendar.OCTOBER, 17);
calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
assertEquals(1697500800L, calendar.getTimeInMillis() / 1000);
}
@Test
void givenDate_whenUsingJodaTimeInstantClass_thenConvertToUnixTimeStamp() {
org.joda.time.Instant givenDate = org.joda.time.Instant.parse("2020-09-08T12:16:40Z");
assertEquals(1599567400L, givenDate.getMillis() / 1000);
}
@Test
void givenDate_whenUsingJodaTimeDateTimeClass_thenConvertToUnixTimeStamp() {
DateTime givenDate = new DateTime("2020-09-09T12:16:40Z");
assertEquals(1599653800L, givenDate.getMillis() / 1000);
}
}
@@ -4,3 +4,4 @@ This module contains articles about converting between Java date and time object
### Relevant Articles:
- [Convert Gregorian to Hijri Date in Java](https://www.baeldung.com/java-date-gregorian-hijri-conversion)
- [Convert String Date to XMLGregorianCalendar in Java](https://www.baeldung.com/java-string-date-xmlgregoriancalendar-conversion)
@@ -8,5 +8,6 @@ This module contains articles about core Java input and output (IO)
- [Difference Between ZipFile and ZipInputStream in Java](https://www.baeldung.com/java-zipfile-vs-zipinputstream)
- [How to Write Strings to OutputStream in Java](https://www.baeldung.com/java-write-string-outputstream)
- [Read a File and Split It Into Multiple Files in Java](https://www.baeldung.com/java-read-file-split-into-several)
- [Read and Write Files in Java Using Separate Threads](https://www.baeldung.com/java-read-write-files-different-threads)
- [[<-- Prev]](/core-java-modules/core-java-io-4)
@@ -0,0 +1,21 @@
package com.baeldung.objectmutability;
public final class ImmutablePerson {
private final String name;
private final int age;
public ImmutablePerson(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
@@ -0,0 +1,33 @@
package com.baeldung.objectmutability;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotSame;
public class ImmutableObjectExamplesUnitTest {
@Test
public void givenImmutableString_whenConcatString_thenNotSameAndCorrectValues() {
String originalString = "Hello";
String modifiedString = originalString.concat(" World");
assertNotSame(originalString, modifiedString);
assertEquals("Hello", originalString);
assertEquals("Hello World", modifiedString);
}
@Test
public void givenImmutableInteger_whenAddInteger_thenNotSameAndCorrectValue() {
Integer immutableInt = 42;
Integer modifiedInt = immutableInt + 8;
assertNotSame(immutableInt, modifiedInt);
assertEquals(42, (int) immutableInt);
assertEquals(50, (int) modifiedInt);
}
}
@@ -0,0 +1,17 @@
package com.baeldung.objectmutability;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class ImmutablePersonUnitTest {
@Test
public void givenImmutablePerson_whenAccessFields_thenCorrectValues() {
ImmutablePerson person = new ImmutablePerson("John", 30);
assertEquals("John", person.getName());
assertEquals(30, person.getAge());
}
}
@@ -0,0 +1,28 @@
package com.baeldung.objectmutability;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class MutableObjectExamplesUnitTest {
@Test
public void givenMutableString_whenAppendElement_thenCorrectValue() {
StringBuilder mutableString = new StringBuilder("Hello");
mutableString.append(" World");
assertEquals("Hello World", mutableString.toString());
}
@Test
public void givenMutableList_whenAddElement_thenCorrectSize() {
List<String> mutableList = new ArrayList<>();
mutableList.add("Java");
assertEquals(1, mutableList.size());
}
}
@@ -5,3 +5,4 @@
- [Download a Webpage in Java](https://www.baeldung.com/java-download-webpage)
- [URL Query Manipulation in Java](https://www.baeldung.com/java-url-query-manipulation)
- [Understanding the java.net.SocketException Broken Pipe Error](https://www.baeldung.com/java-socketexception-broken-pipe-error)
- [Normalize a URL in Java](https://www.baeldung.com/java-url-normalization)
@@ -2,3 +2,4 @@
- [Check if a double Is an Integer in Java](https://www.baeldung.com/java-check-double-integer)
- [Print a Double Value Without Scientific Notation in Java](https://www.baeldung.com/java-print-double-number-no-scientific-notation)
- [Check if a Float Value is Equivalent to an Integer Value in Java](https://www.baeldung.com/java-float-integer-equal)
- [Generating Unique Positive Long Using SecureRandom in Java](https://www.baeldung.com/java-securerandom-generate-positive-long)
@@ -0,0 +1,18 @@
package com.baeldung.securerandompositivelong;
import org.junit.jupiter.api.Test;
import java.security.SecureRandom;
import static org.assertj.core.api.Assertions.assertThat;
public class SecureRandomPositiveLongUnitTest {
@Test
void whenGenerateRandomPositiveLong_thenGetPositiveValue() {
SecureRandom secureRandom = new SecureRandom();
long randomPositiveLong = Math.abs(secureRandom.nextLong());
assertThat(randomPositiveLong).isNotNegative();
}
}
@@ -6,3 +6,5 @@ This module contains articles about performance of Java applications
- [External Debugging With JMXTerm](https://www.baeldung.com/java-jmxterm-external-debugging)
- [Create and Detect Memory Leaks in Java](https://www.baeldung.com/java-create-detect-memory-leaks)
- [Differences Between Heap Dump, Thread Dump and Core Dump](https://www.baeldung.com/java-heap-thread-core-dumps)
- [Shutting Down on OutOfMemoryError in Java](https://www.baeldung.com/java-shutting-down-outofmemoryerror)
@@ -1,2 +1,3 @@
### Relevant Articles:
- [Is Java Reflection Bad Practice?](https://www.baeldung.com/java-reflection-benefits-drawbacks)
- [Instantiate an Inner Class With Reflection in Java](https://www.baeldung.com/java-reflection-instantiate-inner-class)
@@ -5,3 +5,4 @@ This module contains articles about string-related algorithms.
### Relevant Articles:
- [Rotating a Java String By n Characters](https://www.baeldung.com/java-rotate-string-by-n-characters)
- [Remove Characters From a String That Are in the Other String](https://www.baeldung.com/java-strings-character-difference)
- [Run-Length Encoding and Decoding in Java](https://www.baeldung.com/java-rle-compression)
@@ -7,3 +7,4 @@
- [Generate the Same UUID From a String in Java](https://www.baeldung.com/java-generate-same-uuid-from-string)
- [Generating Time Based UUIDs](https://www.baeldung.com/java-generating-time-based-uuids)
- [Generating Unique Positive long Using UUID in Java](https://www.baeldung.com/java-uuid-unique-long-generation)
- [Storing UUID as Base64 String in Java](https://www.baeldung.com/java-store-uuid-base64-string)
+1
View File
@@ -60,6 +60,7 @@
<module>core-java-methods</module>
<module>core-java-networking-3</module>
<module>core-java-os</module>
<module>core-java-os-2</module>
<module>core-java-perf-2</module>
<module>core-java-streams-4</module>
<module>core-java-streams-5</module>
-2
View File
@@ -55,14 +55,12 @@
<artifactId>httpclient</artifactId>
<version>${httpclient.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
<properties>
-2
View File
@@ -14,14 +14,12 @@
<artifactId>avaje-inject</artifactId>
<version>${avaje.inject.version}</version>
</dependency>
<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-inject-test</artifactId>
<version>${avaje.inject.version}</version>
<scope>test</scope>
</dependency>
<!-- Annotation processors -->
<dependency>
<groupId>io.avaje</groupId>
+17 -28
View File
@@ -1,8 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>k8s-operator</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>k8s-operator</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung</groupId>
@@ -11,30 +15,12 @@
<relativePath>./../../parent-boot-3</relativePath>
</parent>
<artifactId>k8s-operator</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>k8s-operator</name>
<packaging>jar</packaging>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<josdk.version>4.6.0</josdk.version>
<fabric8-client.version>6.9.2</fabric8-client.version>
<bouncycastle.version>1.77</bouncycastle.version>
<slf4j.version>2.0.9</slf4j.version>
<operator-framework-spring-boot.version>5.4.0</operator-framework-spring-boot.version>
</properties>
<dependencies>
<dependency>
<groupId>io.javaoperatorsdk</groupId>
<artifactId>operator-framework-spring-boot-starter</artifactId>
<version>${operator-framework-spring-boot.version}</version>
</dependency>
<dependency>
<groupId>io.javaoperatorsdk</groupId>
<artifactId>operator-framework-spring-boot-starter-test</artifactId>
@@ -47,49 +33,42 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>crd-generator-apt</artifactId>
<version>${fabric8-client.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk18on</artifactId>
<version>${bouncycastle.version}</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcpkix-jdk18on</artifactId>
<version>${bouncycastle.version}</version>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
@@ -108,4 +87,14 @@
</plugins>
</build>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<josdk.version>4.6.0</josdk.version>
<fabric8-client.version>6.9.2</fabric8-client.version>
<bouncycastle.version>1.77</bouncycastle.version>
<slf4j.version>2.0.9</slf4j.version>
<operator-framework-spring-boot.version>5.4.0</operator-framework-spring-boot.version>
</properties>
</project>
+1 -1
View File
@@ -18,7 +18,7 @@
<module>k8s-admission-controller</module>
<module>kubernetes-spring</module>
<module>k8s-java-heap-dump</module>
<module>k8s-operator</module>
<module>k8s-operator</module>
</modules>
</project>
-1
View File
@@ -167,7 +167,6 @@
</resources>
</build>
<properties>
<jcabi-aspects.version>0.22.6</jcabi-aspects.version>
<aspectjrt.version>1.9.20.1</aspectjrt.version>
+1
View File
@@ -41,6 +41,7 @@
<version>${asm.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
+5 -5
View File
@@ -1,15 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>logging-techniques</artifactId>
<parent>
<artifactId>logging-modules</artifactId>
<groupId>com.baeldung</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>logging-techniques</artifactId>
<dependencies>
<dependency>
+2
View File
@@ -55,6 +55,7 @@
<version>${validation-api.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
@@ -82,6 +83,7 @@
</plugin>
</plugins>
</build>
<properties>
<openapi.version>7.1.0</openapi.version>
<gson.version>2.10.1</gson.version>
+13 -13
View File
@@ -14,15 +14,21 @@
<version>0.0.1-SNAPSHOT</version>
</parent>
<properties>
<surefire-version>2.22.2</surefire-version>
</properties>
<modules>
<module>dummy-surefire-junit47</module>
<module>core-java-exclusions</module>
</modules>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
@@ -57,14 +63,8 @@
</pluginManagement>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<properties>
<surefire-version>2.22.2</surefire-version>
</properties>
</project>
@@ -5,11 +5,13 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>patient-data</artifactId>
<name>patient-data</name>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>maven-reactor</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>com.baeldung</groupId>
@@ -17,4 +19,5 @@
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>

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