Merge remote-tracking branch 'upstream/master'

This commit is contained in:
mcasari
2023-09-21 20:28:41 +02:00
57 changed files with 836 additions and 134 deletions
@@ -0,0 +1,96 @@
package com.baeldung.mergeandremoveduplicate;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Stream;
public class MergeArraysAndRemoveDuplicate {
public static int[] removeDuplicateOnSortedArray(int[] arr) {
// Initialize a new array to store unique elements
int[] uniqueArray = new int[arr.length];
uniqueArray[0] = arr[0];
int uniqueCount = 1;
// Iterate through the sorted array to remove duplicates
for (int i = 1; i < arr.length; i++) {
if (arr[i] != arr[i - 1]) {
uniqueArray[uniqueCount] = arr[i];
uniqueCount++;
}
}
int[] truncatedArray = new int[uniqueCount];
System.arraycopy(uniqueArray, 0, truncatedArray, 0, uniqueCount);
return truncatedArray;
}
public static int[] mergeAndRemoveDuplicatesUsingStream(int[] arr1, int[] arr2) {
Stream<Integer> s1 = Arrays.stream(arr1).boxed();
Stream<Integer> s2 = Arrays.stream(arr2).boxed();
return Stream.concat(s1, s2)
.distinct()
.mapToInt(Integer::intValue)
.toArray();
}
public static int[] mergeAndRemoveDuplicatesUsingSet(int[] arr1, int[] arr2) {
int[] mergedArr = mergeArrays(arr1, arr2);
Set<Integer> uniqueInts = new HashSet<>();
for (int el : mergedArr) {
uniqueInts.add(el);
}
return getArrayFromSet(uniqueInts);
}
private static int[] getArrayFromSet(Set<Integer> set) {
int[] mergedArrWithoutDuplicated = new int[set.size()];
int i = 0;
for (Integer el: set) {
mergedArrWithoutDuplicated[i] = el;
i++;
}
return mergedArrWithoutDuplicated;
}
public static int[] mergeAndRemoveDuplicates(int[] arr1, int[] arr2) {
return removeDuplicate(mergeArrays(arr1, arr2));
}
public static int[] mergeAndRemoveDuplicatesOnSortedArray(int[] arr1, int[] arr2) {
return removeDuplicateOnSortedArray(mergeArrays(arr1, arr2));
}
private static int[] mergeArrays(int[] arr1, int[] arr2) {
int[] mergedArrays = new int[arr1.length + arr2.length];
System.arraycopy(arr1, 0, mergedArrays, 0, arr1.length);
System.arraycopy(arr2, 0, mergedArrays, arr1.length, arr2.length);
return mergedArrays;
}
private static int[] removeDuplicate(int[] arr) {
int[] withoutDuplicates = new int[arr.length];
int i = 0;
for(int element : arr) {
if(!isElementPresent(withoutDuplicates, element)) {
withoutDuplicates[i] = element;
i++;
}
}
int[] truncatedArray = new int[i];
System.arraycopy(withoutDuplicates, 0, truncatedArray, 0, i);
return truncatedArray;
}
private static boolean isElementPresent(int[] arr, int element) {
for(int el : arr) {
if(el == element) {
return true;
}
}
return false;
}
}
@@ -0,0 +1,74 @@
package com.baeldung.mergeandremoveduplicate;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Arrays;
import java.util.stream.Collectors;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
public class MergeArraysAndRemoveDuplicateUnitTest {
private final Logger logger = LoggerFactory.getLogger(MergeArraysAndRemoveDuplicateUnitTest.class);
private static String getCommaDelimited(int[] arr) {
return Arrays.stream(arr)
.mapToObj(Integer::toString)
.collect(Collectors.joining(", "));
}
@Test
public void givenNoLibraryAndUnSortedArrays_whenArr1andArr2_thenMergeAndRemoveDuplicates() {
int[] arr1 = {3, 2, 1, 4, 5, 6, 8, 7, 9};
int[] arr2 = {8, 9, 10, 11, 12, 13, 15, 14, 15, 14, 16, 17};
int[] expectedArr = {3, 2, 1, 4, 5, 6, 8, 7, 9, 10, 11, 12, 13, 15, 14, 16, 17};
int[] mergedArr = MergeArraysAndRemoveDuplicate.mergeAndRemoveDuplicates(arr1, arr2);
//merged array maintains the order of the elements in the arrays
assertArrayEquals(expectedArr, mergedArr);
logger.info(getCommaDelimited(mergedArr));
}
@Test
public void givenNoLibraryAndSortedArrays_whenArr1andArr2_thenMergeAndRemoveDuplicates() {
int[] arr1 = {1, 2, 3, 4, 5, 5, 6, 7, 7, 8};
int[] arr2 = {8, 9, 10, 11, 12, 13, 14, 15, 15, 16, 17};
int[] expectedArr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17};
int[] mergedArr = MergeArraysAndRemoveDuplicate.mergeAndRemoveDuplicatesOnSortedArray(arr1, arr2);
assertArrayEquals(expectedArr, mergedArr);
logger.info(getCommaDelimited(mergedArr));
}
@Test
public void givenSet_whenArr1andArr2_thenMergeAndRemoveDuplicates() {
int[] arr1 = {3, 2, 1, 4, 5, 6, 8, 7, 9};
int[] arr2 = {8, 9, 10, 11, 12, 13, 15, 14, 15, 14, 16, 17};
int[] expectedArr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17};
int[] mergedArr = MergeArraysAndRemoveDuplicate.mergeAndRemoveDuplicatesUsingSet(arr1, arr2);
assertArrayEquals(expectedArr, mergedArr);
logger.info(getCommaDelimited(mergedArr));
}
@Test
public void givenStream_whenArr1andArr2_thenMergeAndRemoveDuplicates() {
int[] arr1 = {3, 2, 1, 4, 5, 6, 8, 7, 9};
int[] arr2 = {8, 9, 10, 11, 12, 13, 15, 14, 15, 14, 16, 17};
int[] expectedArr = {3, 2, 1, 4, 5, 6, 8, 7, 9, 10, 11, 12, 13, 15, 14, 16, 17};
int[] mergedArr = MergeArraysAndRemoveDuplicate.mergeAndRemoveDuplicatesUsingStream(arr1, arr2);
assertArrayEquals(expectedArr, mergedArr);
logger.info(getCommaDelimited(mergedArr));
}
}
@@ -0,0 +1,46 @@
<?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-arrays-operations-advanced-2</artifactId>
<name>core-java-arrays-operations-advanced-2</name>
<packaging>jar</packaging>
<parent>
<artifactId>core-java-modules</artifactId>
<groupId>com.baeldung.core-java-modules</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
@@ -86,4 +86,4 @@ public class MiddleOfArrayUnitTest {
MiddleOfArray middleOfArray = new MiddleOfArray();
Assert.assertEquals(expectMedian, middleOfArray.medianOfArray(array, 0, 100));
}
}
}
@@ -53,7 +53,7 @@ public class IdentityHashMapDemonstrator {
}
}
private static class Book {
static class Book {
String title;
int year;
@@ -1,8 +1,11 @@
package com.baeldung.map.identity;
import com.baeldung.map.identity.IdentityHashMapDemonstrator.Book;
import org.junit.jupiter.api.Test;
import java.util.IdentityHashMap;
import org.junit.jupiter.api.condition.EnabledForJreRange;
import org.junit.jupiter.api.condition.JRE;
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -18,4 +21,44 @@ public class IdentityHashMapDemonstratorUnitTest {
assertEquals("Fantasy", identityHashMap.get("genre"));
assertEquals("Drama", identityHashMap.get(newGenreKey));
}
@Test
@EnabledForJreRange(max = JRE.JAVA_19)
public void removeEntryComparingValueByEquality() {
Book book = new Book("A Passage to India", 1924);
IdentityHashMap<Book, String> identityHashMap = new IdentityHashMap<>(10);
identityHashMap.put(book, "A great work of fiction");
identityHashMap.remove(book, new String("A great work of fiction"));
assertEquals(null, identityHashMap.get(book));
}
@Test
@EnabledForJreRange(max = JRE.JAVA_19)
public void replaceEntryComparingValueByEquality() {
Book book = new Book("A Passage to India", 1924);
IdentityHashMap<Book, String> identityHashMap = new IdentityHashMap<>(10);
identityHashMap.put(book, "A great work of fiction");
identityHashMap.replace(book, new String("A great work of fiction"), "One of the greatest books");
assertEquals("One of the greatest books", identityHashMap.get(book));
}
@Test
@EnabledForJreRange(min = JRE.JAVA_20)
public void dontRemoveEntryComparingValueByEquality() {
Book book = new Book("A Passage to India", 1924);
IdentityHashMap<Book, String> identityHashMap = new IdentityHashMap<>(10);
identityHashMap.put(book, "A great work of fiction");
identityHashMap.remove(book, new String("A great work of fiction"));
assertEquals("A great work of fiction", identityHashMap.get(book));
}
@Test
@EnabledForJreRange(min = JRE.JAVA_20)
public void dontReplaceEntryComparingValueByEquality() {
Book book = new Book("A Passage to India", 1924);
IdentityHashMap<Book, String> identityHashMap = new IdentityHashMap<>(10);
identityHashMap.put(book, "A great work of fiction");
identityHashMap.replace(book, new String("A great work of fiction"), "One of the greatest books");
assertEquals("A great work of fiction", identityHashMap.get(book));
}
}
@@ -14,7 +14,7 @@ import org.apache.tika.mime.MimeTypeException;
import org.junit.Test;
import com.j256.simplemagic.ContentInfo;
import com.j256.simplemagic.ContentType;
public class ExtensionFromMimeTypeUnitTest {
private static final String IMAGE_JPEG_MIME_TYPE = "image/jpeg";
@@ -39,8 +39,7 @@ public class ExtensionFromMimeTypeUnitTest {
@Test
public void whenUsingMimetypesFileTypeMap_thenGetFileExtension() {
List<String> expectedExtensions = Arrays.asList("jpeg", "jpg", "jpe");
ContentInfo contentInfo = new ContentInfo("", IMAGE_JPEG_MIME_TYPE, "", true);
String[] detectedExtensions = contentInfo.getFileExtensions();
String[] detectedExtensions = ContentType.fromMimeType(IMAGE_JPEG_MIME_TYPE).getFileExtensions();
assertThat(detectedExtensions).containsExactlyElementsOf(expectedExtensions);
}
@@ -24,7 +24,7 @@ public class EchoIntegrationTest {
Executors.newSingleThreadExecutor()
.submit(() -> new EchoServer().start(port));
Thread.sleep(500);
Thread.sleep(2000);
}
private EchoClient client = new EchoClient();
@@ -27,7 +27,7 @@ public class GreetServerIntegrationTest {
Executors.newSingleThreadExecutor()
.submit(() -> new GreetServer().start(port));
Thread.sleep(500);
Thread.sleep(2000);
}
@Before
@@ -23,7 +23,7 @@ public class SocketEchoMultiIntegrationTest {
s.close();
Executors.newSingleThreadExecutor().submit(() -> new EchoMultiServer().start(port));
Thread.sleep(500);
Thread.sleep(2000);
}
@Test
+1
View File
@@ -65,6 +65,7 @@
<module>core-java-arrays-convert</module>
<module>core-java-arrays-operations-basic</module>
<module>core-java-arrays-operations-advanced</module>
<module>core-java-arrays-operations-advanced-2</module>
<module>core-java-booleans</module>
<module>core-java-char</module>
<module>core-java-collections</module>