From d0a56cfbe7d564a539c733b3a0f86fee7e11b580 Mon Sep 17 00:00:00 2001 From: "alexandru.borza" Date: Sun, 29 Jan 2023 01:53:53 +0200 Subject: [PATCH 01/10] initialize arraylist with null or zeros --- .../pom.xml | 32 ++++++++ .../InitializeArrayListWithNullOrZeros.java | 14 ++++ ...alizeArrayListWithNullOrZerosUnitTest.java | 75 +++++++++++++++++++ pom.xml | 1 + 4 files changed, 122 insertions(+) create mode 100644 core-java-modules/core-java-collections-array-list-2/pom.xml create mode 100644 core-java-modules/core-java-collections-array-list-2/src/main/java/com/baeldung/InitializeArrayListWithNullOrZeros.java create mode 100644 core-java-modules/core-java-collections-array-list-2/src/test/java/com/baeldung/InitializeArrayListWithNullOrZerosUnitTest.java diff --git a/core-java-modules/core-java-collections-array-list-2/pom.xml b/core-java-modules/core-java-collections-array-list-2/pom.xml new file mode 100644 index 0000000000..ca29a0d14b --- /dev/null +++ b/core-java-modules/core-java-collections-array-list-2/pom.xml @@ -0,0 +1,32 @@ + + + + parent-modules + com.baeldung + 1.0.0-SNAPSHOT + ../../pom.xml + + 4.0.0 + + core-java-collections-array-list-2 + + + + org.apache.maven.plugins + maven-compiler-plugin + + 9 + 9 + + + + + + + 17 + 17 + + + diff --git a/core-java-modules/core-java-collections-array-list-2/src/main/java/com/baeldung/InitializeArrayListWithNullOrZeros.java b/core-java-modules/core-java-collections-array-list-2/src/main/java/com/baeldung/InitializeArrayListWithNullOrZeros.java new file mode 100644 index 0000000000..60cd18af98 --- /dev/null +++ b/core-java-modules/core-java-collections-array-list-2/src/main/java/com/baeldung/InitializeArrayListWithNullOrZeros.java @@ -0,0 +1,14 @@ +package com.baeldung; + +import java.util.ArrayList; + +public class InitializeArrayListWithNullOrZeros { + + public static void main(String[] args) { + + ArrayList arrayList = new ArrayList<>(); + for (int i = 0; i< 10; i++) { + arrayList.add(null); + } + } +} diff --git a/core-java-modules/core-java-collections-array-list-2/src/test/java/com/baeldung/InitializeArrayListWithNullOrZerosUnitTest.java b/core-java-modules/core-java-collections-array-list-2/src/test/java/com/baeldung/InitializeArrayListWithNullOrZerosUnitTest.java new file mode 100644 index 0000000000..71f8a23690 --- /dev/null +++ b/core-java-modules/core-java-collections-array-list-2/src/test/java/com/baeldung/InitializeArrayListWithNullOrZerosUnitTest.java @@ -0,0 +1,75 @@ +package com.baeldung; + +import org.junit.Test; +import org.junit.jupiter.api.Assertions; + +import java.util.*; +import java.util.stream.Collectors; +import java.util.stream.IntStream; +import java.util.stream.Stream; + + +public class InitializeArrayListWithNullOrZerosUnitTest { + + @Test + public void whenInitializingListWithNCopies_thenListIsCorrectlyPopulated() { + // when + ArrayList list = IntStream.of(new int[10]) + .boxed() + .collect(Collectors.toCollection(ArrayList::new)); + + // then + Assertions.assertEquals(10, list.size()); + Assertions.assertTrue(list.stream().allMatch(elem -> elem == 0)); + } + + @Test + public void whenInitializingListWithStream_thenListIsCorrectlyPopulated() { + + // when + ArrayList listWithZeros = Stream.generate(() -> 0) + .limit(10).collect(Collectors.toCollection(ArrayList::new)); + + ArrayList listWithNulls = Stream.generate(() -> null) + .limit(10).collect(Collectors.toCollection(ArrayList::new)); + + // then + Assertions.assertEquals(10, listWithZeros.size()); + Assertions.assertTrue(listWithZeros.stream().allMatch(elem -> elem == 0)); + + Assertions.assertEquals(10, listWithNulls.size()); + Assertions.assertTrue(listWithNulls.stream().allMatch(Objects::isNull)); + } + + @Test public void whenInitializingListWithIntStream_thenListIsCorrectlyPopulated() { + // when + ArrayList list = IntStream.of(new int[10]) + .boxed() + .collect(Collectors.toCollection(ArrayList::new)); + + // then + Assertions.assertEquals(10, list.size()); + Assertions.assertTrue(list.stream().allMatch(elem -> elem == 0)); } + + @Test + public void whenInitializingListWithAsList_thenListIsCorrectlyPopulated() { + // when + Integer[] integers = new Integer[10]; + Arrays.fill(integers, 0); + List integerList = new ArrayList<>(Arrays.asList(integers)); + + // then + Assertions.assertEquals(10, integerList.size()); + Assertions.assertTrue(integerList.stream().allMatch(elem -> elem == 0)); + } + + @Test + public void whenInitializingListWithVector_thenListIsCorrectlyPopulated() { + // when + List integerList = new Vector<>() {{setSize(10);}}; + + // then + Assertions.assertEquals(10, integerList.size()); + Assertions.assertTrue(integerList.stream().allMatch(Objects::isNull)); + } +} diff --git a/pom.xml b/pom.xml index 0b19c6eee6..cd3ae35b15 100644 --- a/pom.xml +++ b/pom.xml @@ -1134,6 +1134,7 @@ core-java-modules/core-java-collections-set core-java-modules/core-java-collections-list-4 core-java-modules/core-java-collections-array-list + core-java-modules/core-java-collections-array-list-2 core-java-modules/core-java-collections-maps-4 core-java-modules/core-java-collections-maps-5 core-java-modules/core-java-concurrency-simple From 88f56c6281bc42b1fae5bef3fb8031fa7829f5d4 Mon Sep 17 00:00:00 2001 From: "alexandru.borza" Date: Sun, 29 Jan 2023 19:46:23 +0200 Subject: [PATCH 02/10] configure pom parent --- .../core-java-collections-array-list-2/pom.xml | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/core-java-modules/core-java-collections-array-list-2/pom.xml b/core-java-modules/core-java-collections-array-list-2/pom.xml index ca29a0d14b..cc15ba7c6e 100644 --- a/core-java-modules/core-java-collections-array-list-2/pom.xml +++ b/core-java-modules/core-java-collections-array-list-2/pom.xml @@ -2,15 +2,18 @@ - - parent-modules - com.baeldung - 1.0.0-SNAPSHOT - ../../pom.xml - 4.0.0 - core-java-collections-array-list-2 + 0.1.0-SNAPSHOT + core-java-collections-array-list-2 + jar + + + com.baeldung.core-java-modules + core-java-modules + 0.0.1-SNAPSHOT + + From cb3f667a4e7a3da1f28684e51c62c22d5d448a3b Mon Sep 17 00:00:00 2001 From: "alexandru.borza" Date: Sun, 29 Jan 2023 20:34:59 +0200 Subject: [PATCH 03/10] move code --- .../pom.xml | 35 ------------------- .../InitializeArrayListWithNullOrZeros.java | 14 -------- .../InitializeArrayListWithNullOrZeros.java | 15 ++++++++ ...alizeArrayListWithNullOrZerosUnitTest.java | 3 +- 4 files changed, 16 insertions(+), 51 deletions(-) delete mode 100644 core-java-modules/core-java-collections-array-list-2/pom.xml delete mode 100644 core-java-modules/core-java-collections-array-list-2/src/main/java/com/baeldung/InitializeArrayListWithNullOrZeros.java create mode 100644 core-java-modules/core-java-collections-array-list/src/main/java/com/baeldung/initializearraylistwithnullorzeros/InitializeArrayListWithNullOrZeros.java rename core-java-modules/{core-java-collections-array-list-2/src/test/java/com/baeldung => core-java-collections-array-list/src/test/java/com/baeldung/initializearraylistwithnullorzeros}/InitializeArrayListWithNullOrZerosUnitTest.java (97%) diff --git a/core-java-modules/core-java-collections-array-list-2/pom.xml b/core-java-modules/core-java-collections-array-list-2/pom.xml deleted file mode 100644 index cc15ba7c6e..0000000000 --- a/core-java-modules/core-java-collections-array-list-2/pom.xml +++ /dev/null @@ -1,35 +0,0 @@ - - - 4.0.0 - core-java-collections-array-list-2 - 0.1.0-SNAPSHOT - core-java-collections-array-list-2 - jar - - - com.baeldung.core-java-modules - core-java-modules - 0.0.1-SNAPSHOT - - - - - - org.apache.maven.plugins - maven-compiler-plugin - - 9 - 9 - - - - - - - 17 - 17 - - - diff --git a/core-java-modules/core-java-collections-array-list-2/src/main/java/com/baeldung/InitializeArrayListWithNullOrZeros.java b/core-java-modules/core-java-collections-array-list-2/src/main/java/com/baeldung/InitializeArrayListWithNullOrZeros.java deleted file mode 100644 index 60cd18af98..0000000000 --- a/core-java-modules/core-java-collections-array-list-2/src/main/java/com/baeldung/InitializeArrayListWithNullOrZeros.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.baeldung; - -import java.util.ArrayList; - -public class InitializeArrayListWithNullOrZeros { - - public static void main(String[] args) { - - ArrayList arrayList = new ArrayList<>(); - for (int i = 0; i< 10; i++) { - arrayList.add(null); - } - } -} diff --git a/core-java-modules/core-java-collections-array-list/src/main/java/com/baeldung/initializearraylistwithnullorzeros/InitializeArrayListWithNullOrZeros.java b/core-java-modules/core-java-collections-array-list/src/main/java/com/baeldung/initializearraylistwithnullorzeros/InitializeArrayListWithNullOrZeros.java new file mode 100644 index 0000000000..66862791aa --- /dev/null +++ b/core-java-modules/core-java-collections-array-list/src/main/java/com/baeldung/initializearraylistwithnullorzeros/InitializeArrayListWithNullOrZeros.java @@ -0,0 +1,15 @@ +package com.baeldung.initializearraylistwithnullorzeros; + +import java.util.ArrayList; + +public class InitializeArrayListWithNullOrZeros { + + public static void main(String[] args) { + + ArrayList arrayList = new ArrayList<>(); + for (int i = 0; i< 10; i++) { + arrayList.add(null); + } + } +} + diff --git a/core-java-modules/core-java-collections-array-list-2/src/test/java/com/baeldung/InitializeArrayListWithNullOrZerosUnitTest.java b/core-java-modules/core-java-collections-array-list/src/test/java/com/baeldung/initializearraylistwithnullorzeros/InitializeArrayListWithNullOrZerosUnitTest.java similarity index 97% rename from core-java-modules/core-java-collections-array-list-2/src/test/java/com/baeldung/InitializeArrayListWithNullOrZerosUnitTest.java rename to core-java-modules/core-java-collections-array-list/src/test/java/com/baeldung/initializearraylistwithnullorzeros/InitializeArrayListWithNullOrZerosUnitTest.java index 71f8a23690..00987fa198 100644 --- a/core-java-modules/core-java-collections-array-list-2/src/test/java/com/baeldung/InitializeArrayListWithNullOrZerosUnitTest.java +++ b/core-java-modules/core-java-collections-array-list/src/test/java/com/baeldung/initializearraylistwithnullorzeros/InitializeArrayListWithNullOrZerosUnitTest.java @@ -1,4 +1,4 @@ -package com.baeldung; +package com.baeldung.initializearraylistwithnullorzeros; import org.junit.Test; import org.junit.jupiter.api.Assertions; @@ -8,7 +8,6 @@ import java.util.stream.Collectors; import java.util.stream.IntStream; import java.util.stream.Stream; - public class InitializeArrayListWithNullOrZerosUnitTest { @Test From 1ba6c00773a5c95a454d380c713eaaf6f57dbb7d Mon Sep 17 00:00:00 2001 From: "alexandru.borza" Date: Sun, 29 Jan 2023 20:38:26 +0200 Subject: [PATCH 04/10] delete module --- pom.xml | 1 - 1 file changed, 1 deletion(-) diff --git a/pom.xml b/pom.xml index cd3ae35b15..0b19c6eee6 100644 --- a/pom.xml +++ b/pom.xml @@ -1134,7 +1134,6 @@ core-java-modules/core-java-collections-set core-java-modules/core-java-collections-list-4 core-java-modules/core-java-collections-array-list - core-java-modules/core-java-collections-array-list-2 core-java-modules/core-java-collections-maps-4 core-java-modules/core-java-collections-maps-5 core-java-modules/core-java-concurrency-simple From a7e626c07c45b10e9dd129d406a0ab596a8e25d9 Mon Sep 17 00:00:00 2001 From: "alexandru.borza" Date: Mon, 6 Feb 2023 23:24:32 +0200 Subject: [PATCH 05/10] BAEL-6195 --- .../FirstOccurrenceOfAnInteger.java | 16 +++++++ .../FirstOccurrenceOfAnIntegerTests.java | 47 +++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 core-java-modules/core-java-regex-2/src/main/java/com/baeldung/firstoccurrenceofaninteger/FirstOccurrenceOfAnInteger.java create mode 100644 core-java-modules/core-java-regex-2/src/test/java/com/baeldung/firstoccurrenceofaninteger/FirstOccurrenceOfAnIntegerTests.java diff --git a/core-java-modules/core-java-regex-2/src/main/java/com/baeldung/firstoccurrenceofaninteger/FirstOccurrenceOfAnInteger.java b/core-java-modules/core-java-regex-2/src/main/java/com/baeldung/firstoccurrenceofaninteger/FirstOccurrenceOfAnInteger.java new file mode 100644 index 0000000000..a8fde1dc16 --- /dev/null +++ b/core-java-modules/core-java-regex-2/src/main/java/com/baeldung/firstoccurrenceofaninteger/FirstOccurrenceOfAnInteger.java @@ -0,0 +1,16 @@ +package com.baeldung.firstoccurrenceofaninteger; + +public class FirstOccurrenceOfAnInteger { + + public static Integer findFirstInteger(String s) { + int i = 0; + while (i < s.length() && !Character.isDigit(s.charAt(i))) { + i++; + } + int j = i; + while (j < s.length() && Character.isDigit(s.charAt(j))) { + j++; + } + return Integer.parseInt(s.substring(i, j)); + } +} diff --git a/core-java-modules/core-java-regex-2/src/test/java/com/baeldung/firstoccurrenceofaninteger/FirstOccurrenceOfAnIntegerTests.java b/core-java-modules/core-java-regex-2/src/test/java/com/baeldung/firstoccurrenceofaninteger/FirstOccurrenceOfAnIntegerTests.java new file mode 100644 index 0000000000..54779bab9a --- /dev/null +++ b/core-java-modules/core-java-regex-2/src/test/java/com/baeldung/firstoccurrenceofaninteger/FirstOccurrenceOfAnIntegerTests.java @@ -0,0 +1,47 @@ +package com.baeldung.firstoccurrenceofaninteger; + +import java.util.Arrays; +import java.util.List; +import java.util.Scanner; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.stream.Collectors; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Assertions; + + +class FirstOccurrenceOfAnIntegerTests { + + @Test + public void whenUsingPatternMatcher_findFirstInteger() { + String s = "ba31dung123"; + Matcher matcher = Pattern.compile("\\d+").matcher(s); + matcher.find(); + int i = Integer.parseInt(matcher.group()); + Assertions.assertEquals(31, i); + } + + @Test + public void whenUsingScanner_findFirstInteger() { + int i = new Scanner("ba31dung123").useDelimiter("\\D+").nextInt(); + Assertions.assertEquals(31, i); + } + + @Test + public void whenUsingSplit_findFirstInteger() { + String str = "ba31dung123"; + List tokens = Arrays.stream(str.split("\\D+")) + .filter(s -> s.length() > 0).collect(Collectors.toList()); + Assertions.assertEquals(31, Integer.parseInt(tokens.get(0))); + } + + @Test + public void whenUsingCustomFunction_findFirstInteger() { + String str = "ba31dung123"; + Integer i = FirstOccurrenceOfAnInteger.findFirstInteger(str); + Assertions.assertEquals(31, i); + } + + +} From e14feb5351d23b9df5f476e0352a4e0383c10bf5 Mon Sep 17 00:00:00 2001 From: "alexandru.borza" Date: Fri, 17 Mar 2023 19:58:29 +0200 Subject: [PATCH 06/10] time-based UUIDs --- core-java-modules/core-java-uuid/pom.xml | 17 +++++++- .../timebaseduuid/JavaUUIDCreator.java | 42 +++++++++++++++++++ .../baeldung/timebaseduuid/UUIDCreator.java | 41 ++++++++++++++++++ 3 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/JavaUUIDCreator.java create mode 100644 core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/UUIDCreator.java diff --git a/core-java-modules/core-java-uuid/pom.xml b/core-java-modules/core-java-uuid/pom.xml index 7d851292f5..442a266dab 100644 --- a/core-java-modules/core-java-uuid/pom.xml +++ b/core-java-modules/core-java-uuid/pom.xml @@ -25,6 +25,21 @@ log4j-over-slf4j ${org.slf4j.version} + + com.github.f4b6a3 + uuid-creator + 5.2.0 + + + com.fasterxml.uuid + java-uuid-generator + 4.1.0 + + + com.github.f4b6a3 + tsid-creator + 5.2.3 + @@ -143,4 +158,4 @@ 3.0.0-M1 - \ No newline at end of file + diff --git a/core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/JavaUUIDCreator.java b/core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/JavaUUIDCreator.java new file mode 100644 index 0000000000..39308c8982 --- /dev/null +++ b/core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/JavaUUIDCreator.java @@ -0,0 +1,42 @@ +package com.baeldung.timebaseduuid; + +import com.fasterxml.uuid.Generators; +import com.github.f4b6a3.uuid.UuidCreator; + +import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicLong; + +public class JavaUUIDCreator { + + public static void main(String[] args) throws InterruptedException { + + int threadCount = 128; + int iterationCount = 100_000; + ConcurrentMap uuidMap = new ConcurrentHashMap<>(); + AtomicLong collisionCount = new AtomicLong(); + long startNanos = System.nanoTime(); + CountDownLatch endLatch = new CountDownLatch(threadCount); + + for (long i = 0; i < threadCount; i++) { + final long threadId = i; + new Thread(() -> { + for (long j = 0; j < iterationCount; j++) { + UUID uuid = Generators.timeBasedGenerator().generate(); + Long existingUUID = uuidMap.put(uuid, (threadId * iterationCount) + j); + if(existingUUID != null) { + collisionCount.incrementAndGet(); + } + } + endLatch.countDown(); + }).start(); + } + + endLatch.await(); + System.out.println(threadCount * iterationCount + " UUIDs generated, " + collisionCount + " collisions in " + + TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNanos) + "ms"); + } +} diff --git a/core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/UUIDCreator.java b/core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/UUIDCreator.java new file mode 100644 index 0000000000..8695a54bae --- /dev/null +++ b/core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/UUIDCreator.java @@ -0,0 +1,41 @@ +package com.baeldung.timebaseduuid; + +import com.github.f4b6a3.uuid.UuidCreator; + +import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicLong; + +public class UUIDCreator { + + public static void main(String[] args) throws InterruptedException { + + int threadCount = 128; + int iterationCount = 100_000; + ConcurrentMap uuidMap = new ConcurrentHashMap<>(); + AtomicLong collisionCount = new AtomicLong(); + long startNanos = System.nanoTime(); + CountDownLatch endLatch = new CountDownLatch(threadCount); + + for (long i = 0; i < threadCount; i++) { + final long threadId = i; + new Thread(() -> { + for (long j = 0; j < iterationCount; j++) { + UUID uuid = UuidCreator.getTimeBased(); + Long existingUUID = uuidMap.put(uuid, (threadId * iterationCount) + j); + if(existingUUID != null) { + collisionCount.incrementAndGet(); + } + } + endLatch.countDown(); + }).start(); + } + + endLatch.await(); + System.out.println(threadCount * iterationCount + " UUIDs generated, " + collisionCount + " collisions in " + + TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNanos) + "ms"); + } +} From a93d82d800d4919c98dd52e19d757114fa5125cd Mon Sep 17 00:00:00 2001 From: "alexandru.borza" Date: Fri, 24 Mar 2023 22:44:22 +0200 Subject: [PATCH 07/10] review --- .../timebaseduuid/JavaUUIDCreator.java | 42 ------------------- .../JavaUUIDCreatorBenchmark.java | 42 +++++++++++++++++++ .../timebaseduuid/JavaUUIDCreatorExample.java | 13 ++++++ ...Creator.java => UUIDCreatorBenchmark.java} | 2 +- .../timebaseduuid/UUIDCreatorExample.java | 13 ++++++ 5 files changed, 69 insertions(+), 43 deletions(-) delete mode 100644 core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/JavaUUIDCreator.java create mode 100644 core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/JavaUUIDCreatorBenchmark.java create mode 100644 core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/JavaUUIDCreatorExample.java rename core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/{UUIDCreator.java => UUIDCreatorBenchmark.java} (97%) create mode 100644 core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/UUIDCreatorExample.java diff --git a/core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/JavaUUIDCreator.java b/core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/JavaUUIDCreator.java deleted file mode 100644 index 39308c8982..0000000000 --- a/core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/JavaUUIDCreator.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.baeldung.timebaseduuid; - -import com.fasterxml.uuid.Generators; -import com.github.f4b6a3.uuid.UuidCreator; - -import java.util.UUID; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicLong; - -public class JavaUUIDCreator { - - public static void main(String[] args) throws InterruptedException { - - int threadCount = 128; - int iterationCount = 100_000; - ConcurrentMap uuidMap = new ConcurrentHashMap<>(); - AtomicLong collisionCount = new AtomicLong(); - long startNanos = System.nanoTime(); - CountDownLatch endLatch = new CountDownLatch(threadCount); - - for (long i = 0; i < threadCount; i++) { - final long threadId = i; - new Thread(() -> { - for (long j = 0; j < iterationCount; j++) { - UUID uuid = Generators.timeBasedGenerator().generate(); - Long existingUUID = uuidMap.put(uuid, (threadId * iterationCount) + j); - if(existingUUID != null) { - collisionCount.incrementAndGet(); - } - } - endLatch.countDown(); - }).start(); - } - - endLatch.await(); - System.out.println(threadCount * iterationCount + " UUIDs generated, " + collisionCount + " collisions in " - + TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNanos) + "ms"); - } -} diff --git a/core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/JavaUUIDCreatorBenchmark.java b/core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/JavaUUIDCreatorBenchmark.java new file mode 100644 index 0000000000..20b2c127bd --- /dev/null +++ b/core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/JavaUUIDCreatorBenchmark.java @@ -0,0 +1,42 @@ +package com.baeldung.timebaseduuid; + +import com.fasterxml.uuid.Generators; + +import java.util.Map; +import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicLong; + +public class JavaUUIDCreatorBenchmark { + +public static void main(String[] args) throws InterruptedException { + + int threadCount = 128; + int iterationCount = 100_000; + Map uuidMap = new ConcurrentHashMap<>(); + AtomicLong collisionCount = new AtomicLong(); + long startNanos = System.nanoTime(); + CountDownLatch endLatch = new CountDownLatch(threadCount); + + for (long i = 0; i < threadCount; i++) { + long threadId = i; + new Thread(() -> { + for (long j = 0; j < iterationCount; j++) { + UUID uuid = Generators.timeBasedGenerator().generate(); + Long existingUUID = uuidMap.put(uuid, (threadId * iterationCount) + j); + if(existingUUID != null) { + collisionCount.incrementAndGet(); + } + } + endLatch.countDown(); + }).start(); + } + + endLatch.await(); + System.out.println(threadCount * iterationCount + " UUIDs generated, " + collisionCount + " collisions in " + + TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNanos) + "ms"); +} +} + diff --git a/core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/JavaUUIDCreatorExample.java b/core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/JavaUUIDCreatorExample.java new file mode 100644 index 0000000000..b59d7e236a --- /dev/null +++ b/core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/JavaUUIDCreatorExample.java @@ -0,0 +1,13 @@ +package com.baeldung.timebaseduuid; + +import com.fasterxml.uuid.Generators; + +public class JavaUUIDCreatorExample { + + public static void main(String[] args) { + System.out.println("UUID Version 1: " + Generators.timeBasedGenerator().generate()); + System.out.println("UUID Version 6: " + Generators.timeBasedReorderedGenerator().generate()); + System.out.println("UUID Version 7: " + Generators.timeBasedEpochGenerator().generate()); + + } +} diff --git a/core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/UUIDCreator.java b/core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/UUIDCreatorBenchmark.java similarity index 97% rename from core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/UUIDCreator.java rename to core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/UUIDCreatorBenchmark.java index 8695a54bae..04779c3590 100644 --- a/core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/UUIDCreator.java +++ b/core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/UUIDCreatorBenchmark.java @@ -9,7 +9,7 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicLong; -public class UUIDCreator { +public class UUIDCreatorBenchmark { public static void main(String[] args) throws InterruptedException { diff --git a/core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/UUIDCreatorExample.java b/core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/UUIDCreatorExample.java new file mode 100644 index 0000000000..fad2f55c93 --- /dev/null +++ b/core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/UUIDCreatorExample.java @@ -0,0 +1,13 @@ +package com.baeldung.timebaseduuid; + +import com.github.f4b6a3.uuid.UuidCreator; + +public class UUIDCreatorExample { + + public static void main(String[] args) { + System.out.println("UUID Version 1: " + UuidCreator.getTimeBased()); + System.out.println("UUID Version 6: " + UuidCreator.getTimeOrdered()); + System.out.println("UUID Version 7: " + UuidCreator.getTimeOrderedEpoch()); + } +} + From 63b455ed471157e916997497e38a8f9bf047fa2d Mon Sep 17 00:00:00 2001 From: "alexandru.borza" Date: Fri, 24 Mar 2023 22:46:29 +0200 Subject: [PATCH 08/10] review --- .../com/baeldung/timebaseduuid/UUIDCreatorBenchmark.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/UUIDCreatorBenchmark.java b/core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/UUIDCreatorBenchmark.java index 04779c3590..d93cd73a25 100644 --- a/core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/UUIDCreatorBenchmark.java +++ b/core-java-modules/core-java-uuid/src/main/java/com/baeldung/timebaseduuid/UUIDCreatorBenchmark.java @@ -2,6 +2,7 @@ package com.baeldung.timebaseduuid; import com.github.f4b6a3.uuid.UuidCreator; +import java.util.Map; import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; @@ -15,13 +16,13 @@ public class UUIDCreatorBenchmark { int threadCount = 128; int iterationCount = 100_000; - ConcurrentMap uuidMap = new ConcurrentHashMap<>(); + Map uuidMap = new ConcurrentHashMap<>(); AtomicLong collisionCount = new AtomicLong(); long startNanos = System.nanoTime(); CountDownLatch endLatch = new CountDownLatch(threadCount); for (long i = 0; i < threadCount; i++) { - final long threadId = i; + long threadId = i; new Thread(() -> { for (long j = 0; j < iterationCount; j++) { UUID uuid = UuidCreator.getTimeBased(); From c5146ab2c1ad45040dee0b86afb72c8d8c667723 Mon Sep 17 00:00:00 2001 From: "alexandru.borza" Date: Wed, 31 May 2023 22:56:50 +0300 Subject: [PATCH 09/10] BAEL-6024 - Guide to YugabyteDB --- persistence-modules/pom.xml | 1 + .../spring-data-yugabytedb/pom.xml | 55 +++++++++++++++++++ .../src/main/java/com/baeldung/Main.java | 32 +++++++++++ .../src/main/java/com/baeldung/User.java | 44 +++++++++++++++ .../java/com/baeldung/UserRepository.java | 6 ++ .../src/main/resources/application.properties | 4 ++ 6 files changed, 142 insertions(+) create mode 100644 persistence-modules/spring-data-yugabytedb/pom.xml create mode 100644 persistence-modules/spring-data-yugabytedb/src/main/java/com/baeldung/Main.java create mode 100644 persistence-modules/spring-data-yugabytedb/src/main/java/com/baeldung/User.java create mode 100644 persistence-modules/spring-data-yugabytedb/src/main/java/com/baeldung/UserRepository.java create mode 100644 persistence-modules/spring-data-yugabytedb/src/main/resources/application.properties diff --git a/persistence-modules/pom.xml b/persistence-modules/pom.xml index b92fb45e6f..3b2af84cf4 100644 --- a/persistence-modules/pom.xml +++ b/persistence-modules/pom.xml @@ -109,6 +109,7 @@ spring-mybatis spring-persistence-simple + spring-data-yugabytedb diff --git a/persistence-modules/spring-data-yugabytedb/pom.xml b/persistence-modules/spring-data-yugabytedb/pom.xml new file mode 100644 index 0000000000..a14f0e7114 --- /dev/null +++ b/persistence-modules/spring-data-yugabytedb/pom.xml @@ -0,0 +1,55 @@ + + + 4.0.0 + spring-data-yugabytedb + 1.0 + spring-data-yugabytedb + jar + + + com.baeldung + parent-boot-2 + 0.0.1-SNAPSHOT + ../../parent-boot-2 + + + + + org.springframework.boot + spring-boot-starter-web + + + org.projectlombok + lombok + + + org.springframework + spring-test + + + org.springframework.boot + spring-boot-starter-test + test + + + org.postgresql + postgresql + + + org.springframework.boot + spring-boot-starter-data-jpa + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + + + + + diff --git a/persistence-modules/spring-data-yugabytedb/src/main/java/com/baeldung/Main.java b/persistence-modules/spring-data-yugabytedb/src/main/java/com/baeldung/Main.java new file mode 100644 index 0000000000..27b087790a --- /dev/null +++ b/persistence-modules/spring-data-yugabytedb/src/main/java/com/baeldung/Main.java @@ -0,0 +1,32 @@ +package com.baeldung; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Main implements CommandLineRunner { + + @Autowired + private UserRepository userRepository; + + public static void main(String[] args) { + SpringApplication.run(Main.class, args); + } + + @Override + public void run(String... args) throws InterruptedException { + + int iterationCount = 1_000; + int elementsPerIteration = 100; + + for (int i = 0; i < iterationCount; i++) { + for (long j = 0; j < elementsPerIteration; j++) { + User user = new User(); + userRepository.save(user); + } + Thread.sleep(1000); + } + } +} diff --git a/persistence-modules/spring-data-yugabytedb/src/main/java/com/baeldung/User.java b/persistence-modules/spring-data-yugabytedb/src/main/java/com/baeldung/User.java new file mode 100644 index 0000000000..278bc6c9ae --- /dev/null +++ b/persistence-modules/spring-data-yugabytedb/src/main/java/com/baeldung/User.java @@ -0,0 +1,44 @@ +package com.baeldung; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; + +@Entity +@Table(name = "users") +public class User { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Column + private String name; + + Long getId() { + return id; + } + + void setId(Long id) { + this.id = id; + } + + String getName() { + return name; + } + + void setName(String name) { + this.name = name; + } + + @Override + public String toString() { + return "User{" + + "id=" + id + + ", name='" + name + '\'' + + '}'; + } +} diff --git a/persistence-modules/spring-data-yugabytedb/src/main/java/com/baeldung/UserRepository.java b/persistence-modules/spring-data-yugabytedb/src/main/java/com/baeldung/UserRepository.java new file mode 100644 index 0000000000..d04047d416 --- /dev/null +++ b/persistence-modules/spring-data-yugabytedb/src/main/java/com/baeldung/UserRepository.java @@ -0,0 +1,6 @@ +package com.baeldung; + +import org.springframework.data.jpa.repository.JpaRepository; + +public interface UserRepository extends JpaRepository { +} diff --git a/persistence-modules/spring-data-yugabytedb/src/main/resources/application.properties b/persistence-modules/spring-data-yugabytedb/src/main/resources/application.properties new file mode 100644 index 0000000000..fdb3d50ec8 --- /dev/null +++ b/persistence-modules/spring-data-yugabytedb/src/main/resources/application.properties @@ -0,0 +1,4 @@ +spring.datasource.url=jdbc:postgresql://localhost:5433/yugabyte +spring.datasource.username=yugabyte +spring.datasource.password=yugabyte +spring.jpa.hibernate.ddl-auto=create From 3229890606aa3ca74e388e254199b35caccda4a4 Mon Sep 17 00:00:00 2001 From: "alexandru.borza" Date: Wed, 31 May 2023 23:00:07 +0300 Subject: [PATCH 10/10] BAEL-6024 - Guide to YugabyteDB --- .../FirstOccurrenceOfAnInteger.java | 16 ------- .../FirstOccurrenceOfAnIntegerTests.java | 47 ------------------- 2 files changed, 63 deletions(-) delete mode 100644 core-java-modules/core-java-regex-2/src/main/java/com/baeldung/firstoccurrenceofaninteger/FirstOccurrenceOfAnInteger.java delete mode 100644 core-java-modules/core-java-regex-2/src/test/java/com/baeldung/firstoccurrenceofaninteger/FirstOccurrenceOfAnIntegerTests.java diff --git a/core-java-modules/core-java-regex-2/src/main/java/com/baeldung/firstoccurrenceofaninteger/FirstOccurrenceOfAnInteger.java b/core-java-modules/core-java-regex-2/src/main/java/com/baeldung/firstoccurrenceofaninteger/FirstOccurrenceOfAnInteger.java deleted file mode 100644 index a8fde1dc16..0000000000 --- a/core-java-modules/core-java-regex-2/src/main/java/com/baeldung/firstoccurrenceofaninteger/FirstOccurrenceOfAnInteger.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.baeldung.firstoccurrenceofaninteger; - -public class FirstOccurrenceOfAnInteger { - - public static Integer findFirstInteger(String s) { - int i = 0; - while (i < s.length() && !Character.isDigit(s.charAt(i))) { - i++; - } - int j = i; - while (j < s.length() && Character.isDigit(s.charAt(j))) { - j++; - } - return Integer.parseInt(s.substring(i, j)); - } -} diff --git a/core-java-modules/core-java-regex-2/src/test/java/com/baeldung/firstoccurrenceofaninteger/FirstOccurrenceOfAnIntegerTests.java b/core-java-modules/core-java-regex-2/src/test/java/com/baeldung/firstoccurrenceofaninteger/FirstOccurrenceOfAnIntegerTests.java deleted file mode 100644 index 54779bab9a..0000000000 --- a/core-java-modules/core-java-regex-2/src/test/java/com/baeldung/firstoccurrenceofaninteger/FirstOccurrenceOfAnIntegerTests.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.baeldung.firstoccurrenceofaninteger; - -import java.util.Arrays; -import java.util.List; -import java.util.Scanner; -import java.util.regex.Matcher; -import java.util.regex.Pattern; -import java.util.stream.Collectors; - -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.Assertions; - - -class FirstOccurrenceOfAnIntegerTests { - - @Test - public void whenUsingPatternMatcher_findFirstInteger() { - String s = "ba31dung123"; - Matcher matcher = Pattern.compile("\\d+").matcher(s); - matcher.find(); - int i = Integer.parseInt(matcher.group()); - Assertions.assertEquals(31, i); - } - - @Test - public void whenUsingScanner_findFirstInteger() { - int i = new Scanner("ba31dung123").useDelimiter("\\D+").nextInt(); - Assertions.assertEquals(31, i); - } - - @Test - public void whenUsingSplit_findFirstInteger() { - String str = "ba31dung123"; - List tokens = Arrays.stream(str.split("\\D+")) - .filter(s -> s.length() > 0).collect(Collectors.toList()); - Assertions.assertEquals(31, Integer.parseInt(tokens.get(0))); - } - - @Test - public void whenUsingCustomFunction_findFirstInteger() { - String str = "ba31dung123"; - Integer i = FirstOccurrenceOfAnInteger.findFirstInteger(str); - Assertions.assertEquals(31, i); - } - - -}