Skip to content

Commit 2763ab2

Browse files
authored
Merge pull request #37 from panos-kakos/master
Update from master
2 parents 0e97e59 + 49dee80 commit 2763ab2

File tree

248 files changed

+3417
-401
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

248 files changed

+3417
-401
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.algorithms.stringsort;
2+
3+
import java.util.Arrays;
4+
import java.util.Comparator;
5+
6+
public class AlphanumericSort {
7+
8+
public static String lexicographicSort(String input) {
9+
char[] stringChars = input.toCharArray();
10+
Arrays.sort(stringChars);
11+
return new String(stringChars);
12+
}
13+
14+
public static String[] naturalAlphanumericSort(String[] arrayToSort) {
15+
Arrays.sort(arrayToSort, new Comparator<String>() {
16+
@Override
17+
public int compare(String s1, String s2) {
18+
return extractInt(s1) - extractInt(s2);
19+
}
20+
21+
private int extractInt(String str) {
22+
String num = str.replaceAll("\\D+", "");
23+
return num.isEmpty() ? 0 : Integer.parseInt(num);
24+
}
25+
});
26+
return arrayToSort;
27+
}
28+
29+
public static String[] naturalAlphanumericCaseInsensitiveSort(String[] arrayToSort) {
30+
Arrays.sort(arrayToSort, Comparator.comparing((String s) -> s.replaceAll("\\d", "").toLowerCase())
31+
.thenComparingInt(s -> {
32+
String num = s.replaceAll("\\D+", "");
33+
return num.isEmpty() ? 0 : Integer.parseInt(num);
34+
})
35+
.thenComparing(Comparator.naturalOrder()));
36+
return arrayToSort;
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.algorithms.stringsort;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.Arrays;
6+
7+
import static org.assertj.core.api.Assertions.assertThat;
8+
9+
class AlphanumericSortUnitTest {
10+
11+
@Test
12+
void givenAlphanumericString_whenLexicographicSort_thenSortLettersFirst() {
13+
String stringToSort = "C4B3A21";
14+
String sorted = AlphanumericSort.lexicographicSort(stringToSort);
15+
assertThat(sorted).isEqualTo("1234ABC");
16+
}
17+
18+
@Test
19+
void givenAlphanumericArrayOfStrings_whenAlphanumericSort_thenSortNaturalOrder() {
20+
String[] arrayToSort = {"file2", "file10", "file0", "file1", "file20"};
21+
String[] sorted = AlphanumericSort.naturalAlphanumericSort(arrayToSort);
22+
assertThat(Arrays.toString(sorted)).isEqualTo("[file0, file1, file2, file10, file20]");
23+
}
24+
25+
@Test
26+
void givenAlphanumericArrayOfStrings_whenAlphanumericCaseInsensitveSort_thenSortNaturalOrder() {
27+
String[] arrayToSort = {"a2", "A10", "b1", "B3", "A2"};
28+
String[] sorted = AlphanumericSort.naturalAlphanumericCaseInsensitiveSort(arrayToSort);
29+
assertThat(Arrays.toString(sorted)).isEqualTo("[A2, a2, A10, b1, B3]");
30+
}
31+
}

core-java-modules/core-java-11/src/test/java/com/baeldung/java11/httpclient/test/HttpClientUnitTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public void shouldFollowRedirectWhenSetToAlways() throws IOException, Interrupte
8282
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
8383
assertThat(response.request()
8484
.uri()
85-
.toString(), equalTo("https://stackoverflow.com/"));
85+
.toString(), equalTo("https://stackoverflow.com/questions"));
8686
}
8787

8888
@Test

core-java-modules/core-java-11/src/test/java/com/baeldung/java11/httpclient/test/HttpRequestUnitTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public void shouldReturnStatusOKWhenSendGetRequest() throws IOException, Interru
3838
@Test
3939
public void shouldUseHttp2WhenWebsiteUsesHttp2() throws IOException, InterruptedException, URISyntaxException {
4040
HttpRequest request = HttpRequest.newBuilder()
41-
.uri(new URI("https://stackoverflow.com"))
41+
.uri(new URI("https://stackoverflow.com/questions"))
4242
.version(HttpClient.Version.HTTP_2)
4343
.GET()
4444
.build();

core-java-modules/core-java-11/src/test/java/com/baeldung/java11/httpclient/test/HttpResponseUnitTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public void shouldReturnStatusOKWhenSendGetRequest() throws IOException, Interru
3434
}
3535

