Merge branch 'master' of https://github.com/eugenp/tutorials into BAEL-6128
This commit is contained in:
commit
8469deeb56
7
.gitignore
vendored
7
.gitignore
vendored
@ -108,3 +108,10 @@ spring-boot-modules/spring-boot-properties-3/*.log
|
|||||||
|
|
||||||
# Localstack
|
# Localstack
|
||||||
**/.localstack
|
**/.localstack
|
||||||
|
|
||||||
|
#libraries-2
|
||||||
|
libraries-2/employee*
|
||||||
|
libraries-2/src/test/resources/crawler4j/**
|
||||||
|
|
||||||
|
#web-modules/ninja
|
||||||
|
devDb*.db
|
@ -74,6 +74,10 @@ Building a single module
|
|||||||
====================
|
====================
|
||||||
To build a specific module, run the command: `mvn clean install` in the module directory.
|
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
|
Building modules from the root of the repository
|
||||||
====================
|
====================
|
||||||
|
@ -43,7 +43,7 @@
|
|||||||
</dependencyManagement>
|
</dependencyManagement>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<spring.version>4.3.4.RELEASE</spring.version>
|
<spring.version>5.3.25</spring.version>
|
||||||
<akka.version>2.4.14</akka.version>
|
<akka.version>2.4.14</akka.version>
|
||||||
</properties>
|
</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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -20,4 +20,8 @@
|
|||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<cxf.version>4.0.0</cxf.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
@ -23,6 +23,16 @@
|
|||||||
<artifactId>cxf-rt-transports-http-jetty</artifactId>
|
<artifactId>cxf-rt-transports-http-jetty</artifactId>
|
||||||
<version>${cxf.version}</version>
|
<version>${cxf.version}</version>
|
||||||
</dependency>
|
</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>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
@ -37,4 +47,10 @@
|
|||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</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>
|
</project>
|
@ -2,8 +2,8 @@ package com.baeldung.cxf.introduction;
|
|||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import javax.jws.WebService;
|
import jakarta.jws.WebService;
|
||||||
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
|
import jakarta.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
|
||||||
|
|
||||||
@WebService
|
@WebService
|
||||||
public interface Baeldung {
|
public interface Baeldung {
|
||||||
|
@ -3,7 +3,7 @@ package com.baeldung.cxf.introduction;
|
|||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import javax.jws.WebService;
|
import jakarta.jws.WebService;
|
||||||
|
|
||||||
@WebService(endpointInterface = "com.baeldung.cxf.introduction.Baeldung")
|
@WebService(endpointInterface = "com.baeldung.cxf.introduction.Baeldung")
|
||||||
public class BaeldungImpl implements Baeldung {
|
public class BaeldungImpl implements Baeldung {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
package com.baeldung.cxf.introduction;
|
package com.baeldung.cxf.introduction;
|
||||||
|
|
||||||
import javax.xml.ws.Endpoint;
|
import jakarta.xml.ws.Endpoint;
|
||||||
|
|
||||||
public class Server {
|
public class Server {
|
||||||
public static void main(String args[]) throws InterruptedException {
|
public static void main(String args[]) throws InterruptedException {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
package com.baeldung.cxf.introduction;
|
package com.baeldung.cxf.introduction;
|
||||||
|
|
||||||
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
|
import jakarta.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
|
||||||
|
|
||||||
@XmlJavaTypeAdapter(StudentAdapter.class)
|
@XmlJavaTypeAdapter(StudentAdapter.class)
|
||||||
public interface Student {
|
public interface Student {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
package com.baeldung.cxf.introduction;
|
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 class StudentAdapter extends XmlAdapter<StudentImpl, Student> {
|
||||||
public StudentImpl marshal(Student student) throws Exception {
|
public StudentImpl marshal(Student student) throws Exception {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
package com.baeldung.cxf.introduction;
|
package com.baeldung.cxf.introduction;
|
||||||
|
|
||||||
import javax.xml.bind.annotation.XmlType;
|
import jakarta.xml.bind.annotation.XmlType;
|
||||||
|
|
||||||
@XmlType(name = "Student")
|
@XmlType(name = "Student")
|
||||||
public class StudentImpl implements Student {
|
public class StudentImpl implements Student {
|
||||||
|
@ -3,8 +3,8 @@ package com.baeldung.cxf.introduction;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import javax.xml.bind.annotation.XmlElement;
|
import jakarta.xml.bind.annotation.XmlElement;
|
||||||
import javax.xml.bind.annotation.XmlType;
|
import jakarta.xml.bind.annotation.XmlType;
|
||||||
|
|
||||||
@XmlType(name = "StudentMap")
|
@XmlType(name = "StudentMap")
|
||||||
public class StudentMap {
|
public class StudentMap {
|
||||||
|
@ -3,7 +3,7 @@ package com.baeldung.cxf.introduction;
|
|||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.Map;
|
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 class StudentMapAdapter extends XmlAdapter<StudentMap, Map<Integer, Student>> {
|
||||||
public StudentMap marshal(Map<Integer, Student> boundMap) throws Exception {
|
public StudentMap marshal(Map<Integer, Student> boundMap) throws Exception {
|
||||||
|
@ -5,8 +5,8 @@ import static org.junit.Assert.assertEquals;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import javax.xml.namespace.QName;
|
import javax.xml.namespace.QName;
|
||||||
import javax.xml.ws.Service;
|
import jakarta.xml.ws.Service;
|
||||||
import javax.xml.ws.soap.SOAPBinding;
|
import jakarta.xml.ws.soap.SOAPBinding;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
@ -16,12 +16,28 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.cxf</groupId>
|
<groupId>org.apache.cxf</groupId>
|
||||||
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
|
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
|
||||||
<version>${cxf.version}</version>
|
<version>4.0.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.cxf</groupId>
|
<groupId>org.apache.cxf</groupId>
|
||||||
<artifactId>cxf-rt-transports-http-jetty</artifactId>
|
<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>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.httpcomponents</groupId>
|
<groupId>org.apache.httpcomponents</groupId>
|
||||||
@ -50,6 +66,9 @@
|
|||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<httpclient.version>4.5.2</httpclient.version>
|
<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>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
@ -1,8 +1,8 @@
|
|||||||
package com.baeldung.cxf.jaxrs.implementation;
|
package com.baeldung.cxf.jaxrs.implementation;
|
||||||
|
|
||||||
import javax.ws.rs.*;
|
import jakarta.ws.rs.*;
|
||||||
import javax.ws.rs.core.Response;
|
import jakarta.ws.rs.core.Response;
|
||||||
import javax.xml.bind.annotation.XmlRootElement;
|
import jakarta.xml.bind.annotation.XmlRootElement;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package com.baeldung.cxf.jaxrs.implementation;
|
package com.baeldung.cxf.jaxrs.implementation;
|
||||||
|
|
||||||
import javax.ws.rs.*;
|
import jakarta.ws.rs.*;
|
||||||
import javax.ws.rs.core.Response;
|
import jakarta.ws.rs.core.Response;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
package com.baeldung.cxf.jaxrs.implementation;
|
package com.baeldung.cxf.jaxrs.implementation;
|
||||||
|
|
||||||
import javax.xml.bind.annotation.XmlRootElement;
|
import jakarta.xml.bind.annotation.XmlRootElement;
|
||||||
|
|
||||||
@XmlRootElement(name = "Student")
|
@XmlRootElement(name = "Student")
|
||||||
public class Student {
|
public class Student {
|
||||||
|
@ -7,7 +7,7 @@ import java.io.InputStream;
|
|||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
|
||||||
import javax.xml.bind.JAXB;
|
import jakarta.xml.bind.JAXB;
|
||||||
|
|
||||||
import org.apache.http.HttpResponse;
|
import org.apache.http.HttpResponse;
|
||||||
import org.apache.http.client.methods.HttpDelete;
|
import org.apache.http.client.methods.HttpDelete;
|
||||||
|
@ -40,10 +40,22 @@
|
|||||||
<artifactId>spring-webmvc</artifactId>
|
<artifactId>spring-webmvc</artifactId>
|
||||||
<version>${spring.version}</version>
|
<version>${spring.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.sun.xml.ws</groupId>
|
||||||
|
<artifactId>jaxws-ri</artifactId>
|
||||||
|
<version>2.3.3</version>
|
||||||
|
<type>pom</type>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>javax.servlet</groupId>
|
<groupId>javax.servlet</groupId>
|
||||||
<artifactId>javax.servlet-api</artifactId>
|
<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>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
@ -103,8 +115,9 @@
|
|||||||
</profiles>
|
</profiles>
|
||||||
|
|
||||||
<properties>
|
<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>
|
<cargo-maven2-plugin.version>1.6.1</cargo-maven2-plugin.version>
|
||||||
|
<maven-war-plugin.version>3.3.2</maven-war-plugin.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
@ -23,6 +23,11 @@
|
|||||||
<artifactId>cxf-rt-rs-sse</artifactId>
|
<artifactId>cxf-rt-rs-sse</artifactId>
|
||||||
<version>${cxf-version}</version>
|
<version>${cxf-version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>jakarta.ws.rs</groupId>
|
||||||
|
<artifactId>jakarta.ws.rs-api</artifactId>
|
||||||
|
<version>${jakarta-ws.version}</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
@ -55,7 +60,8 @@
|
|||||||
</build>
|
</build>
|
||||||
|
|
||||||
<properties>
|
<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>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
@ -1,10 +1,10 @@
|
|||||||
package com.baeldung.sse.jaxrs.client;
|
package com.baeldung.sse.jaxrs.client;
|
||||||
|
|
||||||
import javax.ws.rs.client.Client;
|
import jakarta.ws.rs.client.Client;
|
||||||
import javax.ws.rs.client.ClientBuilder;
|
import jakarta.ws.rs.client.ClientBuilder;
|
||||||
import javax.ws.rs.client.WebTarget;
|
import jakarta.ws.rs.client.WebTarget;
|
||||||
import javax.ws.rs.sse.InboundSseEvent;
|
import jakarta.ws.rs.sse.InboundSseEvent;
|
||||||
import javax.ws.rs.sse.SseEventSource;
|
import jakarta.ws.rs.sse.SseEventSource;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
public class SseClientApp {
|
public class SseClientApp {
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
package com.baeldung.sse.jaxrs.client;
|
package com.baeldung.sse.jaxrs.client;
|
||||||
|
|
||||||
import javax.ws.rs.client.Client;
|
import jakarta.ws.rs.client.Client;
|
||||||
import javax.ws.rs.client.ClientBuilder;
|
import jakarta.ws.rs.client.ClientBuilder;
|
||||||
import javax.ws.rs.client.WebTarget;
|
import jakarta.ws.rs.client.WebTarget;
|
||||||
import javax.ws.rs.sse.InboundSseEvent;
|
import jakarta.ws.rs.sse.InboundSseEvent;
|
||||||
import javax.ws.rs.sse.SseEventSource;
|
import jakarta.ws.rs.sse.SseEventSource;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
@ -15,16 +15,14 @@
|
|||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>javax.ws.rs</groupId>
|
<groupId>jakarta.ws.rs</groupId>
|
||||||
<artifactId>javax.ws.rs-api</artifactId>
|
<artifactId>jakarta.ws.rs-api</artifactId>
|
||||||
<version>${rs-api.version}</version>
|
<version>${jakarta-ws.version}</version>
|
||||||
<scope>provided</scope>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>javax.enterprise</groupId>
|
<groupId>jakarta.enterprise</groupId>
|
||||||
<artifactId>cdi-api</artifactId>
|
<artifactId>jakarta.enterprise.cdi-api</artifactId>
|
||||||
<version>${cdi-api.version}</version>
|
<version>${jakarta-cdi-api}</version>
|
||||||
<scope>provided</scope>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>javax.json.bind</groupId>
|
<groupId>javax.json.bind</groupId>
|
||||||
@ -37,6 +35,11 @@
|
|||||||
<build>
|
<build>
|
||||||
<finalName>${project.artifactId}</finalName>
|
<finalName>${project.artifactId}</finalName>
|
||||||
<plugins>
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-war-plugin</artifactId>
|
||||||
|
<version>${maven-war-plugin.version}</version>
|
||||||
|
</plugin>
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>net.wasdev.wlp.maven.plugins</groupId>
|
<groupId>net.wasdev.wlp.maven.plugins</groupId>
|
||||||
<artifactId>liberty-maven-plugin</artifactId>
|
<artifactId>liberty-maven-plugin</artifactId>
|
||||||
@ -78,9 +81,10 @@
|
|||||||
<liberty-maven-plugin.version>2.4.2</liberty-maven-plugin.version>
|
<liberty-maven-plugin.version>2.4.2</liberty-maven-plugin.version>
|
||||||
<failOnMissingWebXml>false</failOnMissingWebXml>
|
<failOnMissingWebXml>false</failOnMissingWebXml>
|
||||||
<openliberty-version>18.0.0.2</openliberty-version>
|
<openliberty-version>18.0.0.2</openliberty-version>
|
||||||
<rs-api.version>2.1</rs-api.version>
|
<jakarta-ws.version>3.1.0</jakarta-ws.version>
|
||||||
<cdi-api.version>2.0</cdi-api.version>
|
<jakarta-cdi-api>4.0.1</jakarta-cdi-api>
|
||||||
<bind-api.version>1.0</bind-api.version>
|
<bind-api.version>1.0</bind-api.version>
|
||||||
|
<maven-war-plugin.version>3.3.2</maven-war-plugin.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
@ -1,7 +1,7 @@
|
|||||||
package com.baeldung.sse.jaxrs;
|
package com.baeldung.sse.jaxrs;
|
||||||
|
|
||||||
import javax.ws.rs.ApplicationPath;
|
import jakarta.ws.rs.ApplicationPath;
|
||||||
import javax.ws.rs.core.Application;
|
import jakarta.ws.rs.core.Application;
|
||||||
|
|
||||||
@ApplicationPath("sse")
|
@ApplicationPath("sse")
|
||||||
public class AppConfig extends Application {
|
public class AppConfig extends Application {
|
||||||
|
@ -1,15 +1,15 @@
|
|||||||
package com.baeldung.sse.jaxrs;
|
package com.baeldung.sse.jaxrs;
|
||||||
|
|
||||||
import javax.enterprise.context.ApplicationScoped;
|
import jakarta.enterprise.context.ApplicationScoped;
|
||||||
import javax.inject.Inject;
|
import jakarta.inject.Inject;
|
||||||
import javax.ws.rs.*;
|
import jakarta.ws.rs.*;
|
||||||
import javax.ws.rs.core.Context;
|
import jakarta.ws.rs.core.Context;
|
||||||
import javax.ws.rs.core.HttpHeaders;
|
import jakarta.ws.rs.core.HttpHeaders;
|
||||||
import javax.ws.rs.core.MediaType;
|
import jakarta.ws.rs.core.MediaType;
|
||||||
import javax.ws.rs.sse.OutboundSseEvent;
|
import jakarta.ws.rs.sse.OutboundSseEvent;
|
||||||
import javax.ws.rs.sse.Sse;
|
import jakarta.ws.rs.sse.Sse;
|
||||||
import javax.ws.rs.sse.SseBroadcaster;
|
import jakarta.ws.rs.sse.SseBroadcaster;
|
||||||
import javax.ws.rs.sse.SseEventSink;
|
import jakarta.ws.rs.sse.SseEventSink;
|
||||||
|
|
||||||
@ApplicationScoped
|
@ApplicationScoped
|
||||||
@Path("stock")
|
@Path("stock")
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
package com.baeldung.sse.jaxrs;
|
package com.baeldung.sse.jaxrs;
|
||||||
|
|
||||||
import javax.enterprise.context.ApplicationScoped;
|
import jakarta.enterprise.context.ApplicationScoped;
|
||||||
import javax.enterprise.context.Initialized;
|
import jakarta.enterprise.context.Initialized;
|
||||||
import javax.enterprise.event.Event;
|
import jakarta.enterprise.event.Event;
|
||||||
import javax.enterprise.event.Observes;
|
import jakarta.enterprise.event.Observes;
|
||||||
import javax.inject.Inject;
|
import jakarta.inject.Inject;
|
||||||
import javax.inject.Named;
|
import jakarta.inject.Named;
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.math.RoundingMode;
|
import java.math.RoundingMode;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
@ -21,17 +21,6 @@
|
|||||||
<version>${commons-lang3.version}</version>
|
<version>${commons-lang3.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<!-- http client -->
|
<!-- http client -->
|
||||||
<dependency>
|
|
||||||
<groupId>org.apache.httpcomponents</groupId>
|
|
||||||
<artifactId>httpclient</artifactId>
|
|
||||||
<version>${httpclient.version}</version>
|
|
||||||
<exclusions>
|
|
||||||
<exclusion>
|
|
||||||
<artifactId>commons-logging</artifactId>
|
|
||||||
<groupId>commons-logging</groupId>
|
|
||||||
</exclusion>
|
|
||||||
</exclusions>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.httpcomponents.client5</groupId>
|
<groupId>org.apache.httpcomponents.client5</groupId>
|
||||||
<artifactId>httpclient5</artifactId>
|
<artifactId>httpclient5</artifactId>
|
||||||
@ -97,8 +86,7 @@
|
|||||||
<properties>
|
<properties>
|
||||||
<assertj.version>3.22.0</assertj.version>
|
<assertj.version>3.22.0</assertj.version>
|
||||||
<mockserver.version>5.11.2</mockserver.version>
|
<mockserver.version>5.11.2</mockserver.version>
|
||||||
<httpclient.version>4.5.8</httpclient.version>
|
<httpclient5.version>5.2.1</httpclient5.version>
|
||||||
<httpclient5.version>5.2</httpclient5.version>
|
|
||||||
<maven.compiler.source.version>11</maven.compiler.source.version>
|
<maven.compiler.source.version>11</maven.compiler.source.version>
|
||||||
<maven.compiler.target.version>11</maven.compiler.target.version>
|
<maven.compiler.target.version>11</maven.compiler.target.version>
|
||||||
<spring-boot.version>2.1.7.RELEASE</spring-boot.version>
|
<spring-boot.version>2.1.7.RELEASE</spring-boot.version>
|
||||||
|
@ -1,37 +1,39 @@
|
|||||||
package com.baeldung.httpclient.httpclient;
|
package com.baeldung.httpclient.httpclient;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
import org.apache.hc.client5.http.classic.HttpClient;
|
import org.apache.hc.client5.http.classic.HttpClient;
|
||||||
import org.apache.hc.client5.http.classic.methods.HttpGet;
|
import org.apache.hc.client5.http.classic.methods.HttpGet;
|
||||||
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
|
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
|
||||||
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
|
|
||||||
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
|
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
|
||||||
import org.apache.hc.client5.http.impl.classic.HttpClients;
|
import org.apache.hc.client5.http.impl.classic.HttpClients;
|
||||||
import org.apache.hc.core5.http.HttpEntity;
|
import org.apache.hc.core5.http.HttpEntity;
|
||||||
import org.apache.hc.core5.http.HttpResponse;
|
|
||||||
import org.apache.hc.core5.http.HttpStatus;
|
import org.apache.hc.core5.http.HttpStatus;
|
||||||
import org.apache.hc.core5.http.io.entity.EntityUtils;
|
import org.apache.hc.core5.http.io.entity.EntityUtils;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
|
||||||
|
|
||||||
class ApacheHttpClientUnitTest extends GetRequestMockServer {
|
class ApacheHttpClientUnitTest extends GetRequestMockServer {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void givenDeveloperUsedHttpClient_whenExecutingGetRequest_thenStatusIsOkButSonarReportsAnIssue() throws IOException {
|
void givenDeveloperUsedHttpClient_whenExecutingGetRequest_thenStatusIsOkButSonarReportsAnIssue() throws IOException {
|
||||||
HttpClient httpClient = HttpClients.createDefault();
|
HttpClient httpClient = HttpClients.createDefault();
|
||||||
HttpGet httpGet = new HttpGet(serviceOneUrl);
|
HttpGet httpGet = new HttpGet(serviceOneUrl);
|
||||||
HttpResponse response = httpClient.execute(httpGet);
|
httpClient.execute(httpGet, response -> {
|
||||||
assertThat(response.getCode()).isEqualTo(HttpStatus.SC_OK);
|
assertThat(response.getCode()).isEqualTo(HttpStatus.SC_OK);
|
||||||
|
return response;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void givenDeveloperUsedCloseableHttpClient_whenExecutingGetRequest_thenStatusIsOk() throws IOException {
|
void givenDeveloperUsedCloseableHttpClient_whenExecutingGetRequest_thenStatusIsOk() throws IOException {
|
||||||
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
|
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
|
||||||
HttpGet httpGet = new HttpGet(serviceOneUrl);
|
HttpGet httpGet = new HttpGet(serviceOneUrl);
|
||||||
HttpResponse response = httpClient.execute(httpGet);
|
httpClient.execute(httpGet, response -> {
|
||||||
assertThat(response.getCode()).isEqualTo(HttpStatus.SC_OK);
|
assertThat(response.getCode()).isEqualTo(HttpStatus.SC_OK);
|
||||||
|
return response;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -39,20 +41,10 @@ class ApacheHttpClientUnitTest extends GetRequestMockServer {
|
|||||||
void givenDeveloperUsedHttpClientBuilder_whenExecutingGetRequest_thenStatusIsOk() throws IOException {
|
void givenDeveloperUsedHttpClientBuilder_whenExecutingGetRequest_thenStatusIsOk() throws IOException {
|
||||||
try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
|
try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
|
||||||
HttpGet httpGet = new HttpGet(serviceOneUrl);
|
HttpGet httpGet = new HttpGet(serviceOneUrl);
|
||||||
HttpResponse response = httpClient.execute(httpGet);
|
httpClient.execute(httpGet, response -> {
|
||||||
assertThat(response.getCode()).isEqualTo(HttpStatus.SC_OK);
|
assertThat(response.getCode()).isEqualTo(HttpStatus.SC_OK);
|
||||||
}
|
return response;
|
||||||
}
|
});
|
||||||
|
|
||||||
@Test
|
|
||||||
void givenDeveloperUsedCloseableHttpResponse_whenExecutingGetRequest_thenStatusIsOk() throws IOException {
|
|
||||||
try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
|
|
||||||
HttpGet httpGet = new HttpGet(serviceOneUrl);
|
|
||||||
try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
|
|
||||||
HttpEntity entity = response.getEntity();
|
|
||||||
EntityUtils.consume(entity);
|
|
||||||
assertThat(response.getCode()).isEqualTo(HttpStatus.SC_OK);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -60,18 +52,20 @@ class ApacheHttpClientUnitTest extends GetRequestMockServer {
|
|||||||
void givenDeveloperUsedSingleClient_whenExecutingTwoGetRequest_thenStatusIsOk() throws IOException {
|
void givenDeveloperUsedSingleClient_whenExecutingTwoGetRequest_thenStatusIsOk() throws IOException {
|
||||||
try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
|
try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
|
||||||
HttpGet httpGetOne = new HttpGet(serviceOneUrl);
|
HttpGet httpGetOne = new HttpGet(serviceOneUrl);
|
||||||
try (CloseableHttpResponse responseOne = httpClient.execute(httpGetOne)) {
|
httpClient.execute(httpGetOne, responseOne -> {
|
||||||
HttpEntity entityOne = responseOne.getEntity();
|
HttpEntity entityOne = responseOne.getEntity();
|
||||||
EntityUtils.consume(entityOne);
|
EntityUtils.consume(entityOne);
|
||||||
assertThat(responseOne.getCode()).isEqualTo(HttpStatus.SC_OK);
|
assertThat(responseOne.getCode()).isEqualTo(HttpStatus.SC_OK);
|
||||||
}
|
return responseOne;
|
||||||
|
});
|
||||||
|
|
||||||
HttpGet httpGetTwo = new HttpGet(serviceTwoUrl);
|
HttpGet httpGetTwo = new HttpGet(serviceTwoUrl);
|
||||||
try (CloseableHttpResponse responseTwo = httpClient.execute(httpGetTwo)) {
|
httpClient.execute(httpGetTwo, responseTwo -> {
|
||||||
HttpEntity entityTwo = responseTwo.getEntity();
|
HttpEntity entityTwo = httpGetTwo.getEntity();
|
||||||
EntityUtils.consume(entityTwo);
|
EntityUtils.consume(entityTwo);
|
||||||
assertThat(responseTwo.getCode()).isEqualTo(HttpStatus.SC_OK);
|
assertThat(responseTwo.getCode()).isEqualTo(HttpStatus.SC_OK);
|
||||||
}
|
return responseTwo;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
## Apache HttpClient
|
## Apache HttpClient 4
|
||||||
|
|
||||||
This module contains articles about Apache HttpClient 4.5
|
This module contains articles about Apache HttpClient 4.5
|
||||||
|
|
||||||
@ -7,6 +7,8 @@ This module contains articles about Apache HttpClient 4.5
|
|||||||
- [Apache HttpClient with SSL](https://www.baeldung.com/httpclient-ssl)
|
- [Apache HttpClient with SSL](https://www.baeldung.com/httpclient-ssl)
|
||||||
- [Apache HttpClient Timeout](https://www.baeldung.com/httpclient-timeout)
|
- [Apache HttpClient Timeout](https://www.baeldung.com/httpclient-timeout)
|
||||||
- [Custom HTTP Header with the Apache HttpClient](https://www.baeldung.com/httpclient-custom-http-header)
|
- [Custom HTTP Header with the Apache HttpClient](https://www.baeldung.com/httpclient-custom-http-header)
|
||||||
|
- [Apache HttpClient vs. CloseableHttpClient](https://www.baeldung.com/apache-httpclient-vs-closeablehttpclient)
|
||||||
|
- [Expand Shortened URLs with Apache HttpClient](https://www.baeldung.com/apache-httpclient-expand-url)
|
||||||
|
|
||||||
### Running the Tests
|
### Running the Tests
|
||||||
To run the live tests, use the command: mvn clean install -Plive
|
To run the live tests, use the command: mvn clean install -Plive
|
@ -3,9 +3,9 @@
|
|||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
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">
|
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>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<artifactId>httpclient4</artifactId>
|
<artifactId>apache-httpclient4</artifactId>
|
||||||
<version>0.1-SNAPSHOT</version>
|
<version>0.1-SNAPSHOT</version>
|
||||||
<name>httpclient4</name>
|
<name>apache-httpclient4</name>
|
||||||
<packaging>war</packaging>
|
<packaging>war</packaging>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
@ -152,6 +152,11 @@
|
|||||||
</exclusion>
|
</exclusion>
|
||||||
</exclusions>
|
</exclusions>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mock-server</groupId>
|
||||||
|
<artifactId>mockserver-netty</artifactId>
|
||||||
|
<version>${mockserver.version}</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.github.tomakehurst</groupId>
|
<groupId>com.github.tomakehurst</groupId>
|
||||||
<artifactId>wiremock</artifactId>
|
<artifactId>wiremock</artifactId>
|
||||||
@ -284,6 +289,7 @@
|
|||||||
<wiremock.version>2.5.1</wiremock.version>
|
<wiremock.version>2.5.1</wiremock.version>
|
||||||
<httpcore.version>4.4.16</httpcore.version>
|
<httpcore.version>4.4.16</httpcore.version>
|
||||||
<httpclient.version>4.5.14</httpclient.version>
|
<httpclient.version>4.5.14</httpclient.version>
|
||||||
|
<mockserver.version>5.11.2</mockserver.version>
|
||||||
<!-- maven plugins -->
|
<!-- maven plugins -->
|
||||||
<cargo-maven2-plugin.version>1.6.1</cargo-maven2-plugin.version>
|
<cargo-maven2-plugin.version>1.6.1</cargo-maven2-plugin.version>
|
||||||
</properties>
|
</properties>
|
@ -0,0 +1,31 @@
|
|||||||
|
package com.baeldung.httpclient.httpclient;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.apache.http.HttpEntity;
|
||||||
|
import org.apache.http.HttpStatus;
|
||||||
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||||
|
import org.apache.http.client.methods.HttpGet;
|
||||||
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
|
import org.apache.http.impl.client.HttpClientBuilder;
|
||||||
|
import org.apache.http.util.EntityUtils;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
class ApacheHttpClientUnitTest extends GetRequestMockServer {
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenDeveloperUsedCloseableHttpResponse_whenExecutingGetRequest_thenStatusIsOk() throws IOException {
|
||||||
|
try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
|
||||||
|
HttpGet httpGet = new HttpGet(serviceOneUrl);
|
||||||
|
try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
|
||||||
|
HttpEntity entity = response.getEntity();
|
||||||
|
EntityUtils.consume(entity);
|
||||||
|
assertThat(response.getStatusLine().getStatusCode()).isEqualTo(HttpStatus.SC_OK);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,78 @@
|
|||||||
|
package com.baeldung.httpclient.httpclient;
|
||||||
|
|
||||||
|
import org.apache.http.HttpStatus;
|
||||||
|
import org.junit.jupiter.api.AfterAll;
|
||||||
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
|
import org.mockserver.client.MockServerClient;
|
||||||
|
import org.mockserver.integration.ClientAndServer;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.ServerSocket;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
|
|
||||||
|
import static org.mockserver.integration.ClientAndServer.startClientAndServer;
|
||||||
|
import static org.mockserver.matchers.Times.exactly;
|
||||||
|
import static org.mockserver.model.HttpRequest.request;
|
||||||
|
import static org.mockserver.model.HttpResponse.response;
|
||||||
|
|
||||||
|
public class GetRequestMockServer {
|
||||||
|
|
||||||
|
public static ClientAndServer mockServer;
|
||||||
|
public static String serviceOneUrl;
|
||||||
|
public static String serviceTwoUrl;
|
||||||
|
|
||||||
|
private static int serverPort;
|
||||||
|
|
||||||
|
public static final String SERVER_ADDRESS = "127.0.0.1";
|
||||||
|
public static final String PATH_ONE = "/test1";
|
||||||
|
public static final String PATH_TWO = "/test2";
|
||||||
|
public static final String METHOD = "GET";
|
||||||
|
|
||||||
|
@BeforeAll
|
||||||
|
static void startServer() throws IOException, URISyntaxException {
|
||||||
|
serverPort = getFreePort();
|
||||||
|
serviceOneUrl = "http://" + SERVER_ADDRESS + ":" + serverPort + PATH_ONE;
|
||||||
|
serviceTwoUrl = "http://" + SERVER_ADDRESS + ":" + serverPort + PATH_TWO;
|
||||||
|
mockServer = startClientAndServer(serverPort);
|
||||||
|
mockGetRequest();
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterAll
|
||||||
|
static void stopServer() {
|
||||||
|
mockServer.stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void mockGetRequest() {
|
||||||
|
new MockServerClient(SERVER_ADDRESS, serverPort)
|
||||||
|
.when(
|
||||||
|
request()
|
||||||
|
.withPath(PATH_ONE)
|
||||||
|
.withMethod(METHOD),
|
||||||
|
exactly(5)
|
||||||
|
)
|
||||||
|
.respond(
|
||||||
|
response()
|
||||||
|
.withStatusCode(HttpStatus.SC_OK)
|
||||||
|
.withBody("{\"status\":\"ok\"}")
|
||||||
|
);
|
||||||
|
new MockServerClient(SERVER_ADDRESS, serverPort)
|
||||||
|
.when(
|
||||||
|
request()
|
||||||
|
.withPath(PATH_TWO)
|
||||||
|
.withMethod(METHOD),
|
||||||
|
exactly(1)
|
||||||
|
)
|
||||||
|
.respond(
|
||||||
|
response()
|
||||||
|
.withStatusCode(HttpStatus.SC_OK)
|
||||||
|
.withBody("{\"status\":\"ok\"}")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int getFreePort () throws IOException {
|
||||||
|
try (ServerSocket serverSocket = new ServerSocket(0)) {
|
||||||
|
return serverSocket.getLocalPort();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -118,11 +118,6 @@
|
|||||||
<artifactId>curator-recipes</artifactId>
|
<artifactId>curator-recipes</artifactId>
|
||||||
<version>${curator.version}</version>
|
<version>${curator.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>org.apache.zookeeper</groupId>
|
|
||||||
<artifactId>zookeeper</artifactId>
|
|
||||||
<version>${zookeeper.version}</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.fasterxml.jackson.core</groupId>
|
<groupId>com.fasterxml.jackson.core</groupId>
|
||||||
<artifactId>jackson-core</artifactId>
|
<artifactId>jackson-core</artifactId>
|
||||||
@ -191,17 +186,15 @@
|
|||||||
</build>
|
</build>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<maven.compiler.source>1.8</maven.compiler.source>
|
|
||||||
<maven.compiler.target>1.8</maven.compiler.target>
|
|
||||||
<avro.version>1.8.2</avro.version>
|
<avro.version>1.8.2</avro.version>
|
||||||
<beam.version>2.19.0</beam.version>
|
<beam.version>2.45.0</beam.version>
|
||||||
<bval.version>1.1.2</bval.version>
|
<bval.version>2.0.6</bval.version>
|
||||||
<javax.validation.validation-api.version>1.1.0.Final</javax.validation.validation-api.version>
|
<javax.validation.validation-api.version>2.0.1.Final</javax.validation.validation-api.version>
|
||||||
<meecrowave-junit.version>1.2.0</meecrowave-junit.version>
|
<meecrowave-junit.version>1.2.15</meecrowave-junit.version>
|
||||||
<okhttp.version>3.10.0</okhttp.version>
|
<okhttp.version>3.10.0</okhttp.version>
|
||||||
<meecrowave-jpa.version>1.2.1</meecrowave-jpa.version>
|
<meecrowave-jpa.version>1.2.15</meecrowave-jpa.version>
|
||||||
<meecrowave-core.version>1.2.1</meecrowave-core.version>
|
<meecrowave-core.version>1.2.15</meecrowave-core.version>
|
||||||
<meecrowave-maven-plugin.version>1.2.1</meecrowave-maven-plugin.version>
|
<meecrowave-maven-plugin.version>1.2.15</meecrowave-maven-plugin.version>
|
||||||
<opennlp.opennlp-tools.version>1.8.4</opennlp.opennlp-tools.version>
|
<opennlp.opennlp-tools.version>1.8.4</opennlp.opennlp-tools.version>
|
||||||
<pulsar-client.version>2.1.1-incubating</pulsar-client.version>
|
<pulsar-client.version>2.1.1-incubating</pulsar-client.version>
|
||||||
<zookeeper.version>3.4.11</zookeeper.version>
|
<zookeeper.version>3.4.11</zookeeper.version>
|
||||||
|
@ -69,8 +69,8 @@
|
|||||||
</build>
|
</build>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<maven.compiler.source>1.8</maven.compiler.source>
|
<maven.compiler.source>11</maven.compiler.source>
|
||||||
<maven.compiler.target>1.8</maven.compiler.target>
|
<maven.compiler.target>11</maven.compiler.target>
|
||||||
<hibernate.version>5.4.21.Final</hibernate.version>
|
<hibernate.version>5.4.21.Final</hibernate.version>
|
||||||
<aws-lambda-java-core.version>1.2.0</aws-lambda-java-core.version>
|
<aws-lambda-java-core.version>1.2.0</aws-lambda-java-core.version>
|
||||||
<aws-lambda-java-events.version>3.1.0</aws-lambda-java-events.version>
|
<aws-lambda-java-events.version>3.1.0</aws-lambda-java-events.version>
|
||||||
|
@ -101,16 +101,16 @@
|
|||||||
</build>
|
</build>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<maven.compiler.source>1.8</maven.compiler.source>
|
<maven.compiler.source>11</maven.compiler.source>
|
||||||
<maven.compiler.target>1.8</maven.compiler.target>
|
<maven.compiler.target>11</maven.compiler.target>
|
||||||
<aws-lambda-java-core.version>1.2.1</aws-lambda-java-core.version>
|
<aws-lambda-java-core.version>1.2.1</aws-lambda-java-core.version>
|
||||||
<aws-lambda-java-events.version>3.6.0</aws-lambda-java-events.version>
|
<aws-lambda-java-events.version>3.6.0</aws-lambda-java-events.version>
|
||||||
<lightweight-config.version>1.1.0</lightweight-config.version>
|
<lightweight-config.version>1.2.1</lightweight-config.version>
|
||||||
<aws-lambda-java-log4j2.version>1.2.0</aws-lambda-java-log4j2.version>
|
<aws-lambda-java-log4j2.version>1.2.0</aws-lambda-java-log4j2.version>
|
||||||
<log4j-slf4j-impl.version>2.13.2</log4j-slf4j-impl.version>
|
<log4j-slf4j-impl.version>2.13.2</log4j-slf4j-impl.version>
|
||||||
<feign-core.version>11.2</feign-core.version>
|
<feign-core.version>11.2</feign-core.version>
|
||||||
<guice.version>5.0.1</guice.version>
|
<guice.version>5.1.0</guice.version>
|
||||||
<system-stubs-junit4.version>1.2.0</system-stubs-junit4.version>
|
<system-stubs-junit4.version>2.0.2</system-stubs-junit4.version>
|
||||||
<mockito-core.version>4.1.0</mockito-core.version>
|
<mockito-core.version>4.1.0</mockito-core.version>
|
||||||
<assertj-core.version>3.19.0</assertj-core.version>
|
<assertj-core.version>3.19.0</assertj-core.version>
|
||||||
<junit-jupiter.version>5.8.1</junit-jupiter.version>
|
<junit-jupiter.version>5.8.1</junit-jupiter.version>
|
||||||
|
@ -14,4 +14,4 @@ This module contains articles about core Groovy concepts
|
|||||||
- [A Quick Guide to Working with Web Services in Groovy](https://www.baeldung.com/groovy-web-services)
|
- [A Quick Guide to Working with Web Services in Groovy](https://www.baeldung.com/groovy-web-services)
|
||||||
- [Categories in Groovy](https://www.baeldung.com/groovy-categories)
|
- [Categories in Groovy](https://www.baeldung.com/groovy-categories)
|
||||||
- [How to Determine the Data Type in Groovy](https://www.baeldung.com/groovy-determine-data-type)
|
- [How to Determine the Data Type in Groovy](https://www.baeldung.com/groovy-determine-data-type)
|
||||||
- [[<-- Prev]](/core-groovy)
|
- [[<-- Prev]](/core-groovy-modules/core-groovy)
|
||||||
|
@ -83,7 +83,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.codehaus.groovy</groupId>
|
<groupId>org.codehaus.groovy</groupId>
|
||||||
<artifactId>groovy-eclipse-batch</artifactId>
|
<artifactId>groovy-eclipse-batch</artifactId>
|
||||||
<version>${groovy.version}-01</version>
|
<version>${groovy-eclipse-batch.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</plugin>
|
</plugin>
|
||||||
@ -163,9 +163,10 @@
|
|||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<groovy-wslite.version>1.1.3</groovy-wslite.version>
|
<groovy-wslite.version>1.1.3</groovy-wslite.version>
|
||||||
<assembly.plugin.version>3.1.0</assembly.plugin.version>
|
<assembly.plugin.version>3.4.2</assembly.plugin.version>
|
||||||
<compiler.plugin.version>3.8.0</compiler.plugin.version>
|
<compiler.plugin.version>3.8.1</compiler.plugin.version>
|
||||||
<groovy.compiler.version>3.3.0-01</groovy.compiler.version>
|
<groovy.compiler.version>3.7.0</groovy.compiler.version>
|
||||||
|
<groovy-eclipse-batch.version>3.0.8-01</groovy-eclipse-batch.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
@ -21,12 +21,12 @@
|
|||||||
</modules>
|
</modules>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<groovy.version>3.0.8</groovy.version>
|
<groovy.version>3.0.15</groovy.version>
|
||||||
<groovy-all.version>3.0.8</groovy-all.version>
|
<groovy-all.version>3.0.15</groovy-all.version>
|
||||||
<groovy-sql.version>3.0.8</groovy-sql.version>
|
<groovy-sql.version>3.0.15</groovy-sql.version>
|
||||||
<hsqldb.version>2.4.0</hsqldb.version>
|
<hsqldb.version>2.7.1</hsqldb.version>
|
||||||
<spock-core.version>2.3-groovy-3.0</spock-core.version>
|
<spock-core.version>2.3-groovy-3.0</spock-core.version>
|
||||||
<gmavenplus-plugin.version>1.6</gmavenplus-plugin.version>
|
<gmavenplus-plugin.version>2.1.0</gmavenplus-plugin.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
@ -0,0 +1,61 @@
|
|||||||
|
package org.example;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
public class StudentClassic {
|
||||||
|
private String name;
|
||||||
|
private int rollNo;
|
||||||
|
private int marks;
|
||||||
|
|
||||||
|
public StudentClassic(String name, int rollNo, int marks) {
|
||||||
|
this.name = name;
|
||||||
|
this.rollNo = rollNo;
|
||||||
|
this.marks = marks;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getRollNo() {
|
||||||
|
return rollNo;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getMarks() {
|
||||||
|
return marks;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRollNo(int rollNo) {
|
||||||
|
this.rollNo = rollNo;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMarks(int marks) {
|
||||||
|
this.marks = marks;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (this == o) return true;
|
||||||
|
if (o == null || getClass() != o.getClass()) return false;
|
||||||
|
StudentClassic that = (StudentClassic) o;
|
||||||
|
return rollNo == that.rollNo && marks == that.marks && Objects.equals(name, that.name);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(name, rollNo, marks);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "StudentClassic{" +
|
||||||
|
"name='" + name + '\'' +
|
||||||
|
", rollNo=" + rollNo +
|
||||||
|
", marks=" + marks +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,19 @@
|
|||||||
|
package org.example;
|
||||||
|
|
||||||
|
import java.util.Comparator;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public record StudentRecord(String name, int rollNo, int marks) {
|
||||||
|
public StudentRecord(String name){
|
||||||
|
this(name, 0,0);
|
||||||
|
}
|
||||||
|
public StudentRecord {
|
||||||
|
if (marks < 0 || marks > 100) {
|
||||||
|
throw new IllegalArgumentException("Marks should be between 0 and 100.");
|
||||||
|
}
|
||||||
|
if (name == null) {
|
||||||
|
throw new IllegalArgumentException("Name cannot be null");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,22 @@
|
|||||||
|
package org.example;
|
||||||
|
|
||||||
|
record StudentRecordV2(String name, int rollNo, int marks, char grade) {
|
||||||
|
public StudentRecordV2(String name, int rollNo, int marks) {
|
||||||
|
this(name, rollNo, marks, calculateGrade(marks));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static char calculateGrade(int marks) {
|
||||||
|
if (marks >= 90) {
|
||||||
|
return 'A';
|
||||||
|
} else if (marks >= 80) {
|
||||||
|
return 'B';
|
||||||
|
} else if (marks >= 70) {
|
||||||
|
return 'C';
|
||||||
|
} else if (marks >= 60) {
|
||||||
|
return 'D';
|
||||||
|
} else {
|
||||||
|
return 'F';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,11 @@
|
|||||||
|
package org.example;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
record StudentRecordV3(String name, int rollNo, int marks, String id) {
|
||||||
|
|
||||||
|
public StudentRecordV3(String name, int rollNo, int marks) {
|
||||||
|
this(name, rollNo, marks, UUID.randomUUID().toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,37 @@
|
|||||||
|
import org.example.StudentRecord;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Comparator;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class StudentRecordJUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenStudentRecordData_whenCreated_thenStudentPropertiesMatch() {
|
||||||
|
StudentRecord s1 = new StudentRecord("John", 1, 90);
|
||||||
|
StudentRecord s2 = new StudentRecord("Jane", 2, 80);
|
||||||
|
assertEquals("John", s1.name());
|
||||||
|
assertEquals(1, s1.rollNo());
|
||||||
|
assertEquals(90, s1.marks());
|
||||||
|
assertEquals("Jane", s2.name());
|
||||||
|
assertEquals(2, s2.rollNo());
|
||||||
|
assertEquals(80, s2.marks());
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
public void givenStudentRecordsList_whenSortingDataWithName_thenStudentsSorted(){
|
||||||
|
List<StudentRecord> studentRecords = List.of(
|
||||||
|
new StudentRecord("Dana", 1, 85),
|
||||||
|
new StudentRecord("Jim", 2, 90),
|
||||||
|
new StudentRecord("Jane", 3, 80)
|
||||||
|
);
|
||||||
|
|
||||||
|
List<StudentRecord> mutableStudentRecords = new ArrayList<>(studentRecords);
|
||||||
|
mutableStudentRecords.sort(Comparator.comparing(StudentRecord::name));
|
||||||
|
|
||||||
|
List<StudentRecord> sortedStudentRecords = List.copyOf(mutableStudentRecords);
|
||||||
|
assertEquals("Jane", sortedStudentRecords.get(1).name());
|
||||||
|
}
|
||||||
|
}
|
@ -6,3 +6,4 @@
|
|||||||
- [New Features in Java 17](https://www.baeldung.com/java-17-new-features)
|
- [New Features in Java 17](https://www.baeldung.com/java-17-new-features)
|
||||||
- [Random Number Generators in Java 17](https://www.baeldung.com/java-17-random-number-generators)
|
- [Random Number Generators in Java 17](https://www.baeldung.com/java-17-random-number-generators)
|
||||||
- [Sealed Classes and Interfaces in Java](https://www.baeldung.com/java-sealed-classes-interfaces)
|
- [Sealed Classes and Interfaces in Java](https://www.baeldung.com/java-sealed-classes-interfaces)
|
||||||
|
- [Migrate From Java 8 to Java 17](https://www.baeldung.com/java-migrate-8-to-17)
|
||||||
|
@ -10,4 +10,5 @@ This module contains articles about Java 8 core features
|
|||||||
- [Interface With Default Methods vs Abstract Class](https://www.baeldung.com/java-interface-default-method-vs-abstract-class)
|
- [Interface With Default Methods vs Abstract Class](https://www.baeldung.com/java-interface-default-method-vs-abstract-class)
|
||||||
- [Convert Between Byte Array and UUID in Java](https://www.baeldung.com/java-byte-array-to-uuid)
|
- [Convert Between Byte Array and UUID in Java](https://www.baeldung.com/java-byte-array-to-uuid)
|
||||||
- [Create a Simple “Rock-Paper-Scissors” Game in Java](https://www.baeldung.com/java-rock-paper-scissors)
|
- [Create a Simple “Rock-Paper-Scissors” Game in Java](https://www.baeldung.com/java-rock-paper-scissors)
|
||||||
|
- [VarArgs vs Array Input Parameters in Java](https://www.baeldung.com/varargs-vs-array)
|
||||||
- [[<-- Prev]](/core-java-modules/core-java-8)
|
- [[<-- Prev]](/core-java-modules/core-java-8)
|
||||||
|
@ -17,7 +17,7 @@ public class ClassWithSuppressWarningsNames implements Serializable {
|
|||||||
list.add(Integer.valueOf(value));
|
list.add(Integer.valueOf(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("deprecated")
|
@SuppressWarnings("deprecation")
|
||||||
void suppressDeprecatedWarning() {
|
void suppressDeprecatedWarning() {
|
||||||
ClassWithSuppressWarningsNames cls = new ClassWithSuppressWarningsNames();
|
ClassWithSuppressWarningsNames cls = new ClassWithSuppressWarningsNames();
|
||||||
cls.deprecatedMethod(); // no warning here
|
cls.deprecatedMethod(); // no warning here
|
||||||
|
@ -13,4 +13,4 @@
|
|||||||
- [Getting the Size of an Iterable in Java](https://www.baeldung.com/java-iterable-size)
|
- [Getting the Size of an Iterable in Java](https://www.baeldung.com/java-iterable-size)
|
||||||
- [Java Null-Safe Streams from Collections](https://www.baeldung.com/java-null-safe-streams-from-collections)
|
- [Java Null-Safe Streams from Collections](https://www.baeldung.com/java-null-safe-streams-from-collections)
|
||||||
- [Differences Between Iterator and Iterable and How to Use Them?](https://www.baeldung.com/java-iterator-vs-iterable)
|
- [Differences Between Iterator and Iterable and How to Use Them?](https://www.baeldung.com/java-iterator-vs-iterable)
|
||||||
- More articles: [[<-- prev]](/core-java-modules/core-java-collections-1) [[next -->]](/core-java-modules/core-java-collections-3)
|
- More articles: [[<-- prev]](/core-java-modules/core-java-collections) [[next -->]](/core-java-modules/core-java-collections-3)
|
||||||
|
7
core-java-modules/core-java-collections-5/README.md
Normal file
7
core-java-modules/core-java-collections-5/README.md
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
=========
|
||||||
|
|
||||||
|
## Core Java Collections Cookbooks and Examples
|
||||||
|
|
||||||
|
### Relevant Articles:
|
||||||
|
- [Introduction to Roaring Bitmap](https://www.baeldung.com/java-roaring-bitmap-intro)
|
||||||
|
- More articles: [[<-- prev]](/core-java-modules/core-java-collections-4)
|
58
core-java-modules/core-java-collections-5/pom.xml
Normal file
58
core-java-modules/core-java-collections-5/pom.xml
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
<?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-5</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<name>core-java-collections-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>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.platform</groupId>
|
||||||
|
<artifactId>junit-platform-runner</artifactId>
|
||||||
|
<version>${junit-platform.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter</artifactId>
|
||||||
|
<version>${junit.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.vintage</groupId>
|
||||||
|
<artifactId>junit-vintage-engine</artifactId>
|
||||||
|
<version>${junit.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.roaringbitmap</groupId>
|
||||||
|
<artifactId>RoaringBitmap</artifactId>
|
||||||
|
<version>${roaringbitmap.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.openjdk.jmh</groupId>
|
||||||
|
<artifactId>jmh-generator-annprocess</artifactId>
|
||||||
|
<version>${jmh.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.openjdk.jmh</groupId>
|
||||||
|
<artifactId>jmh-core</artifactId>
|
||||||
|
<version>${jmh.version}</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<junit.version>5.9.2</junit.version>
|
||||||
|
<roaringbitmap.version>0.9.38</roaringbitmap.version>
|
||||||
|
<jmh.version>1.36</jmh.version>
|
||||||
|
</properties>
|
||||||
|
</project>
|
@ -0,0 +1,82 @@
|
|||||||
|
package com.baeldung.roaringbitmap;
|
||||||
|
|
||||||
|
import java.util.BitSet;
|
||||||
|
|
||||||
|
import org.openjdk.jmh.annotations.Benchmark;
|
||||||
|
import org.openjdk.jmh.annotations.Scope;
|
||||||
|
import org.openjdk.jmh.annotations.Setup;
|
||||||
|
import org.openjdk.jmh.annotations.State;
|
||||||
|
import org.roaringbitmap.RoaringBitmap;
|
||||||
|
|
||||||
|
@State(Scope.Thread)
|
||||||
|
public class BitSetsBenchmark {
|
||||||
|
private RoaringBitmap rb1;
|
||||||
|
private BitSet bs1;
|
||||||
|
private RoaringBitmap rb2;
|
||||||
|
private BitSet bs2;
|
||||||
|
private final static int SIZE = 10_000_000;
|
||||||
|
|
||||||
|
@Setup
|
||||||
|
public void setup() {
|
||||||
|
rb1 = new RoaringBitmap();
|
||||||
|
bs1 = new BitSet(SIZE);
|
||||||
|
rb2 = new RoaringBitmap();
|
||||||
|
bs2 = new BitSet(SIZE);
|
||||||
|
for (int i = 0; i < SIZE / 2; i++) {
|
||||||
|
rb1.add(i);
|
||||||
|
bs1.set(i);
|
||||||
|
}
|
||||||
|
for (int i = SIZE / 2; i < SIZE; i++) {
|
||||||
|
rb2.add(i);
|
||||||
|
bs2.set(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
public RoaringBitmap roaringBitmapUnion() {
|
||||||
|
return RoaringBitmap.or(rb1, rb2);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
public BitSet bitSetUnion() {
|
||||||
|
BitSet result = (BitSet) bs1.clone();
|
||||||
|
result.or(bs2);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
public RoaringBitmap roaringBitmapIntersection() {
|
||||||
|
return RoaringBitmap.and(rb1, rb2);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
public BitSet bitSetIntersection() {
|
||||||
|
BitSet result = (BitSet) bs1.clone();
|
||||||
|
result.and(bs2);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
public RoaringBitmap roaringBitmapDifference() {
|
||||||
|
return RoaringBitmap.andNot(rb1, rb2);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
public BitSet bitSetDifference() {
|
||||||
|
BitSet result = (BitSet) bs1.clone();
|
||||||
|
result.andNot(bs2);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
public RoaringBitmap roaringBitmapXOR() {
|
||||||
|
return RoaringBitmap.xor(rb1, rb2);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
public BitSet bitSetXOR() {
|
||||||
|
BitSet result = (BitSet) bs1.clone();
|
||||||
|
result.xor(bs2);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
package com.baeldung.roaringbitmap;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class BitSetsBenchmarkRunner {
|
||||||
|
public static void main(String... args) throws IOException {
|
||||||
|
org.openjdk.jmh.Main.main(args);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,45 @@
|
|||||||
|
package com.baeldung.roaringbitmap;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.roaringbitmap.RoaringBitmap;
|
||||||
|
|
||||||
|
class RoaringBitmapBenchmarkUnitTest {
|
||||||
|
@Test
|
||||||
|
public void givenTwoRoaringBitmap_whenUsingOr_thenWillGetSetsUnion() {
|
||||||
|
RoaringBitmap expected = RoaringBitmap.bitmapOf(1, 2, 3, 4, 5, 6, 7, 8);
|
||||||
|
RoaringBitmap A = RoaringBitmap.bitmapOf(1, 2, 3, 4, 5);
|
||||||
|
RoaringBitmap B = RoaringBitmap.bitmapOf(4, 5, 6, 7, 8);
|
||||||
|
RoaringBitmap union = RoaringBitmap.or(A, B);
|
||||||
|
assertEquals(expected, union);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenTwoRoaringBitmap_whenUsingAnd_thenWillGetSetsIntersection() {
|
||||||
|
RoaringBitmap expected = RoaringBitmap.bitmapOf(4, 5);
|
||||||
|
RoaringBitmap A = RoaringBitmap.bitmapOfRange(1, 6);
|
||||||
|
RoaringBitmap B = RoaringBitmap.bitmapOf(4, 5, 6, 7, 8);
|
||||||
|
RoaringBitmap intersection = RoaringBitmap.and(A, B);
|
||||||
|
assertEquals(expected, intersection);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenTwoRoaringBitmap_whenUsingAndNot_thenWillGetSetsDifference() {
|
||||||
|
RoaringBitmap expected = RoaringBitmap.bitmapOf(1, 2, 3);
|
||||||
|
RoaringBitmap A = new RoaringBitmap();
|
||||||
|
A.add(1L, 6L);
|
||||||
|
RoaringBitmap B = RoaringBitmap.bitmapOf(4, 5, 6, 7, 8);
|
||||||
|
RoaringBitmap difference = RoaringBitmap.andNot(A, B);
|
||||||
|
assertEquals(expected, difference);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenTwoRoaringBitmap_whenUsingXOR_thenWillGetSetsSymmetricDifference() {
|
||||||
|
RoaringBitmap expected = RoaringBitmap.bitmapOf(1, 2, 3, 6, 7, 8);
|
||||||
|
RoaringBitmap A = RoaringBitmap.bitmapOfRange(1, 6);
|
||||||
|
RoaringBitmap B = RoaringBitmap.bitmapOfRange(4, 9);
|
||||||
|
RoaringBitmap xor = RoaringBitmap.xor(A, B);
|
||||||
|
assertEquals(expected, xor);
|
||||||
|
}
|
||||||
|
}
|
@ -10,4 +10,5 @@ This module contains articles about conversions among Collection types and array
|
|||||||
- [Arrays.asList vs new ArrayList(Arrays.asList())](https://www.baeldung.com/java-arrays-aslist-vs-new-arraylist)
|
- [Arrays.asList vs new ArrayList(Arrays.asList())](https://www.baeldung.com/java-arrays-aslist-vs-new-arraylist)
|
||||||
- [Iterate Over a Set in Java](https://www.baeldung.com/java-iterate-set)
|
- [Iterate Over a Set in Java](https://www.baeldung.com/java-iterate-set)
|
||||||
- [Convert a List of Integers to a List of Strings](https://www.baeldung.com/java-convert-list-integers-to-list-strings)
|
- [Convert a List of Integers to a List of Strings](https://www.baeldung.com/java-convert-list-integers-to-list-strings)
|
||||||
|
- [Combining Two Lists Into a Map in Java](https://www.baeldung.com/java-combine-two-lists-into-map)
|
||||||
- More articles: [[<-- prev]](../core-java-collections-conversions)
|
- More articles: [[<-- prev]](../core-java-collections-conversions)
|
||||||
|
@ -4,3 +4,4 @@ This module contains articles about the Java List collection
|
|||||||
|
|
||||||
### Relevant Articles:
|
### Relevant Articles:
|
||||||
- [Java List Interface](https://www.baeldung.com/java-list-interface)
|
- [Java List Interface](https://www.baeldung.com/java-list-interface)
|
||||||
|
- [Finding All Duplicates in a List in Java](https://www.baeldung.com/java-list-find-duplicates)
|
||||||
|
@ -0,0 +1,69 @@
|
|||||||
|
package com.baeldung.listduplicate;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.function.Function;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
public class ListDuplicate {
|
||||||
|
public List<Integer> listDuplicateUsingSet(List<Integer> list) {
|
||||||
|
List<Integer> duplicates = new ArrayList<>();
|
||||||
|
Set<Integer> set = new HashSet<>();
|
||||||
|
for (Integer i : list) {
|
||||||
|
if (set.contains(i)) {
|
||||||
|
duplicates.add(i);
|
||||||
|
} else {
|
||||||
|
set.add(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return duplicates;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Integer> listDuplicateUsingMap(List<Integer> list) {
|
||||||
|
List<Integer> duplicates = new ArrayList<>();
|
||||||
|
Map<Integer, Integer> frequencyMap = new HashMap<>();
|
||||||
|
for (Integer number : list) {
|
||||||
|
frequencyMap.put(number, frequencyMap.getOrDefault(number, 0) + 1);
|
||||||
|
}
|
||||||
|
for (int number : frequencyMap.keySet()) {
|
||||||
|
if (frequencyMap.get(number) != 1) {
|
||||||
|
duplicates.add(number);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return duplicates;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Integer> listDuplicateUsingFilterAndSetAdd(List<Integer> list) {
|
||||||
|
Set<Integer> elements = new HashSet<Integer>();
|
||||||
|
return list.stream()
|
||||||
|
.filter(n -> !elements.add(n))
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Integer> listDuplicateUsingCollectionsFrequency(List<Integer> list) {
|
||||||
|
List<Integer> duplicates = new ArrayList<>();
|
||||||
|
Set<Integer> set = list.stream()
|
||||||
|
.filter(i -> Collections.frequency(list, i) > 1)
|
||||||
|
.collect(Collectors.toSet());
|
||||||
|
duplicates.addAll(set);
|
||||||
|
return duplicates;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Integer> listDuplicateUsingMapAndCollectorsGroupingBy(List<Integer> list) {
|
||||||
|
List<Integer> duplicates = new ArrayList<>();
|
||||||
|
Set<Integer> set = list.stream()
|
||||||
|
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
|
||||||
|
.entrySet()
|
||||||
|
.stream()
|
||||||
|
.filter(m -> m.getValue() > 1)
|
||||||
|
.map(Map.Entry::getKey)
|
||||||
|
.collect(Collectors.toSet());
|
||||||
|
duplicates.addAll(set);
|
||||||
|
return duplicates;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,53 @@
|
|||||||
|
package com.baeldung.java.list;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class MovingItemsInArrayListUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAList_whenManuallyReordering_thenOneItemMovesPosition() {
|
||||||
|
ArrayList<String> arrayList = new ArrayList<>(Arrays.asList("one", "two", "three", "four", "five"));
|
||||||
|
|
||||||
|
String removed = arrayList.remove(3);
|
||||||
|
arrayList.add(2, removed);
|
||||||
|
|
||||||
|
ArrayList<String> expectedResult = new ArrayList<>(Arrays.asList("one", "two", "four", "three", "five"));
|
||||||
|
assertEquals(expectedResult, arrayList);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAList_whenUsingSwap_thenItemsSwapPositions() {
|
||||||
|
ArrayList<String> arrayList = new ArrayList<>(Arrays.asList("one", "two", "three", "four", "five"));
|
||||||
|
|
||||||
|
Collections.swap(arrayList, 1, 3);
|
||||||
|
|
||||||
|
ArrayList<String> expectedResult = new ArrayList<>(Arrays.asList("one", "four", "three", "two", "five"));
|
||||||
|
assertEquals(expectedResult, arrayList);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAList_whenUsingRotateWithPositiveDistance_thenItemsMoveToTheRight() {
|
||||||
|
ArrayList<String> arrayList = new ArrayList<>(Arrays.asList("one", "two", "three", "four", "five"));
|
||||||
|
|
||||||
|
Collections.rotate(arrayList, 2);
|
||||||
|
|
||||||
|
ArrayList<String> expectedResult = new ArrayList<>(Arrays.asList("four", "five", "one", "two", "three"));
|
||||||
|
assertEquals(expectedResult, arrayList);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAList_whenUsingRotateWithNegativeDistance_thenItemsMoveToTheLeft() {
|
||||||
|
ArrayList<String> arrayList = new ArrayList<>(Arrays.asList("one", "two", "three", "four", "five"));
|
||||||
|
|
||||||
|
Collections.rotate(arrayList, -2);
|
||||||
|
|
||||||
|
ArrayList<String> expectedResult = new ArrayList<>(Arrays.asList("three", "four", "five", "one", "two"));
|
||||||
|
assertEquals(expectedResult, arrayList);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,68 @@
|
|||||||
|
package com.baeldung.listduplicate;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.BeforeClass;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class ListDuplicateUnitTest {
|
||||||
|
private static ListDuplicate listDuplicate;
|
||||||
|
|
||||||
|
@BeforeClass
|
||||||
|
public static void setup() {
|
||||||
|
listDuplicate = new ListDuplicate();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenList_whenUsingSet_thenReturnDuplicateElements() {
|
||||||
|
List<Integer> list = Arrays.asList(1, 2, 3, 3, 4, 4, 5);
|
||||||
|
List<Integer> duplicates = listDuplicate.listDuplicateUsingSet(list);
|
||||||
|
Assert.assertEquals(duplicates.size(), 2);
|
||||||
|
Assert.assertEquals(duplicates.contains(3), true);
|
||||||
|
Assert.assertEquals(duplicates.contains(4), true);
|
||||||
|
Assert.assertEquals(duplicates.contains(1), false);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenList_whenUsingFrequencyMap_thenReturnDuplicateElements() {
|
||||||
|
List<Integer> list = Arrays.asList(1, 2, 3, 3, 4, 4, 5);
|
||||||
|
List<Integer> duplicates = listDuplicate.listDuplicateUsingMap(list);
|
||||||
|
Assert.assertEquals(duplicates.size(), 2);
|
||||||
|
Assert.assertEquals(duplicates.contains(3), true);
|
||||||
|
Assert.assertEquals(duplicates.contains(4), true);
|
||||||
|
Assert.assertEquals(duplicates.contains(1), false);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenList_whenUsingFilterAndSetAdd_thenReturnDuplicateElements() {
|
||||||
|
List<Integer> list = Arrays.asList(1, 2, 3, 3, 4, 4, 5);
|
||||||
|
List<Integer> duplicates = listDuplicate.listDuplicateUsingFilterAndSetAdd(list);
|
||||||
|
Assert.assertEquals(duplicates.size(), 2);
|
||||||
|
Assert.assertEquals(duplicates.contains(3), true);
|
||||||
|
Assert.assertEquals(duplicates.contains(4), true);
|
||||||
|
Assert.assertEquals(duplicates.contains(1), false);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenList_whenUsingCollectionsFrequency_thenReturnDuplicateElements() {
|
||||||
|
List<Integer> list = Arrays.asList(1, 2, 3, 3, 4, 4, 5);
|
||||||
|
List<Integer> duplicates = listDuplicate.listDuplicateUsingCollectionsFrequency(list);
|
||||||
|
Assert.assertEquals(duplicates.size(), 2);
|
||||||
|
Assert.assertEquals(duplicates.contains(3), true);
|
||||||
|
Assert.assertEquals(duplicates.contains(4), true);
|
||||||
|
Assert.assertEquals(duplicates.contains(1), false);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenList_whenUsingMapAndCollectorsGroupingBy_thenReturnDuplicateElements() {
|
||||||
|
List<Integer> list = Arrays.asList(1, 2, 3, 3, 4, 4, 5);
|
||||||
|
List<Integer> duplicates = listDuplicate.listDuplicateUsingCollectionsFrequency(list);
|
||||||
|
Assert.assertEquals(duplicates.size(), 2);
|
||||||
|
Assert.assertEquals(duplicates.contains(3), true);
|
||||||
|
Assert.assertEquals(duplicates.contains(4), true);
|
||||||
|
Assert.assertEquals(duplicates.contains(1), false);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
2
core-java-modules/core-java-collections-maps-6/README.md
Normal file
2
core-java-modules/core-java-collections-maps-6/README.md
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
## Relevant Articles
|
||||||
|
- [Copying All Keys and Values From One Hashmap Onto Another Without Replacing Existing Keys and Values](https://www.baeldung.com/java-copy-hashmap-no-changes)
|
@ -5,4 +5,5 @@ This module contains articles about basic Java concurrency.
|
|||||||
### Relevant Articles:
|
### Relevant Articles:
|
||||||
|
|
||||||
- [How to Handle InterruptedException in Java](https://www.baeldung.com/java-interrupted-exception)
|
- [How to Handle InterruptedException in Java](https://www.baeldung.com/java-interrupted-exception)
|
||||||
|
- [Thread.sleep() vs Awaitility.await()](https://www.baeldung.com/java-thread-sleep-vs-awaitility-await)
|
||||||
- [[<-- Prev]](../core-java-concurrency-basic-2)
|
- [[<-- Prev]](../core-java-concurrency-basic-2)
|
||||||
|
@ -0,0 +1,29 @@
|
|||||||
|
package com.baeldung.path;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import javax.swing.filechooser.FileSystemView;
|
||||||
|
|
||||||
|
public class DesktopPathUnitTest {
|
||||||
|
// Adapt DESKTOP_PATH variable to your own system path
|
||||||
|
// private static final String DESKTOP_PATH = "C:\\Users\\HRAF\\Desktop";
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenUsingGetUserHomeProperty_thenShouldEqualDesktopPath() {
|
||||||
|
String desktopPath = System.getProperty("user.home") + File.separator + "Desktop";
|
||||||
|
// assertEquals(DESKTOP_PATH, desktopPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenUsingFileSystemViewGetHomeDirectory_thenShouldEqualDesktopPath() {
|
||||||
|
FileSystemView view = FileSystemView.getFileSystemView();
|
||||||
|
File file = view.getHomeDirectory();
|
||||||
|
String path = file.getPath();
|
||||||
|
// assertEquals(DESKTOP_PATH, path);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -51,6 +51,18 @@
|
|||||||
|
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
<version>2.22.2</version>
|
||||||
|
<configuration combine.self="override">
|
||||||
|
<forkCount>3</forkCount>
|
||||||
|
<reuseForks>false</reuseForks>
|
||||||
|
<includes>
|
||||||
|
<include>SpringContextTest</include>
|
||||||
|
<include>**/*UnitTest</include>
|
||||||
|
</includes>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-compiler-plugin</artifactId>
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
@ -21,7 +21,6 @@ public class JndiExceptionsUnitTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Order(1)
|
@Order(1)
|
||||||
@Disabled
|
|
||||||
void givenNoContext_whenLookupObject_thenThrowNoInitialContext() {
|
void givenNoContext_whenLookupObject_thenThrowNoInitialContext() {
|
||||||
assertThrows(NoInitialContextException.class, () -> {
|
assertThrows(NoInitialContextException.class, () -> {
|
||||||
JndiTemplate jndiTemplate = new JndiTemplate();
|
JndiTemplate jndiTemplate = new JndiTemplate();
|
||||||
|
@ -6,4 +6,5 @@ This module contains articles about working with the Java Virtual Machine (JVM).
|
|||||||
|
|
||||||
- [Difference Between Class.getResource() and ClassLoader.getResource()](https://www.baeldung.com/java-class-vs-classloader-getresource)
|
- [Difference Between Class.getResource() and ClassLoader.getResource()](https://www.baeldung.com/java-class-vs-classloader-getresource)
|
||||||
- [Compiling and Executing Code From a String in Java](https://www.baeldung.com/java-string-compile-execute-code)
|
- [Compiling and Executing Code From a String in Java](https://www.baeldung.com/java-string-compile-execute-code)
|
||||||
|
- [Difference Between Class.forName() and Class.forName().newInstance()](https://www.baeldung.com/java-class-forname-vs-class-forname-newinstance)
|
||||||
- More articles: [[<-- prev]](/core-java-modules/core-java-jvm-2)
|
- More articles: [[<-- prev]](/core-java-modules/core-java-jvm-2)
|
||||||
|
@ -10,3 +10,4 @@
|
|||||||
- [Serialize a Lambda in Java](https://www.baeldung.com/java-serialize-lambda)
|
- [Serialize a Lambda in Java](https://www.baeldung.com/java-serialize-lambda)
|
||||||
- [Convert Anonymous Class into Lambda in Java](https://www.baeldung.com/java-from-anonymous-class-to-lambda)
|
- [Convert Anonymous Class into Lambda in Java](https://www.baeldung.com/java-from-anonymous-class-to-lambda)
|
||||||
- [When to Use Callable and Supplier in Java](https://www.baeldung.com/java-callable-vs-supplier)
|
- [When to Use Callable and Supplier in Java](https://www.baeldung.com/java-callable-vs-supplier)
|
||||||
|
- [Lambda Expression vs. Anonymous Inner Class](https://www.baeldung.com/java-lambdas-vs-anonymous-class)
|
||||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user