Merge branch 'master' into BAEL-10229
This commit is contained in:
@@ -1,3 +1,7 @@
|
||||
## Relevant articles:
|
||||
## Akka HTTP
|
||||
|
||||
This module contains articles about Akka HTTP.
|
||||
|
||||
### Relevant articles:
|
||||
|
||||
- [Introduction to Akka HTTP](https://www.baeldung.com/akka-http)
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
## Akka Streams
|
||||
|
||||
This module contains articles about Akka Streams.
|
||||
|
||||
### Relevant articles
|
||||
|
||||
- [Guide to Akka Streams](https://www.baeldung.com/akka-streams)
|
||||
|
||||
@@ -1,4 +1,8 @@
|
||||
## Relevant articles:
|
||||
## Genetic Algorithms
|
||||
|
||||
This module contains articles about genetic algorithms.
|
||||
|
||||
### Relevant articles:
|
||||
|
||||
- [Introduction to Jenetics Library](https://www.baeldung.com/jenetics)
|
||||
- [Ant Colony Optimization](https://www.baeldung.com/java-ant-colony-optimization)
|
||||
|
||||
@@ -1,4 +1,9 @@
|
||||
## Relevant articles:
|
||||
## Algorithms - Miscellaneous
|
||||
|
||||
This module contains articles about algorithms. Some classes of algorithms, e.g., [sorting](/../algorithms-sorting) and
|
||||
[genetic algorithms](/../algorithms-genetic), have their own dedicated modules.
|
||||
|
||||
### Relevant articles:
|
||||
|
||||
- [Validating Input With Finite Automata in Java](https://www.baeldung.com/java-finite-automata)
|
||||
- [Example of Hill Climbing Algorithm](https://www.baeldung.com/java-hill-climbing-algorithm)
|
||||
@@ -6,4 +11,5 @@
|
||||
- [Binary Search Algorithm in Java](https://www.baeldung.com/java-binary-search)
|
||||
- [Introduction to Minimax Algorithm](https://www.baeldung.com/java-minimax-algorithm)
|
||||
- [How to Calculate Levenshtein Distance in Java?](https://www.baeldung.com/java-levenshtein-distance)
|
||||
- [How to Find the Kth Largest Element in Java](https://www.baeldung.com/java-kth-largest-element)
|
||||
- [How to Find the Kth Largest Element in Java](https://www.baeldung.com/java-kth-largest-element)
|
||||
- More articles: [[next -->]](/../algorithms-miscellaneous-2)
|
||||
|
||||
@@ -1,4 +1,9 @@
|
||||
## Relevant articles:
|
||||
## Algorithms - Miscellaneous
|
||||
|
||||
This module contains articles about algorithms. Some classes of algorithms, e.g., [sorting](/../algorithms-sorting) and
|
||||
[genetic algorithms](/../algorithms-genetic), have their own dedicated modules.
|
||||
|
||||
### Relevant articles:
|
||||
|
||||
- [Dijkstra Shortest Path Algorithm in Java](https://www.baeldung.com/java-dijkstra)
|
||||
- [Introduction to Cobertura](https://www.baeldung.com/cobertura)
|
||||
@@ -8,3 +13,4 @@
|
||||
- [Create a Sudoku Solver in Java](https://www.baeldung.com/java-sudoku)
|
||||
- [Displaying Money Amounts in Words](https://www.baeldung.com/java-money-into-words)
|
||||
- [A Collaborative Filtering Recommendation System in Java](https://www.baeldung.com/java-collaborative-filtering-recommendations)
|
||||
- More articles: [[<-- prev]](/../algorithms-miscellaneous-1) [[next -->]](/../algorithms-miscellaneous-3)
|
||||
|
||||
@@ -1,4 +1,9 @@
|
||||
## Relevant articles:
|
||||
## Algorithms - Miscellaneous
|
||||
|
||||
This module contains articles about algorithms. Some classes of algorithms, e.g., [sorting](/../algorithms-sorting) and
|
||||
[genetic algorithms](/../algorithms-genetic), have their own dedicated modules.
|
||||
|
||||
### Relevant articles:
|
||||
|
||||
- [Multi-Swarm Optimization Algorithm in Java](https://www.baeldung.com/java-multi-swarm-algorithm)
|
||||
- [String Search Algorithms for Large Texts](https://www.baeldung.com/java-full-text-search-algorithms)
|
||||
@@ -6,4 +11,5 @@
|
||||
- [Find the Middle Element of a Linked List](https://www.baeldung.com/java-linked-list-middle-element)
|
||||
- [Find Substrings That Are Palindromes in Java](https://www.baeldung.com/java-palindrome-substrings)
|
||||
- [Find the Longest Substring without Repeating Characters](https://www.baeldung.com/java-longest-substring-without-repeated-characters)
|
||||
- [Permutations of an Array in Java](https://www.baeldung.com/java-array-permutations)
|
||||
- [Permutations of an Array in Java](https://www.baeldung.com/java-array-permutations)
|
||||
- More articles: [[<-- prev]](/../algorithms-miscellaneous-3) [[next -->]](/../algorithms-miscellaneous-5)
|
||||
|
||||
@@ -1,5 +1,11 @@
|
||||
## Relevant articles:
|
||||
## Algorithms - Miscellaneous
|
||||
|
||||
This module contains articles about algorithms. Some classes of algorithms, e.g., [sorting](/../algorithms-sorting) and
|
||||
[genetic algorithms](/../algorithms-genetic), have their own dedicated modules.
|
||||
|
||||
### Relevant articles:
|
||||
|
||||
- [Converting Between Byte Arrays and Hexadecimal Strings in Java](https://www.baeldung.com/java-byte-arrays-hex-strings)
|
||||
- [Reversing a Binary Tree in Java](https://www.baeldung.com/java-reversing-a-binary-tree)
|
||||
- [Find If Two Numbers Are Relatively Prime in Java](https://www.baeldung.com/java-two-relatively-prime-numbers)
|
||||
- [Find If Two Numbers Are Relatively Prime in Java](https://www.baeldung.com/java-two-relatively-prime-numbers)
|
||||
- More articles: [[<-- prev]](/../algorithms-miscellaneous-4)
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.baeldung.algorithms.knapsack;
|
||||
|
||||
public class Knapsack {
|
||||
|
||||
public int knapsackRec(int[] w, int[] v, int n, int W) {
|
||||
if (n <= 0) {
|
||||
return 0;
|
||||
} else if (w[n - 1] > W) {
|
||||
return knapsackRec(w, v, n - 1, W);
|
||||
} else {
|
||||
return Math.max(knapsackRec(w, v, n - 1, W), v[n - 1] + knapsackRec(w, v, n - 1, W - w[n - 1]));
|
||||
}
|
||||
}
|
||||
|
||||
public int knapsackDP(int[] w, int[] v, int n, int W) {
|
||||
if (n <= 0 || W <= 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int[][] m = new int[n + 1][W + 1];
|
||||
for (int j = 0; j <= W; j++) {
|
||||
m[0][j] = 0;
|
||||
}
|
||||
|
||||
for (int i = 1; i <= n; i++) {
|
||||
for (int j = 1; j <= W; j++) {
|
||||
if (w[i - 1] > j) {
|
||||
m[i][j] = m[i - 1][j];
|
||||
} else {
|
||||
m[i][j] = Math.max(m[i - 1][j], m[i - 1][j - w[i - 1]] + v[i - 1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return m[n][W];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.baeldung.algorithms.knapsack;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class KnapsackUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenWeightsandValues_whenCalculateMax_thenOutputCorrectResult() {
|
||||
final int[] w = new int[] { 23, 26, 20, 18, 32, 27, 29, 26, 30, 27 };
|
||||
final int[] v = new int[] { 505, 352, 458, 220, 354, 414, 498, 545, 473, 543 };
|
||||
final int n = 10;
|
||||
final int W = 67;
|
||||
final Knapsack knapsack = new Knapsack();
|
||||
|
||||
assertEquals(1270, knapsack.knapsackRec(w, v, n, W));
|
||||
assertEquals(1270, knapsack.knapsackDP(w, v, n, W));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenZeroItems_whenCalculateMax_thenOutputZero() {
|
||||
final int[] w = new int[] {};
|
||||
final int[] v = new int[] {};
|
||||
final int n = 0;
|
||||
final int W = 67;
|
||||
final Knapsack knapsack = new Knapsack();
|
||||
|
||||
assertEquals(0, knapsack.knapsackRec(w, v, n, W));
|
||||
assertEquals(0, knapsack.knapsackDP(w, v, n, W));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenZeroWeightLimit_whenCalculateMax_thenOutputZero() {
|
||||
final int[] w = new int[] { 23, 26, 20, 18, 32, 27, 29, 26, 30, 27 };
|
||||
final int[] v = new int[] { 505, 352, 458, 220, 354, 414, 498, 545, 473, 543 };
|
||||
final int n = 10;
|
||||
final int W = 0;
|
||||
final Knapsack knapsack = new Knapsack();
|
||||
|
||||
assertEquals(0, knapsack.knapsackRec(w, v, n, W));
|
||||
assertEquals(0, knapsack.knapsackDP(w, v, n, W));
|
||||
}
|
||||
}
|
||||
@@ -16,3 +16,4 @@ This module contains articles about sorting algorithms.
|
||||
- [Selection Sort in Java](https://www.baeldung.com/java-selection-sort)
|
||||
- [Sorting Strings by Contained Numbers in Java](https://www.baeldung.com/java-sort-strings-contained-numbers)
|
||||
- [Radix Sort in Java](https://www.baeldung.com/java-radix-sort)
|
||||
- [Sorting a String Alphabetically in Java](https://www.baeldung.com/java-sort-string-alphabetically)
|
||||
|
||||
@@ -28,6 +28,12 @@
|
||||
<version>${lombok.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-api</artifactId>
|
||||
<version>${junit-jupiter-api.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
@@ -52,6 +58,7 @@
|
||||
<commons-math3.version>3.6.1</commons-math3.version>
|
||||
<org.assertj.core.version>3.9.0</org.assertj.core.version>
|
||||
<commons-codec.version>1.11</commons-codec.version>
|
||||
<junit-jupiter-api.version>5.3.1</junit-jupiter-api.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.string.sorting;
|
||||
package com.baeldung.algorithms.stringsort;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
package com.baeldung.string.sorting;
|
||||
package com.baeldung.algorithms.stringsort;
|
||||
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import com.baeldung.string.sorting.AnagramValidator;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class AnagramValidatorUnitTest {
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
package com.baeldung.string.sorting;
|
||||
|
||||
import java.util.Arrays;
|
||||
package com.baeldung.algorithms.stringsort;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class SortStringUnitTest {
|
||||
@@ -1,12 +1,12 @@
|
||||
package com.baeldung.java_8_features.groupingby;
|
||||
|
||||
public class Tuple {
|
||||
import java.util.Objects;
|
||||
|
||||
public class Tuple {
|
||||
private final BlogPostType type;
|
||||
private final String author;
|
||||
|
||||
private BlogPostType type;
|
||||
private String author;
|
||||
|
||||
public Tuple(BlogPostType type, String author) {
|
||||
super();
|
||||
this.type = type;
|
||||
this.author = author;
|
||||
}
|
||||
@@ -15,20 +15,27 @@ public class Tuple {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(BlogPostType type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public void setAuthor(String author) {
|
||||
this.author = author;
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o)
|
||||
return true;
|
||||
if (o == null || getClass() != o.getClass())
|
||||
return false;
|
||||
Tuple tuple = (Tuple) o;
|
||||
return type == tuple.type && author.equals(tuple.author);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(type, author);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Tuple [type=" + type + ", author=" + author + ", getType()=" + getType() + ", getAuthor()=" + getAuthor() + ", getClass()=" + getClass() + ", hashCode()=" + hashCode() + ", toString()=" + super.toString() + "]";
|
||||
return "Tuple{" + "type=" + type + ", author='" + author + '\'' + '}';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +1,32 @@
|
||||
package com.baeldung.java_8_features.groupingby;
|
||||
|
||||
import com.baeldung.java_8_features.groupingby.BlogPost;
|
||||
import com.baeldung.java_8_features.groupingby.BlogPostType;
|
||||
import org.junit.Test;
|
||||
import static java.util.Comparator.comparingInt;
|
||||
import static java.util.stream.Collectors.averagingInt;
|
||||
import static java.util.stream.Collectors.counting;
|
||||
import static java.util.stream.Collectors.groupingBy;
|
||||
import static java.util.stream.Collectors.groupingByConcurrent;
|
||||
import static java.util.stream.Collectors.joining;
|
||||
import static java.util.stream.Collectors.mapping;
|
||||
import static java.util.stream.Collectors.maxBy;
|
||||
import static java.util.stream.Collectors.summarizingInt;
|
||||
import static java.util.stream.Collectors.summingInt;
|
||||
import static java.util.stream.Collectors.toList;
|
||||
import static java.util.stream.Collectors.toSet;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.Arrays;
|
||||
import java.util.EnumMap;
|
||||
import java.util.IntSummaryStatistics;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
|
||||
import static java.util.Comparator.comparingInt;
|
||||
import static java.util.stream.Collectors.*;
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Test;
|
||||
|
||||
public class Java8GroupingByCollectorUnitTest {
|
||||
|
||||
@@ -180,4 +197,19 @@ public class Java8GroupingByCollectorUnitTest {
|
||||
assertEquals(15, newsLikeStatistics.getMin());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAListOfPosts_whenGroupedByComplexMapKeyType_thenGetAMapBetweenTupleAndList() {
|
||||
Map<Tuple, List<BlogPost>> postsPerTypeAndAuthor = posts.stream()
|
||||
.collect(groupingBy(post -> new Tuple(post.getType(), post.getAuthor())));
|
||||
|
||||
List<BlogPost> result = postsPerTypeAndAuthor.get(new Tuple(BlogPostType.GUIDE, "Author 1"));
|
||||
|
||||
assertThat(result.size()).isEqualTo(1);
|
||||
|
||||
BlogPost blogPost = result.get(0);
|
||||
|
||||
assertThat(blogPost.getTitle()).isEqualTo("Programming guide");
|
||||
assertThat(blogPost.getType()).isEqualTo(BlogPostType.GUIDE);
|
||||
assertThat(blogPost.getAuthor()).isEqualTo("Author 1");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -192,15 +192,48 @@ public class ArrayOperations {
|
||||
return array[new Random().nextInt(array.length)];
|
||||
}
|
||||
|
||||
public static Integer[] intersectionSimple(final Integer[] a, final Integer[] b){
|
||||
return Stream.of(a).filter(Arrays.asList(b)::contains).toArray(Integer[]::new);
|
||||
public static Integer[] intersectionSimple(final Integer[] a, final Integer[] b) {
|
||||
return Stream.of(a)
|
||||
.filter(Arrays.asList(b)::contains)
|
||||
.toArray(Integer[]::new);
|
||||
}
|
||||
|
||||
public static Integer[] intersectionSet(final Integer[] a, final Integer[] b){
|
||||
return Stream.of(a).filter(Arrays.asList(b)::contains).distinct().toArray(Integer[]::new);
|
||||
public static Integer[] intersectionSet(final Integer[] a, final Integer[] b) {
|
||||
return Stream.of(a)
|
||||
.filter(Arrays.asList(b)::contains)
|
||||
.distinct()
|
||||
.toArray(Integer[]::new);
|
||||
}
|
||||
|
||||
public static Integer[] intersectionMultiSet(final Integer[] a, final Integer[] b){
|
||||
return Stream.of(a).filter(new LinkedList<>(Arrays.asList(b))::remove).toArray(Integer[]::new);
|
||||
public static Integer[] intersectionMultiSet(final Integer[] a, final Integer[] b) {
|
||||
return Stream.of(a)
|
||||
.filter(new LinkedList<>(Arrays.asList(b))::remove)
|
||||
.toArray(Integer[]::new);
|
||||
}
|
||||
|
||||
public static Integer[] addElementUsingPureJava(Integer[] srcArray, int elementToAdd) {
|
||||
Integer[] destArray = new Integer[srcArray.length + 1];
|
||||
|
||||
for (int i = 0; i < srcArray.length; i++) {
|
||||
destArray[i] = srcArray[i];
|
||||
}
|
||||
|
||||
destArray[destArray.length - 1] = elementToAdd;
|
||||
return destArray;
|
||||
}
|
||||
|
||||
public static int[] insertAnElementAtAGivenIndex(final int[] srcArray, int index, int newElement) {
|
||||
int[] destArray = new int[srcArray.length + 1];
|
||||
int j = 0;
|
||||
for (int i = 0; i < destArray.length - 1; i++) {
|
||||
|
||||
if (i == index) {
|
||||
destArray[i] = newElement;
|
||||
} else {
|
||||
destArray[i] = srcArray[j];
|
||||
j++;
|
||||
}
|
||||
}
|
||||
return destArray;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.arraylist.operations;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class ArrayListOperations {
|
||||
|
||||
public static Integer getAnIntegerElement(ArrayList<Integer> anArrayList, int index) {
|
||||
return anArrayList.get(index);
|
||||
}
|
||||
|
||||
public static void modifyAnIntegerElement(ArrayList<Integer> anArrayList, int index, Integer newElement) {
|
||||
anArrayList.set(index, newElement);
|
||||
}
|
||||
|
||||
public static void appendAnIntegerElement(ArrayList<Integer> anArrayList, Integer newElement) {
|
||||
anArrayList.add(newElement);
|
||||
}
|
||||
|
||||
public static void insertAnIntegerElementAtIndex(ArrayList<Integer> anArrayList, int index, Integer newElement) {
|
||||
anArrayList.add(index, newElement);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,12 +1,11 @@
|
||||
package com.baeldung.array.operations;
|
||||
|
||||
import java.util.Arrays;
|
||||
import org.assertj.core.api.Condition;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
|
||||
public class ArrayOperationsUnitTest {
|
||||
|
||||
@@ -262,8 +261,7 @@ public class ArrayOperationsUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenMapIntArrayToString_thenReturnArray() {
|
||||
String[] expectedArray = new String[] { "Value: 3", "Value: 5", "Value: 2", "Value: 5", "Value: 14",
|
||||
"Value: 4" };
|
||||
String[] expectedArray = new String[] { "Value: 3", "Value: 5", "Value: 2", "Value: 5", "Value: 14", "Value: 4" };
|
||||
String[] output = ArrayOperations.mapIntArrayToString(defaultIntArray);
|
||||
|
||||
assertThat(output).containsExactly(expectedArray);
|
||||
@@ -313,13 +311,10 @@ public class ArrayOperationsUnitTest {
|
||||
int[] output5 = ArrayOperations.shuffleIntArray(defaultIntArray);
|
||||
int[] output6 = ArrayOperations.shuffleIntArray(defaultIntArray);
|
||||
|
||||
Condition<int[]> atLeastOneArraysIsNotEqual = new Condition<int[]>(
|
||||
"at least one output should be different (order-wise)") {
|
||||
Condition<int[]> atLeastOneArraysIsNotEqual = new Condition<int[]>("at least one output should be different (order-wise)") {
|
||||
@Override
|
||||
public boolean matches(int[] value) {
|
||||
return !Arrays.equals(value, output) || !Arrays.equals(value, output2) || !Arrays.equals(value, output3)
|
||||
|| !Arrays.equals(value, output4) || !Arrays.equals(value, output5)
|
||||
|| !Arrays.equals(value, output6);
|
||||
return !Arrays.equals(value, output) || !Arrays.equals(value, output2) || !Arrays.equals(value, output3) || !Arrays.equals(value, output4) || !Arrays.equals(value, output5) || !Arrays.equals(value, output6);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -335,13 +330,10 @@ public class ArrayOperationsUnitTest {
|
||||
Integer[] output5 = ArrayOperations.shuffleObjectArray(defaultObjectArray);
|
||||
Integer[] output6 = ArrayOperations.shuffleObjectArray(defaultObjectArray);
|
||||
|
||||
Condition<Integer[]> atLeastOneArraysIsNotEqual = new Condition<Integer[]>(
|
||||
"at least one output should be different (order-wise)") {
|
||||
Condition<Integer[]> atLeastOneArraysIsNotEqual = new Condition<Integer[]>("at least one output should be different (order-wise)") {
|
||||
@Override
|
||||
public boolean matches(Integer[] value) {
|
||||
return !Arrays.equals(value, output) || !Arrays.equals(value, output2) || !Arrays.equals(value, output3)
|
||||
|| !Arrays.equals(value, output4) || !Arrays.equals(value, output5)
|
||||
|| !Arrays.equals(value, output6);
|
||||
return !Arrays.equals(value, output) || !Arrays.equals(value, output2) || !Arrays.equals(value, output3) || !Arrays.equals(value, output4) || !Arrays.equals(value, output5) || !Arrays.equals(value, output6);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -362,4 +354,27 @@ public class ArrayOperationsUnitTest {
|
||||
|
||||
assertThat(defaultObjectArray).contains(output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSourceArrayAndElement_whenAddElementUsingPureJavaIsInvoked_thenNewElementMustBeAdded() {
|
||||
Integer[] sourceArray = { 1, 2, 3, 4 };
|
||||
int elementToAdd = 5;
|
||||
|
||||
Integer[] destArray = ArrayOperations.addElementUsingPureJava(sourceArray, elementToAdd);
|
||||
|
||||
Integer[] expectedArray = { 1, 2, 3, 4, 5 };
|
||||
assertArrayEquals(expectedArray, destArray);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenInsertAnElementAtAGivenIndexCalled_thenShiftTheFollowingElementsAndInsertTheElementInArray() {
|
||||
int[] expectedArray = { 1, 4, 2, 3, 0 };
|
||||
int[] anArray = new int[4];
|
||||
anArray[0] = 1;
|
||||
anArray[1] = 2;
|
||||
anArray[2] = 3;
|
||||
int[] outputArray = ArrayOperations.insertAnElementAtAGivenIndex(anArray, 1, 4);
|
||||
|
||||
assertThat(outputArray).containsExactly(expectedArray);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
package com.baeldung.arraylist.operations;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class ArrayListOperationsUnitTest {
|
||||
|
||||
private ArrayList<Integer> anArrayList;
|
||||
|
||||
@BeforeEach
|
||||
public void setupDefaults() {
|
||||
anArrayList = new ArrayList<>();
|
||||
anArrayList.add(2);
|
||||
anArrayList.add(3);
|
||||
anArrayList.add(4);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGetAnIntegerElementCalled_thenReturnTheIntegerElement() {
|
||||
Integer output = ArrayListOperations.getAnIntegerElement(anArrayList, 1);
|
||||
|
||||
assertThat(output).isEqualTo(3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenModifyAnIntegerElementCalled_thenModifyTheIntegerElement() {
|
||||
ArrayListOperations.modifyAnIntegerElement(anArrayList, 2, 5);
|
||||
Integer output = ArrayListOperations.getAnIntegerElement(anArrayList, 2);
|
||||
|
||||
assertThat(output).isEqualTo(5);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAppendAnIntegerElementCalled_thenTheIntegerElementIsAppendedToArrayList() {
|
||||
ArrayListOperations.appendAnIntegerElement(anArrayList, 6);
|
||||
Integer output = ArrayListOperations.getAnIntegerElement(anArrayList, anArrayList.size() - 1);
|
||||
|
||||
assertThat(output).isEqualTo(6);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenInsertAnIntegerAtIndexCalled_thenTheIntegerElementIsInseredToArrayList() {
|
||||
ArrayListOperations.insertAnIntegerElementAtIndex(anArrayList, 1, 10);
|
||||
Integer output = ArrayListOperations.getAnIntegerElement(anArrayList, 1);
|
||||
|
||||
assertThat(output).isEqualTo(10);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.baeldung.list;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Demo different approaches to get count of duplicated elements in an
|
||||
* arrayList
|
||||
*/
|
||||
public class DuplicatesCounter {
|
||||
|
||||
public static <T> Map<T, Long> countByClassicalLoop(List<T> inputList) {
|
||||
Map<T, Long> resultMap = new HashMap<>();
|
||||
for (T element : inputList) {
|
||||
if (resultMap.containsKey(element)) {
|
||||
resultMap.put(element, resultMap.get(element) + 1L);
|
||||
} else {
|
||||
resultMap.put(element, 1L);
|
||||
}
|
||||
}
|
||||
return resultMap;
|
||||
}
|
||||
|
||||
public static <T> Map<T, Long> countByClassicalLoopWithMapCompute(List<T> inputList) {
|
||||
Map<T, Long> resultMap = new HashMap<>();
|
||||
for (T element : inputList) {
|
||||
resultMap.compute(element, (k, v) -> v == null ? 1 : v + 1);
|
||||
}
|
||||
return resultMap;
|
||||
}
|
||||
|
||||
public static <T> Map<T, Long> countByStreamToMap(List<T> inputList) {
|
||||
return inputList.stream().collect(Collectors.toMap(Function.identity(), v -> 1L, Long::sum));
|
||||
}
|
||||
|
||||
public static <T> Map<T, Long> countByStreamGroupBy(List<T> inputList) {
|
||||
return inputList.stream().collect(Collectors.groupingBy(k -> k, Collectors.counting()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.baeldung.list;
|
||||
|
||||
import org.assertj.core.util.Lists;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.data.MapEntry.entry;
|
||||
|
||||
class DuplicatesCounterUnitTest {
|
||||
|
||||
|
||||
private static List<String> INPUT_LIST = Lists.list(
|
||||
"expect1",
|
||||
"expect2", "expect2",
|
||||
"expect3", "expect3", "expect3",
|
||||
"expect4", "expect4", "expect4", "expect4");
|
||||
|
||||
@Test
|
||||
void givenInput_whenCountByClassicalLoop_thenGetResultMap() {
|
||||
Map<String, Long> result = DuplicatesCounter.countByClassicalLoop(INPUT_LIST);
|
||||
verifyResult(result);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void givenInput_whenCountByClassicalLoopWithMapCompute_thenGetResultMap() {
|
||||
Map<String, Long> result = DuplicatesCounter.countByClassicalLoopWithMapCompute(INPUT_LIST);
|
||||
verifyResult(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenInput_whenCountByStreamToMap_thenGetResultMap() {
|
||||
Map<String, Long> result = DuplicatesCounter.countByStreamToMap(INPUT_LIST);
|
||||
verifyResult(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenInput_whenCountByStreamGroupBy_thenGetResultMap() {
|
||||
Map<String, Long> result = DuplicatesCounter.countByStreamGroupBy(INPUT_LIST);
|
||||
verifyResult(result);
|
||||
}
|
||||
|
||||
private void verifyResult(Map<String, Long> resultMap) {
|
||||
assertThat(resultMap)
|
||||
.isNotEmpty().hasSize(4)
|
||||
.containsExactly(
|
||||
entry("expect1", 1L),
|
||||
entry("expect2", 2L),
|
||||
entry("expect3", 3L),
|
||||
entry("expect4", 4L));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
## Java Date/time conversion Cookbooks and Examples
|
||||
|
||||
This module contains articles about converting between Java date and time objects.
|
||||
|
||||
### Relevant Articles:
|
||||
- [Converting Between LocalDate and XMLGregorianCalendar](https://www.baeldung.com/java-localdate-to-xmlgregoriancalendar)
|
||||
- [Convert Time to Milliseconds in Java](https://www.baeldung.com/java-time-milliseconds)
|
||||
- [Convert Date to LocalDate or LocalDateTime and Back](http://www.baeldung.com/java-date-to-localdate-and-localdatetime)
|
||||
- [Convert Between java.time.Instant and java.sql.Timestamp](https://www.baeldung.com/java-time-instant-to-java-sql-timestamp)
|
||||
70
core-java-modules/core-java-datetime-conversion/pom.xml
Normal file
70
core-java-modules/core-java-datetime-conversion/pom.xml
Normal file
@@ -0,0 +1,70 @@
|
||||
<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-datetime-conversion</artifactId>
|
||||
<version>${project.parent.version}</version>
|
||||
<name>core-java-datetime-conversion</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-java</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>joda-time</groupId>
|
||||
<artifactId>joda-time</artifactId>
|
||||
<version>${joda-time.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>log4j</groupId>
|
||||
<artifactId>log4j</artifactId>
|
||||
<version>${log4j.version}</version>
|
||||
</dependency>
|
||||
<!-- test scoped -->
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>core-java-datetime-conversion</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<source>${maven.compiler.source}</source>
|
||||
<target>${maven.compiler.target}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<joda-time.version>2.10</joda-time.version>
|
||||
<!-- testing -->
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
<maven.compiler.source>1.9</maven.compiler.source>
|
||||
<maven.compiler.target>1.9</maven.compiler.target>
|
||||
</properties>
|
||||
</project>
|
||||
@@ -1,15 +1,14 @@
|
||||
package com.baeldung.datetime;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneId;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import org.junit.Test;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class ConvertInstantToTimestampUnitTest {
|
||||
|
||||
13
core-java-modules/core-java-datetime-string/README.md
Normal file
13
core-java-modules/core-java-datetime-string/README.md
Normal file
@@ -0,0 +1,13 @@
|
||||
## Java Dates Parsing and Formatting Cookbooks and Examples
|
||||
|
||||
This module contains articles about parsing and formatting Java date and time objects.
|
||||
|
||||
### Relevant Articles:
|
||||
- [Check If a String Is a Valid Date in Java](https://www.baeldung.com/java-string-valid-date)
|
||||
- [RegEx for matching Date Pattern in Java](http://www.baeldung.com/java-date-regular-expressions)
|
||||
- [Guide to DateTimeFormatter](https://www.baeldung.com/java-datetimeformatter)
|
||||
- [Format ZonedDateTime to String](https://www.baeldung.com/java-format-zoned-datetime-string)
|
||||
- [A Guide to SimpleDateFormat](https://www.baeldung.com/java-simple-date-format)
|
||||
- [Display All Time Zones With GMT And UTC in Java](http://www.baeldung.com/java-time-zones)
|
||||
- [Convert between String and Timestamp](https://www.baeldung.com/java-string-to-timestamp)
|
||||
- [Convert String to Date in Java](http://www.baeldung.com/java-string-to-date)
|
||||
@@ -1,39 +1,35 @@
|
||||
<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>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>java-dates-2</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<artifactId>core-java-datetime-string</artifactId>
|
||||
<version>${project.parent.version}</version>
|
||||
<name>core-java-datetime-string</name>
|
||||
<packaging>jar</packaging>
|
||||
<name>java-dates-2</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-java</relativePath>
|
||||
<relativePath>../../parent-java</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>joda-time</groupId>
|
||||
<artifactId>joda-time</artifactId>
|
||||
<version>${joda-time.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/commons-validator/commons-validator -->
|
||||
<dependency>
|
||||
<groupId>commons-validator</groupId>
|
||||
<artifactId>commons-validator</artifactId>
|
||||
<version>${commons-validator.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>log4j</groupId>
|
||||
<artifactId>log4j</artifactId>
|
||||
<version>${log4j.version}</version>
|
||||
</dependency>
|
||||
<!-- test scoped -->
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
@@ -41,10 +37,21 @@
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>joda-time</groupId>
|
||||
<artifactId>joda-time</artifactId>
|
||||
<version>${joda-time.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.darwinsys</groupId>
|
||||
<artifactId>hirondelle-date4j</artifactId>
|
||||
<version>RELEASE</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>java-dates-2</finalName>
|
||||
<finalName>core-java-datetime-string</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
@@ -66,12 +73,11 @@
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<commons-validator.version>1.6</commons-validator.version>
|
||||
<joda-time.version>2.10</joda-time.version>
|
||||
<!-- testing -->
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
<joda-time.version>2.10</joda-time.version>
|
||||
<commons-lang3.version>3.9</commons-lang3.version>
|
||||
<commons-validator.version>1.6</commons-validator.version>
|
||||
<maven.compiler.source>1.9</maven.compiler.source>
|
||||
<maven.compiler.source>1.9</maven.compiler.source>
|
||||
<maven.compiler.target>1.9</maven.compiler.target>
|
||||
</properties>
|
||||
</project>
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.zoneddatetime;
|
||||
package com.baeldung.zonedatetime;
|
||||
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.ZoneOffset;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.zoneddatetime;
|
||||
package com.baeldung.zonedatetime;
|
||||
|
||||
import java.time.OffsetTime;
|
||||
import java.time.ZoneOffset;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.zoneddatetime;
|
||||
package com.baeldung.zonedatetime;
|
||||
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.zoneddatetime;
|
||||
package com.baeldung.zonedatetime;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.zoneddatetime;
|
||||
package com.baeldung.zonedatetime;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.zoneddatetime;
|
||||
package com.baeldung.zonedatetime;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.zoneddatetime;
|
||||
package com.baeldung.zonedatetime;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
=========
|
||||
|
||||
## Core Java 8 Cookbooks and Examples
|
||||
|
||||
### Relevant Articles:
|
||||
- [Set the Time Zone of a Date in Java](https://www.baeldung.com/java-set-date-time-zone)
|
||||
- [Overriding System Time for Testing in Java](https://www.baeldung.com/java-override-system-time)
|
||||
@@ -1,82 +0,0 @@
|
||||
<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-datetime</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-datetime</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-java</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<!-- test scoped -->
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>joda-time</groupId>
|
||||
<artifactId>joda-time</artifactId>
|
||||
<version>${joda.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.powermock</groupId>
|
||||
<artifactId>powermock-module-junit4</artifactId>
|
||||
<version>${powermock.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.powermock</groupId>
|
||||
<artifactId>powermock-api-mockito2</artifactId>
|
||||
<version>${powermock.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jmockit</groupId>
|
||||
<artifactId>jmockit</artifactId>
|
||||
<version>${jmockit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>core-java-datetime</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${maven-surefire-plugin.version}</version>
|
||||
<configuration>
|
||||
<argLine>
|
||||
-javaagent:${settings.localRepository}/org/jmockit/jmockit/${jmockit.version}/jmockit-${jmockit.version}.jar
|
||||
</argLine>
|
||||
<disableXmlReport>true</disableXmlReport>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<!-- util -->
|
||||
<joda.version>2.10</joda.version>
|
||||
<!-- testing -->
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
<powermock.version>2.0.0-RC.4</powermock.version>
|
||||
<jmockit.version>1.44</jmockit.version>
|
||||
<!-- plugins -->
|
||||
<maven-surefire-plugin.version>2.22.1</maven-surefire-plugin.version>
|
||||
</properties>
|
||||
</project>
|
||||
@@ -3,7 +3,6 @@
|
||||
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>
|
||||
|
||||
<groupId>com.baeldung.consumermodule</groupId>
|
||||
<artifactId>consumermodule</artifactId>
|
||||
<version>1.0</version>
|
||||
|
||||
@@ -3,16 +3,16 @@
|
||||
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>
|
||||
<groupId>com.baeldung.servicemodule</groupId>
|
||||
<artifactId>servicemodule</artifactId>
|
||||
<version>1.0</version>
|
||||
|
||||
<parent>
|
||||
<artifactId>>com.baeldung.decoupling-pattern2</artifactId>
|
||||
<groupId>decoupling-pattern2</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<groupId>com.baeldung.servicemodule</groupId>
|
||||
<artifactId>servicemodule</artifactId>
|
||||
<version>1.0</version>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
|
||||
@@ -28,12 +28,6 @@
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<!-- testing -->
|
||||
<assertj-core.version>3.10.0</assertj-core.version>
|
||||
<equalsverifier.version>3.0.3</equalsverifier.version>
|
||||
</properties>
|
||||
|
||||
<build>
|
||||
<finalName>core-java-lang-oop-2</finalName>
|
||||
@@ -45,4 +39,10 @@
|
||||
</resources>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<!-- testing -->
|
||||
<assertj-core.version>3.10.0</assertj-core.version>
|
||||
<equalsverifier.version>3.0.3</equalsverifier.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.core.modifiers;
|
||||
|
||||
public class FirstClass {
|
||||
|
||||
protected String name;
|
||||
|
||||
protected FirstClass(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
protected String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
protected static class InnerClass {
|
||||
public InnerClass() {}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.core.modifiers;
|
||||
|
||||
public class GenericClass {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// accessing protected constructor
|
||||
FirstClass first = new FirstClass("random name");
|
||||
// using protected method
|
||||
System.out.println("FirstClass name is " + first.getName());
|
||||
// accessing a protected field
|
||||
first.name = "new name";
|
||||
// instantiating protected inner class
|
||||
FirstClass.InnerClass innerClass = new FirstClass.InnerClass();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.core.modifiers.otherpackage;
|
||||
|
||||
import com.baeldung.core.modifiers.FirstClass;
|
||||
|
||||
public class SecondClass extends FirstClass {
|
||||
|
||||
public SecondClass(String name) {
|
||||
// accessing protected constructor
|
||||
super(name);
|
||||
// using protected method
|
||||
System.out.println("SecondClass name is " + this.getName());
|
||||
// accessing a protected field
|
||||
this.name = "new name";
|
||||
// instantiating protected inner class -> add public constructor to InnerClass
|
||||
FirstClass.InnerClass innerClass = new FirstClass.InnerClass();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.core.modifiers.otherpackage;
|
||||
|
||||
import com.baeldung.core.modifiers.FirstClass;
|
||||
//import com.baeldung.core.modifiers.FirstClass.InnerClass;
|
||||
|
||||
public class SecondGenericClass {
|
||||
|
||||
// uncomment the following lines to see the errors
|
||||
public static void main(String[] args) {
|
||||
// accessing protected constructor
|
||||
// FirstClass first = new FirstClass("random name");
|
||||
// using protected method
|
||||
// System.out.println("FirstClass name is " + first.getName());
|
||||
// accessing a protected field
|
||||
// first.name = "new name";
|
||||
// instantiating protected inner class
|
||||
// FirstClass.InnerClass innerClass = new FirstClass.InnerClass();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -54,7 +54,7 @@
|
||||
</resources>
|
||||
|
||||
<plugins>
|
||||
<plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
|
||||
12
core-java-modules/core-java-string-apis/README.md
Normal file
12
core-java-modules/core-java-string-apis/README.md
Normal file
@@ -0,0 +1,12 @@
|
||||
## Java String APIs
|
||||
|
||||
This module contains articles about string APIs.
|
||||
|
||||
### Relevant Articles:
|
||||
- [Java 8 StringJoiner](https://www.baeldung.com/java-string-joiner)
|
||||
- [Quick Guide to the Java StringTokenizer](https://www.baeldung.com/java-stringtokenizer)
|
||||
- [Guide to java.util.Formatter](https://www.baeldung.com/java-string-formatter)
|
||||
- [Guide to StreamTokenizer](https://www.baeldung.com/java-streamtokenizer)
|
||||
- [CharSequence vs. String in Java](https://www.baeldung.com/java-char-sequence-string)
|
||||
- [StringBuilder and StringBuffer in Java](https://www.baeldung.com/java-string-builder-string-buffer)
|
||||
- [Generate a Secure Random Password in Java](https://www.baeldung.com/java-generate-secure-password)
|
||||
55
core-java-modules/core-java-string-apis/pom.xml
Normal file
55
core-java-modules/core-java-string-apis/pom.xml
Normal file
@@ -0,0 +1,55 @@
|
||||
<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-string-apis</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
<name>core-java-string-apis</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-java</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-core</artifactId>
|
||||
<version>${jmh-core.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-generator-annprocess</artifactId>
|
||||
<version>${jmh-generator.version}</version>
|
||||
</dependency>
|
||||
<!-- Added for password generation -->
|
||||
<dependency>
|
||||
<groupId>org.passay</groupId>
|
||||
<artifactId>passay</artifactId>
|
||||
<version>${passay.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-text</artifactId>
|
||||
<version>${commons-text.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>core-java-string-apis</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<passay.version>1.3.1</passay.version>
|
||||
<commons-text.version>1.4</commons-text.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -1,4 +1,11 @@
|
||||
package com.baeldung.string.password;
|
||||
package com.baeldung.password;
|
||||
|
||||
import org.apache.commons.lang3.RandomStringUtils;
|
||||
import org.apache.commons.text.RandomStringGenerator;
|
||||
import org.passay.CharacterRule;
|
||||
import org.passay.EnglishCharacterData;
|
||||
import org.passay.CharacterData;
|
||||
import org.passay.PasswordGenerator;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
import java.util.Collections;
|
||||
@@ -8,13 +15,6 @@ import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.apache.commons.lang3.RandomStringUtils;
|
||||
import org.apache.commons.text.RandomStringGenerator;
|
||||
import org.passay.CharacterData;
|
||||
import org.passay.CharacterRule;
|
||||
import org.passay.EnglishCharacterData;
|
||||
import org.passay.PasswordGenerator;
|
||||
|
||||
public class RandomPasswordGenerator {
|
||||
|
||||
/**
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.string.streamtokenizer;
|
||||
package com.baeldung.streamtokenizer;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.ArrayList;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.string;
|
||||
package com.baeldung.stringbuilderstringbuffer;
|
||||
|
||||
import org.openjdk.jmh.annotations.Benchmark;
|
||||
import org.openjdk.jmh.annotations.Scope;
|
||||
@@ -8,12 +8,12 @@ import org.openjdk.jmh.runner.RunnerException;
|
||||
import org.openjdk.jmh.runner.options.Options;
|
||||
import org.openjdk.jmh.runner.options.OptionsBuilder;
|
||||
|
||||
public class StringBufferStringBuilder {
|
||||
public class StringBuilderStringBuffer {
|
||||
|
||||
public static void main(String[] args) throws RunnerException {
|
||||
|
||||
Options opt = new OptionsBuilder()
|
||||
.include(StringBufferStringBuilder.class.getSimpleName())
|
||||
.include(StringBuilderStringBuffer.class.getSimpleName())
|
||||
.build();
|
||||
|
||||
new Runner(opt).run();
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.string;
|
||||
package com.baeldung.charsequence;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
package com.baeldung.string.formatter;
|
||||
package com.baeldung.formatter;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.Formatter;
|
||||
import java.util.GregorianCalendar;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class StringFormatterExampleUnitTest {
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
package com.baeldung.string.password;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
package com.baeldung.password;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* Examples of passwords conforming to various specifications, using different libraries.
|
||||
*
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.string.streamtokenizer;
|
||||
package com.baeldung.streamtokenizer;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.baeldung.stringjoiner;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
@@ -8,7 +8,7 @@ import java.util.List;
|
||||
import java.util.StringJoiner;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class StringJoinerUnitTest {
|
||||
private final String DELIMITER_COMMA = ",";
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user