3636
@Test
37-
public void shouldResponseURIDifferentThanRequestUIRWhenRedirect() throws IOException, InterruptedException, URISyntaxException {
37+
public void shouldResponseURIDifferentThanRequestURIWhenRedirect() throws IOException, InterruptedException, URISyntaxException {
3838
HttpRequest request = HttpRequest.newBuilder()
3939
.uri(new URI("http://stackoverflow.com"))
4040
.version(HttpClient.Version.HTTP_2)
@@ -48,7 +48,7 @@ public void shouldResponseURIDifferentThanRequestUIRWhenRedirect() throws IOExce
4848
assertThat(request.uri()
4949
.toString(), equalTo("http://stackoverflow.com"));
5050
assertThat(response.uri()
51-
.toString(), equalTo("https://stackoverflow.com/"));
51+
.toString(), equalTo("https://stackoverflow.com/questions"));
5252
}
5353

5454
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## Core Java Arrays - Conversions
2+
3+
This module contains articles about arrays conversion in Java
4+
5+
## Relevant Articles
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<artifactId>core-java-arrays-convert-2</artifactId>
7+
<packaging>jar</packaging>
8+
<name>core-java-arrays-convert-2</name>
9+
10+
<parent>
11+
<artifactId>core-java-modules</artifactId>
12+
<groupId>com.baeldung.core-java-modules</groupId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
</parent>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>org.apache.commons</groupId>
19+
<artifactId>commons-lang3</artifactId>
20+
<version>${commons-lang3.version}</version>
21+
</dependency>
22+
</dependencies>
23+
24+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package com.baeldung.array.conversions.classcastexception;
2+
3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4+
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
import static org.junit.jupiter.api.Assertions.assertThrows;
6+
7+
import java.lang.reflect.Array;
8+
import java.time.Instant;
9+
import java.util.stream.Stream;
10+
11+
import org.junit.jupiter.api.Test;
12+
import org.slf4j.Logger;
13+
import org.slf4j.LoggerFactory;
14+
15+
public class ClassCastExceptionUnitTest {
16+
17+
private static Logger LOG = LoggerFactory.getLogger(ClassCastExceptionUnitTest.class);
18+
19+
Integer[] convertObjectArray() {
20+
Object[] objArray = new Object[3];
21+
objArray[0] = 1;
22+
objArray[1] = 2;
23+
objArray[2] = 3;
24+
return (Integer[]) objArray;
25+
}
26+
27+
Integer[] getIntegerArray() {
28+
Integer[] intArray = new Integer[3];
29+
intArray[0] = 1;
30+
intArray[1] = 2;
31+
intArray[2] = 3;
32+
return intArray;
33+
}
34+
35+
Integer[] objArrayToIntArray() {
36+
Object[] objArray = new Object[] { 1, 2, 3 };
37+
Integer[] intArray = new Integer[objArray.length];
38+
for (int i = 0; i < objArray.length; i++) {
39+
intArray[i] = (Integer) objArray[i];
40+
}
41+
return intArray;
42+
}
43+
44+
Integer[] objArrayToIntArrayByStream() {
45+
Object[] objArray = new Object[] { 1, 2, 3 };
46+
Integer[] intArray = Stream.of(objArray)
47+
.toArray(Integer[]::new);
48+
return intArray;
49+
}
50+
51+
<T> T[] convertFromObjectArray(Class<T> clazz, Object[] objArray) {
52+
T[] targetArray = (T[]) Array.newInstance(clazz, objArray.length);
53+
for (int i = 0; i < objArray.length; i++) {
54+
if (clazz.isInstance(objArray[i])) {
55+
targetArray[i] = clazz.cast(objArray[i]);
56+
} else {
57+
throw new ClassCastException("Element #" + i + ": Cannot cast " + objArray[i].getClass()
58+
.getName() + " to " + clazz.getName());
59+
}
60+
}
61+
return targetArray;
62+
}
63+
64+
@Test
65+
void whenCallingConvertObjectArray_thenClassCastException() {
66+
Exception ex = assertThrows(ClassCastException.class, this::convertObjectArray);
67+
LOG.error("The exception stacktrace:", ex);
68+
}
69+
70+
@Test
71+
void whenCallingIntegerArray_thenCorrect() {
72+
assertArrayEquals(new Integer[] { 1, 2, 3 }, getIntegerArray());
73+
}
74+
75+
@Test
76+
void whenCallingObjArrayToIntArray_thenCorrect() {
77+
assertArrayEquals(new Integer[] { 1, 2, 3 }, objArrayToIntArray());
78+
}
79+
80+
@Test
81+
void whenCallingObjArrayToIntArrayByStream_thenCorrect() {
82+
assertArrayEquals(new Integer[] { 1, 2, 3 }, objArrayToIntArrayByStream());
83+
}
84+
85+
@Test
86+
void whenCallingSingleElementArray_thenCorrect() {
87+
assertArrayEquals(new Integer[] { 1, 2, 3 }, convertFromObjectArray(Integer.class, new Object[] { 1, 2, 3 }));
88+
assertArrayEquals(new String[] { "I'm Kai", "I'm Liam", "I'm Kevin" },
89+
convertFromObjectArray(String.class, new Object[] { "I'm Kai", "I'm Liam", "I'm Kevin" }));
90+
//class cast ex:
91+
Exception ex = assertThrows(ClassCastException.class,
92+
() -> convertFromObjectArray(String.class, new Object[] { "I'm Kai", Instant.now(), "I'm Kevin" }));
93+
assertEquals("Element #1: Cannot cast java.time.Instant to java.lang.String", ex.getMessage());
94+
}
95+
96+
}

