Merge branch 'master' of https://github.com/eugenp/tutorials into BAEL-6128

This commit is contained in:
Abhinav Pandey
2023-03-12 11:43:55 +05:30
636 changed files with 7748 additions and 2382 deletions
+8 -1
View File
@@ -107,4 +107,11 @@ spring-boot-modules/spring-boot-properties-3/*.log
.sdkmanrc
# Localstack
**/.localstack
**/.localstack
#libraries-2
libraries-2/employee*
libraries-2/src/test/resources/crawler4j/**
#web-modules/ninja
devDb*.db
+4
View File
@@ -74,6 +74,10 @@ Building a single module
====================
To build a specific module, run the command: `mvn clean install` in the module directory.
It can happen that your module is part of a parent module e.g. `parent-boot-1`,`parent-spring-5` etc, then you will need to build the parent module first so that you can build your module.
We have created a `parents` profile that you can use to build just the parent modules, just run the profile as:
`mvn clean install -Pparents`
Building modules from the root of the repository
====================
+1 -1
View File
@@ -43,7 +43,7 @@
</dependencyManagement>
<properties>
<spring.version>4.3.4.RELEASE</spring.version>
<spring.version>5.3.25</spring.version>
<akka.version>2.4.14</akka.version>
</properties>
@@ -0,0 +1,58 @@
package com.baeldung.algorithms.checktargetsum;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class CheckTargetSum {
public boolean isTargetSumExistNaive(int[] nums, int target) {
for (int i = 0; i < nums.length; i++) {
for (int j = i + 1; j < nums.length; j++) {
if (nums[i] + nums[j] == target) {
return true;
}
}
}
return false;
}
public boolean isTargetSumExistSorted(int[] nums, int target) {
Arrays.sort(nums);
int start = 0;
int end = nums.length - 1;
while (start < end) {
int sum = nums[start] + nums[end];
if (sum == target) {
return true;
}
if (sum < target) {
start++;
} else {
end--;
}
}
return false;
}
public boolean isTargetSumExistHashSet(int[] nums, int target) {
Set<Integer> hashSet = new HashSet<>();
for (int num : nums) {
int diff = target - num;
if (hashSet.contains(diff)) {
return true;
}
hashSet.add(num);
}
return false;
}
}
@@ -0,0 +1,46 @@
package com.baeldung.algorithms.checktargetsum;
import org.junit.Test;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class CheckTargetSumUnitTest {
private CheckTargetSum checkTargetSum = new CheckTargetSum();
private int[] nums = new int[] { 10, 5, 15, 7, 14, 1, 9 };
private int existingTarget = 6;
private int nonExistingTarget = 27;
@Test
public void givenArrayOfIntegers_whenTargetSumNaive_thenPairExists() {
assertTrue(checkTargetSum.isTargetSumExistNaive(nums, existingTarget));
}
@Test
public void givenArrayOfIntegers_whenTargetSumNaive_thenPairDoesNotExists() {
assertFalse(checkTargetSum.isTargetSumExistNaive(nums, nonExistingTarget));
}
@Test
public void givenArrayOfIntegers_whenTargetSumSorted_thenPairExists() {
assertTrue(checkTargetSum.isTargetSumExistNaive(nums, existingTarget));
}
@Test
public void givenArrayOfIntegers_whenTargetSumSorted_thenPairDoesNotExists() {
assertFalse(checkTargetSum.isTargetSumExistNaive(nums, nonExistingTarget));
}
@Test
public void givenArrayOfIntegers_whenTargetSumHashSet_thenPairExists() {
assertTrue(checkTargetSum.isTargetSumExistNaive(nums, existingTarget));
}
@Test
public void givenArrayOfIntegers_whenTargetSumHashSet_thenPairDoesNotExists() {
assertFalse(checkTargetSum.isTargetSumExistNaive(nums, nonExistingTarget));
}
}
@@ -0,0 +1,103 @@
package com.baeldung.algorithms.frequentelements;
import java.util.*;
import java.util.stream.Collectors;
public class MostFrequentElementsFinder {
public static List<Integer> findByHashMapAndPriorityQueue(Integer[] array, int n) {
Map<Integer, Integer> countMap = new HashMap<>();
// For each element i in the array, add it to the countMap and increment its count.
for (Integer i : array) {
countMap.put(i, countMap.getOrDefault(i, 0) + 1);
}
// Create a max heap (priority queue) that will prioritize elements with higher counts.
PriorityQueue<Integer> heap = new PriorityQueue<>(
(a, b) -> countMap.get(b) - countMap.get(a));
// Add all the unique elements in the array to the heap.
heap.addAll(countMap.keySet());
List<Integer> result = new ArrayList<>();
for (int i = 0; i < n && !heap.isEmpty(); i++) {
// Poll the highest-count element from the heap and add it to the result list.
result.add(heap.poll());
}
return result;
}
public static List<Integer> findByStream(Integer[] arr, int n) {
// Create a Map to count occurrences of each element
Map<Integer, Long> countMap = Arrays.stream(arr)
.collect(Collectors.groupingBy(i -> i, Collectors.counting()));
// Sort the elements by occurrence count
List<Integer> sortedKeys = countMap.entrySet().stream()
.sorted(Collections.reverseOrder(Map.Entry.comparingByValue()))
.map(Map.Entry::getKey)
.collect(Collectors.toList());
// Extract the n most frequent elements from the sorted list
List<Integer> result = new ArrayList<>();
for (int i = 0; i < n && i < sortedKeys.size(); i++) {
result.add(sortedKeys.get(i));
}
return result;
}
public static List<Integer> findByTreeMap(Integer[] arr, int n) {
// Create a TreeMap and use a reverse order comparator to sort the entries by frequency in descending order
Map<Integer, Integer> countMap = new TreeMap<>(Collections.reverseOrder());
for (int i : arr) {
countMap.put(i, countMap.getOrDefault(i, 0) + 1);
}
// Create a list of the map entries and sort them by value (i.e. by frequency) in descending order
List<Map.Entry<Integer, Integer>> sortedEntries = new ArrayList<>(countMap.entrySet());
sortedEntries.sort((e1, e2) -> e2.getValue().compareTo(e1.getValue()));
// Extract the n most frequent elements from the sorted list of entries
List<Integer> result = new ArrayList<>();
for (int i = 0; i < n && i < sortedEntries.size(); i++) {
result.add(sortedEntries.get(i).getKey());
}
return result;
}
public static List<Integer> findByBucketSort(Integer[] arr, int n) {
List<Integer> result = new ArrayList<>();
Map<Integer, Integer> freqMap = new HashMap<>();
List<Integer>[] bucket = new List[arr.length + 1];
// Loop through the input array and update the frequency count of each element in the HashMap
for (int num : arr) {
freqMap.put(num, freqMap.getOrDefault(num, 0) + 1);
}
// Loop through the HashMap and add each element to its corresponding bucket based on its frequency count
for (int num : freqMap.keySet()) {
int freq = freqMap.get(num);
if (bucket[freq] == null) {
bucket[freq] = new ArrayList<>();
}
bucket[freq].add(num);
}
// Loop through the bucket array in reverse order and add the elements to the result list
// until we have found the n most frequent elements
for (int i = bucket.length - 1; i >= 0 && result.size() < n; i--) {
if (bucket[i] != null) {
result.addAll(bucket[i]);
}
}
// Return a sublist of the result list containing only the first n elements
return result.subList(0, n);
}
}
@@ -0,0 +1,33 @@
package com.baeldung.algorithms.frequentelements;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Java6Assertions.assertThat;
public class MostFrequentElementsUnitTest {
private final Integer[] inputArray = {1, 2, 3, 4, 5, 5, 5, 6, 6, 7, 7, 7, 7, 8, 9, 9, 9, 9, 9};
private final int n = 3;
private final Integer[] outputArray = {9, 7, 5};
@Test
void givenIntegersArray_UseFindByHashMapAndPriorityQueueMethod_thenReturnMostFrequentElements() {
assertThat(MostFrequentElementsFinder.findByHashMapAndPriorityQueue(inputArray, n)).containsExactly(outputArray);
}
@Test
void givenIntegersArray_UseFindByBucketSortMethod_thenReturnMostFrequentElements() {
assertThat(MostFrequentElementsFinder.findByBucketSort(inputArray, n)).containsExactly(outputArray);
}
@Test
void givenIntegersArray_UseFindByStreamMethod_thenReturnMostFrequentElements() {
assertThat(MostFrequentElementsFinder.findByStream(inputArray, n)).containsExactly(outputArray);
}
@Test
void givenIntegersArray_UseFindByTreeMapMethod_thenReturnMostFrequentElements() {
assertThat(MostFrequentElementsFinder.findByTreeMap(inputArray, n)).containsExactly(outputArray);
}
}
+4
View File
@@ -20,4 +20,8 @@
</dependency>
</dependencies>
<properties>
<cxf.version>4.0.0</cxf.version>
</properties>
</project>
@@ -23,6 +23,16 @@
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>jakarta.xml.ws</groupId>
<artifactId>jakarta.xml.ws-api</artifactId>
<version>${jakarta-xml.version}</version>
</dependency>
<dependency>
<groupId>jakarta.jws</groupId>
<artifactId>jakarta.jws-api</artifactId>
<version>${jakarta.jws.version}</version>
</dependency>
</dependencies>
<build>
@@ -37,4 +47,10 @@
</plugins>
</build>
<properties>
<cxf.version>4.0.0</cxf.version>
<jakarta-xml.version>4.0.0</jakarta-xml.version>
<jakarta.jws.version>3.0.0</jakarta.jws.version>
</properties>
</project>
@@ -2,8 +2,8 @@ package com.baeldung.cxf.introduction;
import java.util.Map;
import javax.jws.WebService;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import jakarta.jws.WebService;
import jakarta.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
@WebService
public interface Baeldung {
@@ -3,7 +3,7 @@ package com.baeldung.cxf.introduction;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.jws.WebService;
import jakarta.jws.WebService;
@WebService(endpointInterface = "com.baeldung.cxf.introduction.Baeldung")
public class BaeldungImpl implements Baeldung {
@@ -1,6 +1,6 @@
package com.baeldung.cxf.introduction;
import javax.xml.ws.Endpoint;
import jakarta.xml.ws.Endpoint;
public class Server {
public static void main(String args[]) throws InterruptedException {
@@ -1,6 +1,6 @@
package com.baeldung.cxf.introduction;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import jakarta.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
@XmlJavaTypeAdapter(StudentAdapter.class)
public interface Student {
@@ -1,6 +1,6 @@
package com.baeldung.cxf.introduction;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import jakarta.xml.bind.annotation.adapters.XmlAdapter;
public class StudentAdapter extends XmlAdapter<StudentImpl, Student> {
public StudentImpl marshal(Student student) throws Exception {
@@ -1,6 +1,6 @@
package com.baeldung.cxf.introduction;
import javax.xml.bind.annotation.XmlType;
import jakarta.xml.bind.annotation.XmlType;
@XmlType(name = "Student")
public class StudentImpl implements Student {
@@ -3,8 +3,8 @@ package com.baeldung.cxf.introduction;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
import jakarta.xml.bind.annotation.XmlElement;
import jakarta.xml.bind.annotation.XmlType;
@XmlType(name = "StudentMap")
public class StudentMap {
@@ -3,7 +3,7 @@ package com.baeldung.cxf.introduction;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import jakarta.xml.bind.annotation.adapters.XmlAdapter;
public class StudentMapAdapter extends XmlAdapter<StudentMap, Map<Integer, Student>> {
public StudentMap marshal(Map<Integer, Student> boundMap) throws Exception {
@@ -5,8 +5,8 @@ import static org.junit.Assert.assertEquals;
import java.util.Map;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.soap.SOAPBinding;
import jakarta.xml.ws.Service;
import jakarta.xml.ws.soap.SOAPBinding;
import org.junit.Before;
import org.junit.Test;
@@ -16,12 +16,28 @@
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>${cxf.version}</version>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>${cxf.version}</version>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>jakarta.xml.ws</groupId>
<artifactId>jakarta.xml.ws-api</artifactId>
<version>${jakarta-xml.version}</version>
</dependency>
<dependency>
<groupId>jakarta.jws</groupId>
<artifactId>jakarta.jws-api</artifactId>
<version>${jakarta-jws.version}</version>
</dependency>
<dependency>
<groupId>jakarta.platform</groupId>
<artifactId>jakarta.jakartaee-web-api</artifactId>
<version>${jakarta-platform.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
@@ -50,6 +66,9 @@
<properties>
<httpclient.version>4.5.2</httpclient.version>
<jakarta-xml.version>4.0.0</jakarta-xml.version>
<jakarta-jws.version>3.0.0</jakarta-jws.version>
<jakarta-platform.version>9.0.0</jakarta-platform.version>
</properties>
</project>
@@ -1,8 +1,8 @@
package com.baeldung.cxf.jaxrs.implementation;
import javax.ws.rs.*;
import javax.ws.rs.core.Response;
import javax.xml.bind.annotation.XmlRootElement;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.Response;
import jakarta.xml.bind.annotation.XmlRootElement;
import java.util.ArrayList;
import java.util.List;
@@ -1,7 +1,7 @@
package com.baeldung.cxf.jaxrs.implementation;
import javax.ws.rs.*;
import javax.ws.rs.core.Response;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.Response;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -1,6 +1,6 @@
package com.baeldung.cxf.jaxrs.implementation;
import javax.xml.bind.annotation.XmlRootElement;
import jakarta.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "Student")
public class Student {
@@ -7,7 +7,7 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import javax.xml.bind.JAXB;
import jakarta.xml.bind.JAXB;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpDelete;
+15 -2
View File
@@ -40,10 +40,22 @@
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-ri</artifactId>
<version>2.3.3</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${javax.servlet-api.version}</version>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
@@ -103,8 +115,9 @@
</profiles>
<properties>
<spring.version>4.3.4.RELEASE</spring.version>
<spring.version>5.3.25</spring.version>
<cargo-maven2-plugin.version>1.6.1</cargo-maven2-plugin.version>
<maven-war-plugin.version>3.3.2</maven-war-plugin.version>
</properties>
</project>
@@ -23,6 +23,11 @@
<artifactId>cxf-rt-rs-sse</artifactId>
<version>${cxf-version}</version>
</dependency>
<dependency>
<groupId>jakarta.ws.rs</groupId>
<artifactId>jakarta.ws.rs-api</artifactId>
<version>${jakarta-ws.version}</version>
</dependency>
</dependencies>
<build>
@@ -55,7 +60,8 @@
</build>
<properties>
<cxf-version>3.2.0</cxf-version>
<cxf-version>4.0.0</cxf-version>
<jakarta-ws.version>3.1.0</jakarta-ws.version>
</properties>
</project>
@@ -1,10 +1,10 @@
package com.baeldung.sse.jaxrs.client;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.sse.InboundSseEvent;
import javax.ws.rs.sse.SseEventSource;
import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.ClientBuilder;
import jakarta.ws.rs.client.WebTarget;
import jakarta.ws.rs.sse.InboundSseEvent;
import jakarta.ws.rs.sse.SseEventSource;
import java.util.function.Consumer;
public class SseClientApp {
@@ -1,10 +1,10 @@
package com.baeldung.sse.jaxrs.client;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.sse.InboundSseEvent;
import javax.ws.rs.sse.SseEventSource;
import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.ClientBuilder;
import jakarta.ws.rs.client.WebTarget;
import jakarta.ws.rs.sse.InboundSseEvent;
import jakarta.ws.rs.sse.SseEventSource;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
@@ -15,16 +15,14 @@
<dependencies>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>${rs-api.version}</version>
<scope>provided</scope>
<groupId>jakarta.ws.rs</groupId>
<artifactId>jakarta.ws.rs-api</artifactId>
<version>${jakarta-ws.version}</version>
</dependency>
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<version>${cdi-api.version}</version>
<scope>provided</scope>
<groupId>jakarta.enterprise</groupId>
<artifactId>jakarta.enterprise.cdi-api</artifactId>
<version>${jakarta-cdi-api}</version>
</dependency>
<dependency>
<groupId>javax.json.bind</groupId>
@@ -37,6 +35,11 @@
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>${maven-war-plugin.version}</version>
</plugin>
<plugin>
<groupId>net.wasdev.wlp.maven.plugins</groupId>
<artifactId>liberty-maven-plugin</artifactId>
@@ -78,9 +81,10 @@
<liberty-maven-plugin.version>2.4.2</liberty-maven-plugin.version>
<failOnMissingWebXml>false</failOnMissingWebXml>
<openliberty-version>18.0.0.2</openliberty-version>
<rs-api.version>2.1</rs-api.version>
<cdi-api.version>2.0</cdi-api.version>
<jakarta-ws.version>3.1.0</jakarta-ws.version>
<jakarta-cdi-api>4.0.1</jakarta-cdi-api>
<bind-api.version>1.0</bind-api.version>
<maven-war-plugin.version>3.3.2</maven-war-plugin.version>
</properties>
</project>
@@ -1,7 +1,7 @@
package com.baeldung.sse.jaxrs;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import jakarta.ws.rs.ApplicationPath;
import jakarta.ws.rs.core.Application;
@ApplicationPath("sse")
public class AppConfig extends Application {
@@ -1,15 +1,15 @@
package com.baeldung.sse.jaxrs;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.sse.OutboundSseEvent;
import javax.ws.rs.sse.Sse;
import javax.ws.rs.sse.SseBroadcaster;
import javax.ws.rs.sse.SseEventSink;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.sse.OutboundSseEvent;
import jakarta.ws.rs.sse.Sse;
import jakarta.ws.rs.sse.SseBroadcaster;
import jakarta.ws.rs.sse.SseEventSink;
@ApplicationScoped
@Path("stock")

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