core-java-modules/core-java-collections-list-6/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@
1919
<artifactId>vavr</artifactId>
2020
<version>${vavr.version}</version>
2121
</dependency>
22+
<dependency>
23+
<groupId>org.junit.jupiter</groupId>
24+
<artifactId>junit-jupiter-api</artifactId>
25+
<version>5.8.2</version>
26+
<scope>test</scope>
27+
</dependency>
28+
<dependency>
29+
<groupId>org.junit.jupiter</groupId>
30+
<artifactId>junit-jupiter-engine</artifactId>
31+
<version>5.8.2</version>
32+
<scope>test</scope>
33+
</dependency>
2234
</dependencies>
2335

2436
<properties>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.baeldung.listtostring;
2+
3+
public class DoublyLinkedList {
4+
DoublyLinkedListNode head;
5+
DoublyLinkedListNode tail;
6+
7+
public void add(Integer data) {
8+
DoublyLinkedListNode newNode = new DoublyLinkedListNode(data);
9+
if (head == null) {
10+
head = newNode;
11+
tail = newNode;
12+
} else {
13+
tail.next = newNode;
14+
newNode.prev = tail;
15+
tail = newNode;
16+
}
17+
}
18+
19+
public static String customToString(DoublyLinkedList list) {
20+
StringBuilder sb = new StringBuilder();
21+
sb.append("Custom Doubly LinkedList: ");
22+
if (list.head == null) {
23+
sb.append("Empty List");
24+
} else {
25+
DoublyLinkedListNode currentNode = list.head;
26+
while (currentNode != null) {
27+
sb.append(currentNode.data).append(" - ");
28+
currentNode = currentNode.next;
29+
}
30+
sb.delete(sb.length() - 3, sb.length());
31+
}
32+
return sb.toString();
33+
}
34+
35+
public static void main(String[] args) {
36+
DoublyLinkedList idsList = new DoublyLinkedList();
37+
idsList.add(101);
38+
idsList.add(102);
39+
idsList.add(103);
40+
41+
System.out.println(customToString(idsList));
42+
43+
DoublyLinkedList emptyList = new DoublyLinkedList();
44+
System.out.println(customToString(emptyList));
45+
}
46+
}

0 commit comments

Comments
 (0)