Merge branch 'master' into thombergs-patch-4
This commit is contained in:
		
						commit
						544e7b2d22
					
				| @ -14,7 +14,7 @@ Java and Spring Tutorials | |||||||
| ================ | ================ | ||||||
| 
 | 
 | ||||||
| This project is **a collection of small and focused tutorials** - each covering a single and well defined area of development in the Java ecosystem.  | This project is **a collection of small and focused tutorials** - each covering a single and well defined area of development in the Java ecosystem.  | ||||||
| A strong focus of these is, of course, the Spring Framework - Spring, Spring Boot and Spring Securiyt.  | A strong focus of these is, of course, the Spring Framework - Spring, Spring Boot and Spring Security.  | ||||||
| In additional to Spring, the following technologies are in focus: `core Java`, `Jackson`, `HttpClient`, `Guava`.  | In additional to Spring, the following technologies are in focus: `core Java`, `Jackson`, `HttpClient`, `Guava`.  | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -1,3 +1,3 @@ | |||||||
| ### Relevant articles | ## Relevant articles: | ||||||
| 
 | 
 | ||||||
| - [Introduction to Akka HTTP](https://www.baeldung.com/akka-http) | - [Introduction to Akka HTTP](https://www.baeldung.com/akka-http) | ||||||
|  | |||||||
| @ -12,4 +12,5 @@ | |||||||
| - [Check If a String Contains All The Letters of The Alphabet](https://www.baeldung.com/java-string-contains-all-letters) | - [Check If a String Contains All The Letters of The Alphabet](https://www.baeldung.com/java-string-contains-all-letters) | ||||||
| - [Find the Middle Element of a Linked List](http://www.baeldung.com/java-linked-list-middle-element) | - [Find the Middle Element of a Linked List](http://www.baeldung.com/java-linked-list-middle-element) | ||||||
| - [Calculate Factorial in Java](https://www.baeldung.com/java-calculate-factorial) | - [Calculate Factorial in Java](https://www.baeldung.com/java-calculate-factorial) | ||||||
| - [Find Substrings That Are Palindromes in Java](https://www.baeldung.com/java-palindrome-substrings) | - [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) | ||||||
|  | |||||||
| @ -0,0 +1,123 @@ | |||||||
|  | package com.baeldung.algorithms.permutation; | ||||||
|  | 
 | ||||||
|  | import java.util.Arrays; | ||||||
|  | import java.util.Collections; | ||||||
|  | 
 | ||||||
|  | public  class Permutation { | ||||||
|  | 
 | ||||||
|  |     public static <T> void printAllRecursive(T[] elements, char delimiter) { | ||||||
|  |         printAllRecursive(elements.length, elements, delimiter); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public static <T> void printAllRecursive(int n, T[] elements, char delimiter) { | ||||||
|  | 
 | ||||||
|  |         if(n == 1) { | ||||||
|  |             printArray(elements, delimiter); | ||||||
|  |         } else { | ||||||
|  |             for(int i = 0; i < n-1; i++) { | ||||||
|  |                 printAllRecursive(n - 1, elements, delimiter); | ||||||
|  |                 if(n % 2 == 0) { | ||||||
|  |                     swap(elements, i, n-1); | ||||||
|  |                 } else { | ||||||
|  |                     swap(elements, 0, n-1); | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |             printAllRecursive(n - 1, elements, delimiter); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public static <T> void printAllIterative(int n, T[] elements, char delimiter) { | ||||||
|  | 
 | ||||||
|  |         int[] indexes = new int[n]; | ||||||
|  |         for (int i = 0; i < n; i++) { | ||||||
|  |             indexes[i] = 0; | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         printArray(elements, delimiter); | ||||||
|  | 
 | ||||||
|  |         int i = 0; | ||||||
|  |         while (i < n) { | ||||||
|  |             if (indexes[i] < i) { | ||||||
|  |                 swap(elements, i % 2 == 0 ?  0: indexes[i], i); | ||||||
|  |                 printArray(elements, delimiter); | ||||||
|  |                 indexes[i]++; | ||||||
|  |                 i = 0; | ||||||
|  |             } | ||||||
|  |             else { | ||||||
|  |                 indexes[i] = 0; | ||||||
|  |                 i++; | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public static <T extends Comparable<T>> void printAllOrdered(T[] elements, char delimiter) { | ||||||
|  | 
 | ||||||
|  |         Arrays.sort(elements); | ||||||
|  |         boolean hasNext = true; | ||||||
|  | 
 | ||||||
|  |         while(hasNext) { | ||||||
|  |             printArray(elements, delimiter); | ||||||
|  |             int k = 0, l = 0; | ||||||
|  |             hasNext = false; | ||||||
|  |             for (int i = elements.length - 1; i > 0; i--) { | ||||||
|  |                 if (elements[i].compareTo(elements[i - 1]) > 0) { | ||||||
|  |                     k = i - 1; | ||||||
|  |                     hasNext = true; | ||||||
|  |                     break; | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  | 
 | ||||||
|  |             for (int i = elements.length - 1; i > k; i--) { | ||||||
|  |                 if (elements[i].compareTo(elements[k]) > 0) { | ||||||
|  |                     l = i; | ||||||
|  |                     break; | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  | 
 | ||||||
|  |             swap(elements, k, l); | ||||||
|  |             Collections.reverse(Arrays.asList(elements).subList(k + 1, elements.length)); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public static <T> void printRandom(T[] elements, char delimiter) { | ||||||
|  | 
 | ||||||
|  |         Collections.shuffle(Arrays.asList(elements)); | ||||||
|  |         printArray(elements, delimiter); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     private static <T> void swap(T[] elements, int a, int b) { | ||||||
|  | 
 | ||||||
|  |         T tmp = elements[a]; | ||||||
|  |         elements[a] = elements[b]; | ||||||
|  |         elements[b] = tmp; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     private static <T> void printArray(T[] elements, char delimiter) { | ||||||
|  | 
 | ||||||
|  |         String delimiterSpace = delimiter + " "; | ||||||
|  |         for(int i = 0; i < elements.length; i++) { | ||||||
|  |             System.out.print(elements[i] + delimiterSpace); | ||||||
|  |         } | ||||||
|  |         System.out.print('\n'); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public static void main(String[] argv) { | ||||||
|  | 
 | ||||||
|  |         Integer[] elements = {1,2,3,4}; | ||||||
|  | 
 | ||||||
|  |         System.out.println("Rec:"); | ||||||
|  |         printAllRecursive(elements, ';'); | ||||||
|  | 
 | ||||||
|  |         System.out.println("Iter:"); | ||||||
|  |         printAllIterative(elements.length, elements, ';'); | ||||||
|  | 
 | ||||||
|  |         System.out.println("Orderes:"); | ||||||
|  |         printAllOrdered(elements, ';'); | ||||||
|  | 
 | ||||||
|  |         System.out.println("Random:"); | ||||||
|  |         printRandom(elements, ';'); | ||||||
|  | 
 | ||||||
|  |         System.out.println("Random:"); | ||||||
|  |         printRandom(elements, ';'); | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -0,0 +1,16 @@ | |||||||
|  | package com.baeldung.algorithms.twopointertechnique; | ||||||
|  | 
 | ||||||
|  | public class LinkedListFindMiddle { | ||||||
|  | 
 | ||||||
|  |     public <T> T findMiddle(MyNode<T> head) { | ||||||
|  |         MyNode<T> slowPointer = head; | ||||||
|  |         MyNode<T> fastPointer = head; | ||||||
|  | 
 | ||||||
|  |         while (fastPointer.next != null && fastPointer.next.next != null) { | ||||||
|  |             fastPointer = fastPointer.next.next; | ||||||
|  |             slowPointer = slowPointer.next; | ||||||
|  |         } | ||||||
|  |         return slowPointer.data; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | } | ||||||
| @ -0,0 +1,20 @@ | |||||||
|  | package com.baeldung.algorithms.twopointertechnique; | ||||||
|  | 
 | ||||||
|  | public class MyNode<E> { | ||||||
|  |     MyNode<E> next; | ||||||
|  |     E data; | ||||||
|  | 
 | ||||||
|  |     public MyNode(E value) { | ||||||
|  |         data = value; | ||||||
|  |         next = null; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public MyNode(E value, MyNode<E> n) { | ||||||
|  |         data = value; | ||||||
|  |         next = n; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public void setNext(MyNode<E> n) { | ||||||
|  |         next = n; | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -0,0 +1,22 @@ | |||||||
|  | package com.baeldung.algorithms.twopointertechnique; | ||||||
|  | 
 | ||||||
|  | public class RotateArray { | ||||||
|  | 
 | ||||||
|  |     public void rotate(int[] input, int step) { | ||||||
|  |         step %= input.length; | ||||||
|  |         reverse(input, 0, input.length - 1); | ||||||
|  |         reverse(input, 0, step - 1); | ||||||
|  |         reverse(input, step, input.length - 1); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     private void reverse(int[] input, int start, int end) { | ||||||
|  |         while (start < end) { | ||||||
|  |             int temp = input[start]; | ||||||
|  |             input[start] = input[end]; | ||||||
|  |             input[end] = temp; | ||||||
|  |             start++; | ||||||
|  |             end--; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | } | ||||||
| @ -0,0 +1,38 @@ | |||||||
|  | package com.baeldung.algorithms.twopointertechnique; | ||||||
|  | 
 | ||||||
|  | public class TwoSum { | ||||||
|  | 
 | ||||||
|  |     public boolean twoSum(int[] input, int targetValue) { | ||||||
|  | 
 | ||||||
|  |         int pointerOne = 0; | ||||||
|  |         int pointerTwo = input.length - 1; | ||||||
|  | 
 | ||||||
|  |         while (pointerOne < pointerTwo) { | ||||||
|  |             int sum = input[pointerOne] + input[pointerTwo]; | ||||||
|  | 
 | ||||||
|  |             if (sum == targetValue) { | ||||||
|  |                 return true; | ||||||
|  |             } else if (sum < targetValue) { | ||||||
|  |                 pointerOne++; | ||||||
|  |             } else { | ||||||
|  |                 pointerTwo--; | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         return false; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public boolean twoSumSlow(int[] input, int targetValue) { | ||||||
|  | 
 | ||||||
|  |         for (int i = 0; i < input.length; i++) { | ||||||
|  |             for (int j = 1; j < input.length; j++) { | ||||||
|  |                 if (input[i] + input[j] == targetValue) { | ||||||
|  |                     return true; | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         return false; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | } | ||||||
| @ -0,0 +1,37 @@ | |||||||
|  | package com.baeldung.algorithms.twopointertechnique; | ||||||
|  | 
 | ||||||
|  | import static org.assertj.core.api.Assertions.assertThat; | ||||||
|  | 
 | ||||||
|  | import org.junit.Test; | ||||||
|  | 
 | ||||||
|  | public class LinkedListFindMiddleUnitTest { | ||||||
|  | 
 | ||||||
|  |     LinkedListFindMiddle linkedListFindMiddle = new LinkedListFindMiddle(); | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     public void givenLinkedListOfMyNodes_whenLinkedListFindMiddle_thenCorrect() { | ||||||
|  | 
 | ||||||
|  |         MyNode<String> head = createNodesList(8); | ||||||
|  | 
 | ||||||
|  |         assertThat(linkedListFindMiddle.findMiddle(head)).isEqualTo("4"); | ||||||
|  | 
 | ||||||
|  |         head = createNodesList(9); | ||||||
|  | 
 | ||||||
|  |         assertThat(linkedListFindMiddle.findMiddle(head)).isEqualTo("5"); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     private static MyNode<String> createNodesList(int n) { | ||||||
|  | 
 | ||||||
|  |         MyNode<String> head = new MyNode<String>("1"); | ||||||
|  |         MyNode<String> current = head; | ||||||
|  | 
 | ||||||
|  |         for (int i = 2; i <= n; i++) { | ||||||
|  |             MyNode<String> newNode = new MyNode<String>(String.valueOf(i)); | ||||||
|  |             current.setNext(newNode); | ||||||
|  |             current = newNode; | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         return head; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | } | ||||||
| @ -0,0 +1,26 @@ | |||||||
|  | package com.baeldung.algorithms.twopointertechnique; | ||||||
|  | 
 | ||||||
|  | import static org.assertj.core.api.Assertions.assertThat; | ||||||
|  | 
 | ||||||
|  | import org.junit.Test; | ||||||
|  | 
 | ||||||
|  | public class RotateArrayUnitTest { | ||||||
|  | 
 | ||||||
|  |     private RotateArray rotateArray = new RotateArray(); | ||||||
|  | 
 | ||||||
|  |     private int[] inputArray; | ||||||
|  | 
 | ||||||
|  |     private int step; | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     public void givenAnArrayOfIntegers_whenRotateKsteps_thenCorrect() { | ||||||
|  | 
 | ||||||
|  |         inputArray = new int[] { 1, 2, 3, 4, 5, 6, 7 }; | ||||||
|  |         step = 4; | ||||||
|  | 
 | ||||||
|  |         rotateArray.rotate(inputArray, step); | ||||||
|  | 
 | ||||||
|  |         assertThat(inputArray).containsExactly(new int[] { 4, 5, 6, 7, 1, 2, 3 }); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | } | ||||||
| @ -0,0 +1,56 @@ | |||||||
|  | package com.baeldung.algorithms.twopointertechnique; | ||||||
|  | 
 | ||||||
|  | import static org.junit.Assert.assertFalse; | ||||||
|  | import static org.junit.Assert.assertTrue; | ||||||
|  | 
 | ||||||
|  | import org.junit.Test; | ||||||
|  | 
 | ||||||
|  | public class TwoSumUnitTest { | ||||||
|  | 
 | ||||||
|  |     private TwoSum twoSum = new TwoSum(); | ||||||
|  | 
 | ||||||
|  |     private int[] sortedArray; | ||||||
|  | 
 | ||||||
|  |     private int targetValue; | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     public void givenASortedArrayOfIntegers_whenTwoSumSlow_thenPairExists() { | ||||||
|  | 
 | ||||||
|  |         sortedArray = new int[] { 0, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9 }; | ||||||
|  | 
 | ||||||
|  |         targetValue = 12; | ||||||
|  | 
 | ||||||
|  |         assertTrue(twoSum.twoSumSlow(sortedArray, targetValue)); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     public void givenASortedArrayOfIntegers_whenTwoSumSlow_thenPairDoesNotExists() { | ||||||
|  | 
 | ||||||
|  |         sortedArray = new int[] { 0, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9 }; | ||||||
|  | 
 | ||||||
|  |         targetValue = 20; | ||||||
|  | 
 | ||||||
|  |         assertFalse(twoSum.twoSumSlow(sortedArray, targetValue)); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     public void givenASortedArrayOfIntegers_whenTwoSum_thenPairExists() { | ||||||
|  | 
 | ||||||
|  |         sortedArray = new int[] { 0, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9 }; | ||||||
|  | 
 | ||||||
|  |         targetValue = 12; | ||||||
|  | 
 | ||||||
|  |         assertTrue(twoSum.twoSum(sortedArray, targetValue)); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     public void givenASortedArrayOfIntegers_whenTwoSum_thenPairDoesNotExists() { | ||||||
|  | 
 | ||||||
|  |         sortedArray = new int[] { 0, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9 }; | ||||||
|  | 
 | ||||||
|  |         targetValue = 20; | ||||||
|  | 
 | ||||||
|  |         assertFalse(twoSum.twoSum(sortedArray, targetValue)); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | } | ||||||
| @ -15,16 +15,76 @@ | |||||||
|     </parent> |     </parent> | ||||||
| 
 | 
 | ||||||
|     <dependencies> |     <dependencies> | ||||||
|         <!-- https://mvnrepository.com/artifact/org.apache.spark/spark-core_2.10 --> |  | ||||||
|         <dependency> |         <dependency> | ||||||
|             <groupId>org.apache.spark</groupId> |             <groupId>org.apache.spark</groupId> | ||||||
|             <artifactId>spark-core_2.10</artifactId> |             <artifactId>spark-core_2.11</artifactId> | ||||||
|             <version>${org.apache.spark.spark-core.version}</version> |             <version>${org.apache.spark.spark-core.version}</version> | ||||||
|  |             <scope>provided</scope> | ||||||
|         </dependency> |         </dependency> | ||||||
|  | 		<dependency> | ||||||
|  |     		<groupId>org.apache.spark</groupId> | ||||||
|  |     		<artifactId>spark-sql_2.11</artifactId> | ||||||
|  |     		<version>${org.apache.spark.spark-sql.version}</version> | ||||||
|  |     		<scope>provided</scope> | ||||||
|  | 		</dependency> | ||||||
|  | 		<dependency> | ||||||
|  | 			<groupId>org.apache.spark</groupId> | ||||||
|  | 			<artifactId>spark-streaming_2.11</artifactId> | ||||||
|  | 			<version>${org.apache.spark.spark-streaming.version}</version> | ||||||
|  | 			<scope>provided</scope> | ||||||
|  | 		</dependency> | ||||||
|  | 		<dependency> | ||||||
|  | 			<groupId>org.apache.spark</groupId> | ||||||
|  | 			<artifactId>spark-streaming-kafka-0-10_2.11</artifactId> | ||||||
|  | 			<version>${org.apache.spark.spark-streaming-kafka.version}</version> | ||||||
|  | 		</dependency> | ||||||
|  | 		<dependency> | ||||||
|  | 			<groupId>com.datastax.spark</groupId> | ||||||
|  | 			<artifactId>spark-cassandra-connector_2.11</artifactId> | ||||||
|  | 			<version>${com.datastax.spark.spark-cassandra-connector.version}</version> | ||||||
|  | 		</dependency> | ||||||
|  | 		<dependency> | ||||||
|  | 			<groupId>com.datastax.spark</groupId> | ||||||
|  | 			<artifactId>spark-cassandra-connector-java_2.11</artifactId> | ||||||
|  | 			<version>${com.datastax.spark.spark-cassandra-connector-java.version}</version> | ||||||
|  | 		</dependency> | ||||||
|     </dependencies> |     </dependencies> | ||||||
| 
 | 	<build> | ||||||
|  |     	<plugins> | ||||||
|  |     	  	<plugin> | ||||||
|  |     			<groupId>org.apache.maven.plugins</groupId> | ||||||
|  |     			<artifactId>maven-compiler-plugin</artifactId> | ||||||
|  |     			<version>3.2</version> | ||||||
|  |     			<configuration> | ||||||
|  |       				<source>1.8</source> | ||||||
|  |       				<target>1.8</target> | ||||||
|  |     			</configuration> | ||||||
|  |   			</plugin> | ||||||
|  |       		<plugin> | ||||||
|  |         		<artifactId>maven-assembly-plugin</artifactId> | ||||||
|  |         		<executions> | ||||||
|  |           			<execution> | ||||||
|  |             			<phase>package</phase> | ||||||
|  |             			<goals> | ||||||
|  |               				<goal>single</goal> | ||||||
|  |             			</goals> | ||||||
|  |           			</execution> | ||||||
|  |         		</executions> | ||||||
|  |         		<configuration> | ||||||
|  |           			<descriptorRefs> | ||||||
|  |             			<descriptorRef>jar-with-dependencies</descriptorRef> | ||||||
|  |           			</descriptorRefs> | ||||||
|  |         		</configuration> | ||||||
|  |       		</plugin> | ||||||
|  |    		</plugins> | ||||||
|  |   	</build> | ||||||
|     <properties> |     <properties> | ||||||
|         <org.apache.spark.spark-core.version>2.2.0</org.apache.spark.spark-core.version> |         <org.apache.spark.spark-core.version>2.3.0</org.apache.spark.spark-core.version> | ||||||
|  |         <org.apache.spark.spark-sql.version>2.3.0</org.apache.spark.spark-sql.version> | ||||||
|  |         <org.apache.spark.spark-streaming.version>2.3.0</org.apache.spark.spark-streaming.version> | ||||||
|  |         <org.apache.spark.spark-streaming-kafka.version>2.3.0</org.apache.spark.spark-streaming-kafka.version> | ||||||
|  |         <com.datastax.spark.spark-cassandra-connector.version>2.3.0</com.datastax.spark.spark-cassandra-connector.version> | ||||||
|  |         <com.datastax.spark.spark-cassandra-connector-java.version>1.5.2</com.datastax.spark.spark-cassandra-connector-java.version> | ||||||
|     </properties> |     </properties> | ||||||
| 
 | 
 | ||||||
| </project> | </project> | ||||||
|  | |||||||
| @ -0,0 +1,25 @@ | |||||||
|  | package com.baeldung.data.pipeline; | ||||||
|  | 
 | ||||||
|  | import java.io.Serializable; | ||||||
|  | 
 | ||||||
|  | public class Word implements Serializable { | ||||||
|  |     private static final long serialVersionUID = 1L; | ||||||
|  |     private String word; | ||||||
|  |     private int count; | ||||||
|  |     Word(String word, int count) { | ||||||
|  |         this.word = word; | ||||||
|  |         this.count = count; | ||||||
|  |     } | ||||||
|  |     public String getWord() { | ||||||
|  |         return word; | ||||||
|  |     } | ||||||
|  |     public void setWord(String word) { | ||||||
|  |         this.word = word; | ||||||
|  |     } | ||||||
|  |     public int getCount() { | ||||||
|  |         return count; | ||||||
|  |     } | ||||||
|  |     public void setCount(int count) { | ||||||
|  |         this.count = count; | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -0,0 +1,116 @@ | |||||||
|  | package com.baeldung.data.pipeline; | ||||||
|  | 
 | ||||||
|  | import static com.datastax.spark.connector.japi.CassandraJavaUtil.javaFunctions; | ||||||
|  | import static com.datastax.spark.connector.japi.CassandraJavaUtil.mapToRow; | ||||||
|  | 
 | ||||||
|  | import java.util.Arrays; | ||||||
|  | import java.util.Collection; | ||||||
|  | import java.util.HashMap; | ||||||
|  | import java.util.Iterator; | ||||||
|  | import java.util.List; | ||||||
|  | import java.util.Map; | ||||||
|  | 
 | ||||||
|  | import org.apache.kafka.clients.consumer.ConsumerRecord; | ||||||
|  | import org.apache.kafka.common.serialization.StringDeserializer; | ||||||
|  | import org.apache.log4j.Level; | ||||||
|  | import org.apache.log4j.Logger; | ||||||
|  | import org.apache.spark.SparkConf; | ||||||
|  | import org.apache.spark.api.java.JavaPairRDD; | ||||||
|  | import org.apache.spark.api.java.JavaRDD; | ||||||
|  | import org.apache.spark.api.java.function.FlatMapFunction; | ||||||
|  | import org.apache.spark.api.java.function.Function; | ||||||
|  | import org.apache.spark.api.java.function.Function2; | ||||||
|  | import org.apache.spark.api.java.function.PairFunction; | ||||||
|  | import org.apache.spark.api.java.function.VoidFunction; | ||||||
|  | import org.apache.spark.streaming.Durations; | ||||||
|  | import org.apache.spark.streaming.api.java.JavaDStream; | ||||||
|  | import org.apache.spark.streaming.api.java.JavaInputDStream; | ||||||
|  | import org.apache.spark.streaming.api.java.JavaPairDStream; | ||||||
|  | import org.apache.spark.streaming.api.java.JavaStreamingContext; | ||||||
|  | import org.apache.spark.streaming.kafka010.ConsumerStrategies; | ||||||
|  | import org.apache.spark.streaming.kafka010.KafkaUtils; | ||||||
|  | import org.apache.spark.streaming.kafka010.LocationStrategies; | ||||||
|  | 
 | ||||||
|  | import scala.Tuple2; | ||||||
|  | 
 | ||||||
|  | public class WordCountingApp { | ||||||
|  | 
 | ||||||
|  |     @SuppressWarnings("serial") | ||||||
|  |     public static void main(String[] args) throws InterruptedException { | ||||||
|  |         Logger.getLogger("org") | ||||||
|  |             .setLevel(Level.OFF); | ||||||
|  |         Logger.getLogger("akka") | ||||||
|  |             .setLevel(Level.OFF); | ||||||
|  | 
 | ||||||
|  |         Map<String, Object> kafkaParams = new HashMap<>(); | ||||||
|  |         kafkaParams.put("bootstrap.servers", "localhost:9092"); | ||||||
|  |         kafkaParams.put("key.deserializer", StringDeserializer.class); | ||||||
|  |         kafkaParams.put("value.deserializer", StringDeserializer.class); | ||||||
|  |         kafkaParams.put("group.id", "use_a_separate_group_id_for_each_stream"); | ||||||
|  |         kafkaParams.put("auto.offset.reset", "latest"); | ||||||
|  |         kafkaParams.put("enable.auto.commit", false); | ||||||
|  | 
 | ||||||
|  |         Collection<String> topics = Arrays.asList("messages"); | ||||||
|  | 
 | ||||||
|  |         SparkConf sparkConf = new SparkConf(); | ||||||
|  |         sparkConf.setMaster("local[2]"); | ||||||
|  |         sparkConf.setAppName("WordCountingApp"); | ||||||
|  |         sparkConf.set("spark.cassandra.connection.host", "127.0.0.1"); | ||||||
|  | 
 | ||||||
|  |         JavaStreamingContext streamingContext = new JavaStreamingContext(sparkConf, Durations.seconds(1)); | ||||||
|  | 
 | ||||||
|  |         JavaInputDStream<ConsumerRecord<String, String>> messages = KafkaUtils.createDirectStream(streamingContext, LocationStrategies.PreferConsistent(), ConsumerStrategies.<String, String> Subscribe(topics, kafkaParams)); | ||||||
|  | 
 | ||||||
|  |         JavaPairDStream<String, String> results = messages.mapToPair(new PairFunction<ConsumerRecord<String, String>, String, String>() { | ||||||
|  |             @Override | ||||||
|  |             public Tuple2<String, String> call(ConsumerRecord<String, String> record) { | ||||||
|  |                 return new Tuple2<>(record.key(), record.value()); | ||||||
|  |             } | ||||||
|  |         }); | ||||||
|  | 
 | ||||||
|  |         JavaDStream<String> lines = results.map(new Function<Tuple2<String, String>, String>() { | ||||||
|  |             @Override | ||||||
|  |             public String call(Tuple2<String, String> tuple2) { | ||||||
|  |                 return tuple2._2(); | ||||||
|  |             } | ||||||
|  |         }); | ||||||
|  | 
 | ||||||
|  |         JavaDStream<String> words = lines.flatMap(new FlatMapFunction<String, String>() { | ||||||
|  |             @Override | ||||||
|  |             public Iterator<String> call(String x) { | ||||||
|  |                 return Arrays.asList(x.split("\\s+")) | ||||||
|  |                     .iterator(); | ||||||
|  |             } | ||||||
|  |         }); | ||||||
|  | 
 | ||||||
|  |         JavaPairDStream<String, Integer> wordCounts = words.mapToPair(new PairFunction<String, String, Integer>() { | ||||||
|  |             @Override | ||||||
|  |             public Tuple2<String, Integer> call(String s) { | ||||||
|  |                 return new Tuple2<>(s, 1); | ||||||
|  |             } | ||||||
|  |         }) | ||||||
|  |             .reduceByKey(new Function2<Integer, Integer, Integer>() { | ||||||
|  |                 @Override | ||||||
|  |                 public Integer call(Integer i1, Integer i2) { | ||||||
|  |                     return i1 + i2; | ||||||
|  |                 } | ||||||
|  |             }); | ||||||
|  | 
 | ||||||
|  |         wordCounts.foreachRDD(new VoidFunction<JavaPairRDD<String, Integer>>() { | ||||||
|  |             @Override | ||||||
|  |             public void call(JavaPairRDD<String, Integer> javaRdd) throws Exception { | ||||||
|  |                 Map<String, Integer> wordCountMap = javaRdd.collectAsMap(); | ||||||
|  |                 for (String key : wordCountMap.keySet()) { | ||||||
|  |                     List<Word> words = Arrays.asList(new Word(key, wordCountMap.get(key))); | ||||||
|  |                     JavaRDD<Word> rdd = streamingContext.sparkContext() | ||||||
|  |                         .parallelize(words); | ||||||
|  |                     javaFunctions(rdd).writerBuilder("vocabulary", "words", mapToRow(Word.class)) | ||||||
|  |                         .saveToCassandra(); | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |         }); | ||||||
|  | 
 | ||||||
|  |         streamingContext.start(); | ||||||
|  |         streamingContext.awaitTermination(); | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -0,0 +1,140 @@ | |||||||
|  | package com.baeldung.data.pipeline; | ||||||
|  | 
 | ||||||
|  | import static com.datastax.spark.connector.japi.CassandraJavaUtil.javaFunctions; | ||||||
|  | import static com.datastax.spark.connector.japi.CassandraJavaUtil.mapToRow; | ||||||
|  | 
 | ||||||
|  | import java.util.Arrays; | ||||||
|  | import java.util.Collection; | ||||||
|  | import java.util.HashMap; | ||||||
|  | import java.util.Iterator; | ||||||
|  | import java.util.List; | ||||||
|  | import java.util.Map; | ||||||
|  | 
 | ||||||
|  | import org.apache.kafka.clients.consumer.ConsumerRecord; | ||||||
|  | import org.apache.kafka.common.serialization.StringDeserializer; | ||||||
|  | import org.apache.log4j.Level; | ||||||
|  | import org.apache.log4j.Logger; | ||||||
|  | import org.apache.spark.SparkConf; | ||||||
|  | import org.apache.spark.api.java.JavaPairRDD; | ||||||
|  | import org.apache.spark.api.java.JavaRDD; | ||||||
|  | import org.apache.spark.api.java.JavaSparkContext; | ||||||
|  | import org.apache.spark.api.java.Optional; | ||||||
|  | import org.apache.spark.api.java.function.FlatMapFunction; | ||||||
|  | import org.apache.spark.api.java.function.Function; | ||||||
|  | import org.apache.spark.api.java.function.Function2; | ||||||
|  | import org.apache.spark.api.java.function.Function3; | ||||||
|  | import org.apache.spark.api.java.function.PairFunction; | ||||||
|  | import org.apache.spark.api.java.function.VoidFunction; | ||||||
|  | import org.apache.spark.streaming.Durations; | ||||||
|  | import org.apache.spark.streaming.State; | ||||||
|  | import org.apache.spark.streaming.StateSpec; | ||||||
|  | import org.apache.spark.streaming.api.java.JavaDStream; | ||||||
|  | import org.apache.spark.streaming.api.java.JavaInputDStream; | ||||||
|  | import org.apache.spark.streaming.api.java.JavaMapWithStateDStream; | ||||||
|  | import org.apache.spark.streaming.api.java.JavaPairDStream; | ||||||
|  | import org.apache.spark.streaming.api.java.JavaStreamingContext; | ||||||
|  | import org.apache.spark.streaming.kafka010.ConsumerStrategies; | ||||||
|  | import org.apache.spark.streaming.kafka010.KafkaUtils; | ||||||
|  | import org.apache.spark.streaming.kafka010.LocationStrategies; | ||||||
|  | 
 | ||||||
|  | import scala.Tuple2; | ||||||
|  | 
 | ||||||
|  | public class WordCountingAppWithCheckpoint { | ||||||
|  | 
 | ||||||
|  |     public static JavaSparkContext sparkContext; | ||||||
|  | 
 | ||||||
|  |     @SuppressWarnings("serial") | ||||||
|  |     public static void main(String[] args) throws InterruptedException { | ||||||
|  | 
 | ||||||
|  |         Logger.getLogger("org") | ||||||
|  |             .setLevel(Level.OFF); | ||||||
|  |         Logger.getLogger("akka") | ||||||
|  |             .setLevel(Level.OFF); | ||||||
|  | 
 | ||||||
|  |         Map<String, Object> kafkaParams = new HashMap<>(); | ||||||
|  |         kafkaParams.put("bootstrap.servers", "localhost:9092"); | ||||||
|  |         kafkaParams.put("key.deserializer", StringDeserializer.class); | ||||||
|  |         kafkaParams.put("value.deserializer", StringDeserializer.class); | ||||||
|  |         kafkaParams.put("group.id", "use_a_separate_group_id_for_each_stream"); | ||||||
|  |         kafkaParams.put("auto.offset.reset", "latest"); | ||||||
|  |         kafkaParams.put("enable.auto.commit", false); | ||||||
|  | 
 | ||||||
|  |         Collection<String> topics = Arrays.asList("messages"); | ||||||
|  | 
 | ||||||
|  |         SparkConf sparkConf = new SparkConf(); | ||||||
|  |         sparkConf.setMaster("local[2]"); | ||||||
|  |         sparkConf.setAppName("WordCountingAppWithCheckpoint"); | ||||||
|  |         sparkConf.set("spark.cassandra.connection.host", "127.0.0.1"); | ||||||
|  | 
 | ||||||
|  |         JavaStreamingContext streamingContext = new JavaStreamingContext(sparkConf, Durations.seconds(1)); | ||||||
|  | 
 | ||||||
|  |         sparkContext = streamingContext.sparkContext(); | ||||||
|  | 
 | ||||||
|  |         streamingContext.checkpoint("./.checkpoint"); | ||||||
|  | 
 | ||||||
|  |         JavaInputDStream<ConsumerRecord<String, String>> messages = KafkaUtils.createDirectStream(streamingContext, LocationStrategies.PreferConsistent(), ConsumerStrategies.<String, String> Subscribe(topics, kafkaParams)); | ||||||
|  | 
 | ||||||
|  |         JavaPairDStream<String, String> results = messages.mapToPair(new PairFunction<ConsumerRecord<String, String>, String, String>() { | ||||||
|  |             @Override | ||||||
|  |             public Tuple2<String, String> call(ConsumerRecord<String, String> record) { | ||||||
|  |                 return new Tuple2<>(record.key(), record.value()); | ||||||
|  |             } | ||||||
|  |         }); | ||||||
|  | 
 | ||||||
|  |         JavaDStream<String> lines = results.map(new Function<Tuple2<String, String>, String>() { | ||||||
|  |             @Override | ||||||
|  |             public String call(Tuple2<String, String> tuple2) { | ||||||
|  |                 return tuple2._2(); | ||||||
|  |             } | ||||||
|  |         }); | ||||||
|  | 
 | ||||||
|  |         JavaDStream<String> words = lines.flatMap(new FlatMapFunction<String, String>() { | ||||||
|  |             @Override | ||||||
|  |             public Iterator<String> call(String x) { | ||||||
|  |                 return Arrays.asList(x.split("\\s+")) | ||||||
|  |                     .iterator(); | ||||||
|  |             } | ||||||
|  |         }); | ||||||
|  | 
 | ||||||
|  |         JavaPairDStream<String, Integer> wordCounts = words.mapToPair(new PairFunction<String, String, Integer>() { | ||||||
|  |             @Override | ||||||
|  |             public Tuple2<String, Integer> call(String s) { | ||||||
|  |                 return new Tuple2<>(s, 1); | ||||||
|  |             } | ||||||
|  |         }) | ||||||
|  |             .reduceByKey(new Function2<Integer, Integer, Integer>() { | ||||||
|  |                 @Override | ||||||
|  |                 public Integer call(Integer i1, Integer i2) { | ||||||
|  |                     return i1 + i2; | ||||||
|  |                 } | ||||||
|  |             }); | ||||||
|  | 
 | ||||||
|  |         Function3<String, Optional<Integer>, State<Integer>, Tuple2<String, Integer>> mappingFunc = (word, one, state) -> { | ||||||
|  |             int sum = one.orElse(0) + (state.exists() ? state.get() : 0); | ||||||
|  |             Tuple2<String, Integer> output = new Tuple2<>(word, sum); | ||||||
|  |             state.update(sum); | ||||||
|  |             return output; | ||||||
|  |         }; | ||||||
|  | 
 | ||||||
|  |         JavaPairRDD<String, Integer> initialRDD = JavaPairRDD.fromJavaRDD(sparkContext.emptyRDD()); | ||||||
|  | 
 | ||||||
|  |         JavaMapWithStateDStream<String, Integer, Integer, Tuple2<String, Integer>> cumulativeWordCounts = wordCounts.mapWithState(StateSpec.function(mappingFunc) | ||||||
|  |             .initialState(initialRDD)); | ||||||
|  | 
 | ||||||
|  |         cumulativeWordCounts.foreachRDD(new VoidFunction<JavaRDD<Tuple2<String, Integer>>>() { | ||||||
|  |             @Override | ||||||
|  |             public void call(JavaRDD<Tuple2<String, Integer>> javaRdd) throws Exception { | ||||||
|  |                 List<Tuple2<String, Integer>> wordCountList = javaRdd.collect(); | ||||||
|  |                 for (Tuple2<String, Integer> tuple : wordCountList) { | ||||||
|  |                     List<Word> words = Arrays.asList(new Word(tuple._1, tuple._2)); | ||||||
|  |                     JavaRDD<Word> rdd = sparkContext.parallelize(words); | ||||||
|  |                     javaFunctions(rdd).writerBuilder("vocabulary", "words", mapToRow(Word.class)) | ||||||
|  |                         .saveToCassandra(); | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |         }); | ||||||
|  | 
 | ||||||
|  |         streamingContext.start(); | ||||||
|  |         streamingContext.awaitTermination(); | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -2,4 +2,4 @@ | |||||||
| 
 | 
 | ||||||
| - [Java 11 Single File Source Code](https://www.baeldung.com/java-single-file-source-code) | - [Java 11 Single File Source Code](https://www.baeldung.com/java-single-file-source-code) | ||||||
| - [Java 11 Local Variable Syntax for Lambda Parameters](https://www.baeldung.com/java-var-lambda-params) | - [Java 11 Local Variable Syntax for Lambda Parameters](https://www.baeldung.com/java-var-lambda-params) | ||||||
| 
 | - [Java 11 String API Additions](https://www.baeldung.com/java-11-string-api) | ||||||
|  | |||||||
| @ -33,3 +33,8 @@ | |||||||
| - [Java Primitives versus Objects](https://www.baeldung.com/java-primitives-vs-objects) | - [Java Primitives versus Objects](https://www.baeldung.com/java-primitives-vs-objects) | ||||||
| - [How to Use if/else Logic in Java 8 Streams](https://www.baeldung.com/java-8-streams-if-else-logic) | - [How to Use if/else Logic in Java 8 Streams](https://www.baeldung.com/java-8-streams-if-else-logic) | ||||||
| - [How to Replace Many if Statements in Java](https://www.baeldung.com/java-replace-if-statements) | - [How to Replace Many if Statements in Java](https://www.baeldung.com/java-replace-if-statements) | ||||||
|  | - [Java @Override Annotation](https://www.baeldung.com/java-override) | ||||||
|  | - [Java @SuppressWarnings Annotation](https://www.baeldung.com/java-suppresswarnings) | ||||||
|  | - [Java @SafeVarargs Annotation](https://www.baeldung.com/java-safevarargs) | ||||||
|  | - [Java @Deprecated Annotation](https://www.baeldung.com/java-deprecated) | ||||||
|  | - [Java 8 Predicate Chain](https://www.baeldung.com/java-predicate-chain) | ||||||
|  | |||||||
| @ -0,0 +1,44 @@ | |||||||
|  | package com.baeldung.java9.set; | ||||||
|  | 
 | ||||||
|  | import com.google.common.collect.ImmutableSet; | ||||||
|  | 
 | ||||||
|  | import java.util.Collections; | ||||||
|  | import java.util.HashSet; | ||||||
|  | import java.util.Set; | ||||||
|  | 
 | ||||||
|  | public class UnmodifiableSet { | ||||||
|  | 
 | ||||||
|  |     public static void main(String[] args) { | ||||||
|  | 
 | ||||||
|  |         Set<String> set = new HashSet<>(); | ||||||
|  |         set.add("Canada"); | ||||||
|  |         set.add("USA"); | ||||||
|  | 
 | ||||||
|  |         coreJDK(set); | ||||||
|  |         guavaOf(); | ||||||
|  |         copyOf(set); | ||||||
|  |         java9Of(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     private static void java9Of() { | ||||||
|  |         Set<String> immutable = Set.of("Canada", "USA"); | ||||||
|  |         System.out.println(immutable); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     private static void guavaOf() { | ||||||
|  |         Set<String> immutable = ImmutableSet.of("Canada", "USA"); | ||||||
|  |         System.out.println(immutable); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     private static void copyOf(Set<String> set) { | ||||||
|  |         Set<String> immutable = ImmutableSet.copyOf(set); | ||||||
|  |         set.add("Costa Rica"); | ||||||
|  |         System.out.println(immutable); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     private static void coreJDK(Set<String> set) { | ||||||
|  |         Set<String> unmodifiableSet = Collections.unmodifiableSet(set); | ||||||
|  |         set.add("Costa Rica"); | ||||||
|  |         System.out.println(unmodifiableSet); | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -1,5 +1,7 @@ | |||||||
| package com.baeldung.java9; | package com.baeldung.java9; | ||||||
| 
 | 
 | ||||||
|  | import java.util.Collections; | ||||||
|  | import java.util.HashSet; | ||||||
| import java.util.Set; | import java.util.Set; | ||||||
| import org.junit.Test; | import org.junit.Test; | ||||||
| 
 | 
 | ||||||
| @ -23,4 +25,14 @@ public class SetExamplesUnitTest { | |||||||
|         Set<Integer> intSet = Set.of(intArray); |         Set<Integer> intSet = Set.of(intArray); | ||||||
|         assertEquals(intSet.size(), intArray.length); |         assertEquals(intSet.size(), intArray.length); | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|  |     @Test(expected = UnsupportedOperationException.class) | ||||||
|  |     public void testUnmodifiableSet() { | ||||||
|  |         Set<String> set = new HashSet<>(); | ||||||
|  |         set.add("Canada"); | ||||||
|  |         set.add("USA"); | ||||||
|  | 
 | ||||||
|  |         Set<String> unmodifiableSet = Collections.unmodifiableSet(set); | ||||||
|  |         unmodifiableSet.add("Costa Rica"); | ||||||
|  |     } | ||||||
| } | } | ||||||
|  | |||||||
| @ -13,3 +13,4 @@ | |||||||
| - [How to Invert an Array in Java](http://www.baeldung.com/java-invert-array) | - [How to Invert an Array in Java](http://www.baeldung.com/java-invert-array) | ||||||
| - [Array Operations in Java](http://www.baeldung.com/java-common-array-operations) | - [Array Operations in Java](http://www.baeldung.com/java-common-array-operations) | ||||||
| - [Intersection Between two Integer Arrays](https://www.baeldung.com/java-array-intersection) | - [Intersection Between two Integer Arrays](https://www.baeldung.com/java-array-intersection) | ||||||
|  | - [Sorting Arrays in Java](https://www.baeldung.com/java-sorting-arrays) | ||||||
|  | |||||||
| @ -24,4 +24,5 @@ | |||||||
| - [Java List Initialization in One Line](https://www.baeldung.com/java-init-list-one-line) | - [Java List Initialization in One Line](https://www.baeldung.com/java-init-list-one-line) | ||||||
| - [Ways to Iterate Over a List in Java](https://www.baeldung.com/java-iterate-list) | - [Ways to Iterate Over a List in Java](https://www.baeldung.com/java-iterate-list) | ||||||
| - [ClassCastException: Arrays$ArrayList cannot be cast to ArrayList](https://www.baeldung.com/java-classcastexception-arrays-arraylist) | - [ClassCastException: Arrays$ArrayList cannot be cast to ArrayList](https://www.baeldung.com/java-classcastexception-arrays-arraylist) | ||||||
| - [Flattening Nested Collections in Java](http://www.baeldung.com/java-flatten-nested-collections) | - [Flattening Nested Collections in Java](http://www.baeldung.com/java-flatten-nested-collections) | ||||||
|  | - [Intersection of Two Lists in Java](https://www.baeldung.com/java-lists-intersection) | ||||||
|  | |||||||
| @ -6,11 +6,11 @@ public class ArrayListOfArrayList { | |||||||
| 
 | 
 | ||||||
|     public static void main(String args[]) { |     public static void main(String args[]) { | ||||||
| 
 | 
 | ||||||
|         int numVertices = 3; |         int vertexCount = 3; | ||||||
|         ArrayList<ArrayList<Integer>> graph = new ArrayList<>(numVertices); |         ArrayList<ArrayList<Integer>> graph = new ArrayList<>(vertexCount); | ||||||
| 
 | 
 | ||||||
|         //Initializing each element of ArrayList with ArrayList |         //Initializing each element of ArrayList with ArrayList | ||||||
|         for(int i=0; i< numVertices; i++) { |         for(int i = 0; i< vertexCount; i++) { | ||||||
|             graph.add(new ArrayList<Integer>()); |             graph.add(new ArrayList<Integer>()); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
| @ -21,13 +21,14 @@ public class ArrayListOfArrayList { | |||||||
|         graph.get(1).add(0); |         graph.get(1).add(0); | ||||||
|         graph.get(2).add(1); |         graph.get(2).add(1); | ||||||
|         graph.get(0).add(2); |         graph.get(0).add(2); | ||||||
|        | 
 | ||||||
|         //Printing all the edges |         vertexCount = graph.size(); | ||||||
|         for(int vertexNo=0; vertexNo<numVertices; vertexNo++) { |         for(int i = 0; i < vertexCount; i++) { | ||||||
|             int edgeCount = graph.get(vertexNo).size(); |             int edgeCount = graph.get(i).size(); | ||||||
|             ArrayList<Integer> listOfVertices = graph.get(vertexNo); |             for(int j = 0; j < edgeCount; j++) { | ||||||
|             for(int i=0; i<edgeCount; i++) { |                 Integer startVertex = i; | ||||||
|                 System.out.println("Vertex "+vertexNo+" is connected to vetex "+listOfVertices.get(i)); |                 Integer endVertex = graph.get(i).get(j); | ||||||
|  |                 System.out.printf("Vertex %d is connected to vertex %d%n", startVertex, endVertex); | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  | |||||||
| @ -12,9 +12,9 @@ public class ThreeDimensionalArrayList { | |||||||
|         ArrayList< ArrayList< ArrayList<String> > > space = new ArrayList<>(x_axis_length); |         ArrayList< ArrayList< ArrayList<String> > > space = new ArrayList<>(x_axis_length); | ||||||
| 
 | 
 | ||||||
|         //Initializing each element of ArrayList with ArrayList< ArrayList<String> > |         //Initializing each element of ArrayList with ArrayList< ArrayList<String> > | ||||||
|         for(int i=0; i< x_axis_length; i++) { |         for(int i = 0; i < x_axis_length; i++) { | ||||||
|             space.add(new ArrayList< ArrayList<String> >(y_axis_length)); |             space.add(new ArrayList< ArrayList<String> >(y_axis_length)); | ||||||
|             for(int j =0; j< y_axis_length; j++) { |             for(int j = 0; j < y_axis_length; j++) { | ||||||
|                 space.get(i).add(new ArrayList<String>(z_axis_length)); |                 space.get(i).add(new ArrayList<String>(z_axis_length)); | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
| @ -33,9 +33,9 @@ public class ThreeDimensionalArrayList { | |||||||
|         space.get(1).get(1).add(1,"Yellow"); |         space.get(1).get(1).add(1,"Yellow"); | ||||||
| 
 | 
 | ||||||
|         //Printing colors for all the points |         //Printing colors for all the points | ||||||
|         for(int i=0; i<x_axis_length; i++) { |         for(int i = 0; i < x_axis_length; i++) { | ||||||
|             for(int j=0; j<y_axis_length; j++) { |             for(int j = 0; j < y_axis_length; j++) { | ||||||
|                 for(int k=0; k<z_axis_length; k++) { |                 for(int k = 0; k < z_axis_length; k++) { | ||||||
|                     System.out.println("Color of point ("+i+","+j+","+k+") is :"+space.get(i).get(j).get(k)); |                     System.out.println("Color of point ("+i+","+j+","+k+") is :"+space.get(i).get(j).get(k)); | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|  | |||||||
| @ -28,4 +28,6 @@ | |||||||
| - [Combining Different Types of Collections in Java](https://www.baeldung.com/java-combine-collections) | - [Combining Different Types of Collections in Java](https://www.baeldung.com/java-combine-collections) | ||||||
| - [Sorting in Java](http://www.baeldung.com/java-sorting) | - [Sorting in Java](http://www.baeldung.com/java-sorting) | ||||||
| - [Join and Split Arrays and Collections in Java](http://www.baeldung.com/java-join-and-split) | - [Join and Split Arrays and Collections in Java](http://www.baeldung.com/java-join-and-split) | ||||||
| - [A Guide to EnumMap](https://www.baeldung.com/java-enum-map) | - [A Guide to EnumMap](https://www.baeldung.com/java-enum-map) | ||||||
|  | - [A Guide to Iterator in Java](http://www.baeldung.com/java-iterator) | ||||||
|  | - [Differences Between HashMap and Hashtable](https://www.baeldung.com/hashmap-hashtable-differences) | ||||||
|  | |||||||
| @ -35,3 +35,5 @@ | |||||||
| - [Guide to Java OutputStream](https://www.baeldung.com/java-outputstream) | - [Guide to Java OutputStream](https://www.baeldung.com/java-outputstream) | ||||||
| - [Reading a CSV File into an Array](https://www.baeldung.com/java-csv-file-array) | - [Reading a CSV File into an Array](https://www.baeldung.com/java-csv-file-array) | ||||||
| - [Guide to BufferedReader](https://www.baeldung.com/java-buffered-reader) | - [Guide to BufferedReader](https://www.baeldung.com/java-buffered-reader) | ||||||
|  | - [How to Get the File Extension of a File in Java](http://www.baeldung.com/java-file-extension) | ||||||
|  | - [Getting a File’s Mime Type in Java](http://www.baeldung.com/java-file-mime-type) | ||||||
| @ -160,6 +160,17 @@ | |||||||
|             <version>${opencsv.version}</version> |             <version>${opencsv.version}</version> | ||||||
|             <scope>test</scope> |             <scope>test</scope> | ||||||
|         </dependency> |         </dependency> | ||||||
|  |         <!-- Mime Type Resolution Libraries --> | ||||||
|  |         <dependency> | ||||||
|  |             <groupId>org.apache.tika</groupId> | ||||||
|  |             <artifactId>tika-core</artifactId> | ||||||
|  |             <version>${tika.version}</version> | ||||||
|  |         </dependency> | ||||||
|  |         <dependency> | ||||||
|  |             <groupId>net.sf.jmimemagic</groupId> | ||||||
|  |             <artifactId>jmimemagic</artifactId> | ||||||
|  |             <version>${jmime-magic.version}</version> | ||||||
|  |         </dependency> | ||||||
|     </dependencies> |     </dependencies> | ||||||
| 
 | 
 | ||||||
|     <build> |     <build> | ||||||
| @ -264,6 +275,9 @@ | |||||||
|         <esapi.version>2.1.0.1</esapi.version> |         <esapi.version>2.1.0.1</esapi.version> | ||||||
|         <jmh-generator-annprocess.version>1.19</jmh-generator-annprocess.version> |         <jmh-generator-annprocess.version>1.19</jmh-generator-annprocess.version> | ||||||
|         <async-http-client.version>2.4.5</async-http-client.version> |         <async-http-client.version>2.4.5</async-http-client.version> | ||||||
|  |         <!-- Mime Type Libraries --> | ||||||
|  |         <tika.version>1.18</tika.version> | ||||||
|  |         <jmime-magic.version>0.1.5</jmime-magic.version> | ||||||
|     </properties> |     </properties> | ||||||
| 
 | 
 | ||||||
| </project> | </project> | ||||||
| Before Width: | Height: | Size: 54 KiB After Width: | Height: | Size: 54 KiB | 
| @ -33,3 +33,5 @@ | |||||||
| - [How to Separate Double into Integer and Decimal Parts](https://www.baeldung.com/java-separate-double-into-integer-decimal-parts) | - [How to Separate Double into Integer and Decimal Parts](https://www.baeldung.com/java-separate-double-into-integer-decimal-parts) | ||||||
| - [“Sneaky Throws” in Java](http://www.baeldung.com/java-sneaky-throws) | - [“Sneaky Throws” in Java](http://www.baeldung.com/java-sneaky-throws) | ||||||
| - [Retrieving a Class Name in Java](https://www.baeldung.com/java-class-name) | - [Retrieving a Class Name in Java](https://www.baeldung.com/java-class-name) | ||||||
|  | - [Java Compound Operators](https://www.baeldung.com/java-compound-operators) | ||||||
|  | - [Guide to Java Packages](https://www.baeldung.com/java-packages) | ||||||
							
								
								
									
										11
									
								
								core-java-security/README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								core-java-security/README.md
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,11 @@ | |||||||
|  | ## Core Java Security  | ||||||
|  | 
 | ||||||
|  | ### Relevant Articles:  | ||||||
|  | - [MD5 Hashing in Java](http://www.baeldung.com/java-md5) | ||||||
|  | - [Guide to the Cipher Class](http://www.baeldung.com/java-cipher-class) | ||||||
|  | - [Introduction to SSL in Java](http://www.baeldung.com/java-ssl) | ||||||
|  | - [Java KeyStore API](http://www.baeldung.com/java-keystore) | ||||||
|  | - [Encrypting and Decrypting Files in Java](http://www.baeldung.com/java-cipher-input-output-stream) | ||||||
|  | - [Hashing a Password in Java](https://www.baeldung.com/java-password-hashing) | ||||||
|  | - [SSL Handshake Failures](https://www.baeldung.com/java-ssl-handshake-failures) | ||||||
|  | - [SHA-256 Hashing in Java](https://www.baeldung.com/sha-256-hashing-java) | ||||||
							
								
								
									
										55
									
								
								core-java-security/pom.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										55
									
								
								core-java-security/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> | ||||||
|  |     <groupId>com.baeldung</groupId> | ||||||
|  |     <artifactId>core-java-security</artifactId> | ||||||
|  |     <version>0.1.0-SNAPSHOT</version> | ||||||
|  |     <packaging>jar</packaging> | ||||||
|  |     <name>core-java-security</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.apache.commons</groupId> | ||||||
|  |             <artifactId>commons-lang3</artifactId> | ||||||
|  |             <version>${commons-lang3.version}</version> | ||||||
|  |         </dependency> | ||||||
|  |         <!-- test scoped --> | ||||||
|  |         <dependency> | ||||||
|  |             <groupId>org.assertj</groupId> | ||||||
|  |             <artifactId>assertj-core</artifactId> | ||||||
|  |             <version>${assertj-core.version}</version> | ||||||
|  |             <scope>test</scope> | ||||||
|  |         </dependency> | ||||||
|  | 
 | ||||||
|  |         <dependency> | ||||||
|  |             <groupId>commons-codec</groupId> | ||||||
|  |             <artifactId>commons-codec</artifactId> | ||||||
|  |             <version>${commons-codec.version}</version> | ||||||
|  |         </dependency> | ||||||
|  |         <dependency> | ||||||
|  |             <groupId>org.bouncycastle</groupId> | ||||||
|  |             <artifactId>bcprov-jdk15on</artifactId> | ||||||
|  |             <version>${bouncycastle.version}</version> | ||||||
|  |         </dependency> | ||||||
|  |     </dependencies> | ||||||
|  | 
 | ||||||
|  |     <properties> | ||||||
|  | 
 | ||||||
|  |         <!-- util --> | ||||||
|  |         <commons-lang3.version>3.8.1</commons-lang3.version> | ||||||
|  |         <bouncycastle.version>1.55</bouncycastle.version> | ||||||
|  |         <commons-codec.version>1.10</commons-codec.version> | ||||||
|  | 
 | ||||||
|  |         <!-- testing --> | ||||||
|  |         <assertj-core.version>3.10.0</assertj-core.version> | ||||||
|  |        | ||||||
|  |     </properties> | ||||||
|  | 
 | ||||||
|  | </project> | ||||||
							
								
								
									
										19
									
								
								core-java-security/src/main/resources/logback.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								core-java-security/src/main/resources/logback.xml
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,19 @@ | |||||||
|  | <?xml version="1.0" encoding="UTF-8"?> | ||||||
|  | <configuration> | ||||||
|  |     <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> | ||||||
|  |         <encoder> | ||||||
|  |             <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n | ||||||
|  |             </pattern> | ||||||
|  |         </encoder> | ||||||
|  |     </appender> | ||||||
|  | 
 | ||||||
|  |     <logger name="org.springframework" level="WARN" /> | ||||||
|  |     <logger name="org.springframework.transaction" level="WARN" /> | ||||||
|  | 
 | ||||||
|  |     <!-- in order to debug some marshalling issues, this needs to be TRACE --> | ||||||
|  |     <logger name="org.springframework.web.servlet.mvc" level="WARN" /> | ||||||
|  | 
 | ||||||
|  |     <root level="INFO"> | ||||||
|  |         <appender-ref ref="STDOUT" /> | ||||||
|  |     </root> | ||||||
|  | </configuration> | ||||||
| @ -5,7 +5,6 @@ | |||||||
| ### Relevant Articles:  | ### Relevant Articles:  | ||||||
| - [Java Timer](http://www.baeldung.com/java-timer-and-timertask) | - [Java Timer](http://www.baeldung.com/java-timer-and-timertask) | ||||||
| - [How to Run a Shell Command in Java](http://www.baeldung.com/run-shell-command-in-java) | - [How to Run a Shell Command in Java](http://www.baeldung.com/run-shell-command-in-java) | ||||||
| - [MD5 Hashing in Java](http://www.baeldung.com/java-md5) |  | ||||||
| - [How to Print Screen in Java](http://www.baeldung.com/print-screen-in-java) | - [How to Print Screen in Java](http://www.baeldung.com/print-screen-in-java) | ||||||
| - [A Guide To Java Regular Expressions API](http://www.baeldung.com/regular-expressions-java) | - [A Guide To Java Regular Expressions API](http://www.baeldung.com/regular-expressions-java) | ||||||
| - [Getting Started with Java Properties](http://www.baeldung.com/java-properties) | - [Getting Started with Java Properties](http://www.baeldung.com/java-properties) | ||||||
| @ -23,44 +22,27 @@ | |||||||
| - [Difference between URL and URI](http://www.baeldung.com/java-url-vs-uri) | - [Difference between URL and URI](http://www.baeldung.com/java-url-vs-uri) | ||||||
| - [Creating a Java Compiler Plugin](http://www.baeldung.com/java-build-compiler-plugin) | - [Creating a Java Compiler Plugin](http://www.baeldung.com/java-build-compiler-plugin) | ||||||
| - [Quick Guide to Java Stack](http://www.baeldung.com/java-stack) | - [Quick Guide to Java Stack](http://www.baeldung.com/java-stack) | ||||||
| - [Guide to java.util.Formatter](http://www.baeldung.com/java-string-formatter) |  | ||||||
| - [Guide to the Cipher Class](http://www.baeldung.com/java-cipher-class) |  | ||||||
| - [Compiling Java *.class Files with javac](http://www.baeldung.com/javac) | - [Compiling Java *.class Files with javac](http://www.baeldung.com/javac) | ||||||
| - [A Guide to Iterator in Java](http://www.baeldung.com/java-iterator) |  | ||||||
| - [Introduction to Javadoc](http://www.baeldung.com/javadoc) | - [Introduction to Javadoc](http://www.baeldung.com/javadoc) | ||||||
| - [Guide to the Externalizable Interface in Java](http://www.baeldung.com/java-externalizable) | - [Guide to the Externalizable Interface in Java](http://www.baeldung.com/java-externalizable) | ||||||
| - [A Practical Guide to DecimalFormat](http://www.baeldung.com/java-decimalformat) |  | ||||||
| - [How to Detect the OS Using Java](http://www.baeldung.com/java-detect-os) | - [How to Detect the OS Using Java](http://www.baeldung.com/java-detect-os) | ||||||
| - [ASCII Art in Java](http://www.baeldung.com/ascii-art-in-java) | - [ASCII Art in Java](http://www.baeldung.com/ascii-art-in-java) | ||||||
| - [What is the serialVersionUID?](http://www.baeldung.com/java-serial-version-uid) | - [What is the serialVersionUID?](http://www.baeldung.com/java-serial-version-uid) | ||||||
| - [A Guide to the ResourceBundle](http://www.baeldung.com/java-resourcebundle) | - [A Guide to the ResourceBundle](http://www.baeldung.com/java-resourcebundle) | ||||||
| - [Class Loaders in Java](http://www.baeldung.com/java-classloaders) | - [Class Loaders in Java](http://www.baeldung.com/java-classloaders) | ||||||
| - [Introduction to SSL in Java](http://www.baeldung.com/java-ssl) |  | ||||||
| - [Java KeyStore API](http://www.baeldung.com/java-keystore) |  | ||||||
| - [Double-Checked Locking with Singleton](http://www.baeldung.com/java-singleton-double-checked-locking) | - [Double-Checked Locking with Singleton](http://www.baeldung.com/java-singleton-double-checked-locking) | ||||||
| - [Guide to Java Clock Class](http://www.baeldung.com/java-clock) | - [Guide to Java Clock Class](http://www.baeldung.com/java-clock) | ||||||
| - [Importance of Main Manifest Attribute in a Self-Executing JAR](http://www.baeldung.com/java-jar-executable-manifest-main-class) | - [Importance of Main Manifest Attribute in a Self-Executing JAR](http://www.baeldung.com/java-jar-executable-manifest-main-class) | ||||||
| - [How to Get the File Extension of a File in Java](http://www.baeldung.com/java-file-extension) |  | ||||||
| - [Java Global Exception Handler](http://www.baeldung.com/java-global-exception-handler) | - [Java Global Exception Handler](http://www.baeldung.com/java-global-exception-handler) | ||||||
| - [Encrypting and Decrypting Files in Java](http://www.baeldung.com/java-cipher-input-output-stream) |  | ||||||
| - [How to Get the Size of an Object in Java](http://www.baeldung.com/java-size-of-object) | - [How to Get the Size of an Object in Java](http://www.baeldung.com/java-size-of-object) | ||||||
| - [Guide to Java Instrumentation](http://www.baeldung.com/java-instrumentation) | - [Guide to Java Instrumentation](http://www.baeldung.com/java-instrumentation) | ||||||
| - [Getting a File’s Mime Type in Java](http://www.baeldung.com/java-file-mime-type) |  | ||||||
| - [Common Java Exceptions](http://www.baeldung.com/java-common-exceptions) | - [Common Java Exceptions](http://www.baeldung.com/java-common-exceptions) | ||||||
| - [Throw Exception in Optional in Java 8](https://www.baeldung.com/java-optional-throw-exception) | - [Throw Exception in Optional in Java 8](https://www.baeldung.com/java-optional-throw-exception) | ||||||
| - [Add a Character to a String at a Given Position](https://www.baeldung.com/java-add-character-to-string) |  | ||||||
| - [Calculating the nth Root in Java](https://www.baeldung.com/java-nth-root) |  | ||||||
| - [Convert Double to String, Removing Decimal Places](https://www.baeldung.com/java-double-to-string) |  | ||||||
| - [ZoneOffset in Java](https://www.baeldung.com/java-zone-offset) |  | ||||||
| - [Hashing a Password in Java](https://www.baeldung.com/java-password-hashing) |  | ||||||
| - [Merging java.util.Properties Objects](https://www.baeldung.com/java-merging-properties) | - [Merging java.util.Properties Objects](https://www.baeldung.com/java-merging-properties) | ||||||
| - [A Guide to SimpleDateFormat](https://www.baeldung.com/java-simple-date-format) | - [Merging java.util.Properties Objects](https://www.baeldung.com/java-merging-properties) | ||||||
| - [SSL Handshake Failures](https://www.baeldung.com/java-ssl-handshake-failures) |  | ||||||
| - [Changing the Order in a Sum Operation Can Produce Different Results?](https://www.baeldung.com/java-floating-point-sum-order) |  | ||||||
| - [Java – Try with Resources](https://www.baeldung.com/java-try-with-resources) | - [Java – Try with Resources](https://www.baeldung.com/java-try-with-resources) | ||||||
| - [Abstract Classes in Java](https://www.baeldung.com/java-abstract-class) | - [Abstract Classes in Java](https://www.baeldung.com/java-abstract-class) | ||||||
| - [Guide to Character Encoding](https://www.baeldung.com/java-char-encoding) | - [Guide to Character Encoding](https://www.baeldung.com/java-char-encoding) | ||||||
| - [Calculate the Area of a Circle in Java](https://www.baeldung.com/java-calculate-circle-area) |  | ||||||
| - [A Guide to the Java Math Class](https://www.baeldung.com/java-lang-math) |  | ||||||
| - [Graphs in Java](https://www.baeldung.com/java-graphs) | - [Graphs in Java](https://www.baeldung.com/java-graphs) | ||||||
| - [Console I/O in Java](http://www.baeldung.com/java-console-input-output) | - [Console I/O in Java](http://www.baeldung.com/java-console-input-output) | ||||||
|  | - [Formatting with printf() in Java](https://www.baeldung.com/java-printstream-printf) | ||||||
|  | |||||||
| @ -24,11 +24,6 @@ | |||||||
|             <artifactId>commons-lang3</artifactId> |             <artifactId>commons-lang3</artifactId> | ||||||
|             <version>${commons-lang3.version}</version> |             <version>${commons-lang3.version}</version> | ||||||
|         </dependency> |         </dependency> | ||||||
|         <dependency> |  | ||||||
|             <groupId>org.bouncycastle</groupId> |  | ||||||
|             <artifactId>bcprov-jdk15on</artifactId> |  | ||||||
|             <version>${bouncycastle.version}</version> |  | ||||||
|         </dependency> |  | ||||||
|         <dependency> |         <dependency> | ||||||
|             <groupId>org.unix4j</groupId> |             <groupId>org.unix4j</groupId> | ||||||
|             <artifactId>unix4j-command</artifactId> |             <artifactId>unix4j-command</artifactId> | ||||||
| @ -75,12 +70,6 @@ | |||||||
|             <version>${assertj-core.version}</version> |             <version>${assertj-core.version}</version> | ||||||
|             <scope>test</scope> |             <scope>test</scope> | ||||||
|         </dependency> |         </dependency> | ||||||
| 
 |  | ||||||
|         <dependency> |  | ||||||
|             <groupId>commons-codec</groupId> |  | ||||||
|             <artifactId>commons-codec</artifactId> |  | ||||||
|             <version>${commons-codec.version}</version> |  | ||||||
|         </dependency> |  | ||||||
|         <dependency> |         <dependency> | ||||||
|             <groupId>org.javamoney</groupId> |             <groupId>org.javamoney</groupId> | ||||||
|             <artifactId>moneta</artifactId> |             <artifactId>moneta</artifactId> | ||||||
| @ -126,17 +115,6 @@ | |||||||
|             <artifactId>h2</artifactId> |             <artifactId>h2</artifactId> | ||||||
|             <version>${h2database.version}</version> |             <version>${h2database.version}</version> | ||||||
|         </dependency> |         </dependency> | ||||||
|         <!-- Mime Type Resolution Libraries --> |  | ||||||
|         <dependency> |  | ||||||
|             <groupId>org.apache.tika</groupId> |  | ||||||
|             <artifactId>tika-core</artifactId> |  | ||||||
|             <version>${tika.version}</version> |  | ||||||
|         </dependency> |  | ||||||
|         <dependency> |  | ||||||
|             <groupId>net.sf.jmimemagic</groupId> |  | ||||||
|             <artifactId>jmimemagic</artifactId> |  | ||||||
|             <version>${jmime-magic.version}</version> |  | ||||||
|         </dependency> |  | ||||||
|         <!-- instrumentation --> |         <!-- instrumentation --> | ||||||
|         <dependency> |         <dependency> | ||||||
|             <groupId>org.javassist</groupId> |             <groupId>org.javassist</groupId> | ||||||
| @ -477,8 +455,6 @@ | |||||||
| 
 | 
 | ||||||
|         <!-- util --> |         <!-- util --> | ||||||
|         <commons-lang3.version>3.5</commons-lang3.version> |         <commons-lang3.version>3.5</commons-lang3.version> | ||||||
|         <bouncycastle.version>1.55</bouncycastle.version> |  | ||||||
|         <commons-codec.version>1.10</commons-codec.version> |  | ||||||
|         <commons-io.version>2.5</commons-io.version> |         <commons-io.version>2.5</commons-io.version> | ||||||
|         <commons-math3.version>3.6.1</commons-math3.version> |         <commons-math3.version>3.6.1</commons-math3.version> | ||||||
|         <decimal4j.version>1.0.3</decimal4j.version> |         <decimal4j.version>1.0.3</decimal4j.version> | ||||||
| @ -509,9 +485,6 @@ | |||||||
|         <spring-boot-maven-plugin.version>2.0.3.RELEASE</spring-boot-maven-plugin.version> |         <spring-boot-maven-plugin.version>2.0.3.RELEASE</spring-boot-maven-plugin.version> | ||||||
|         <exec-maven-plugin.version>1.6.0</exec-maven-plugin.version> |         <exec-maven-plugin.version>1.6.0</exec-maven-plugin.version> | ||||||
|         <icu4j.version>61.1</icu4j.version> |         <icu4j.version>61.1</icu4j.version> | ||||||
|         <!-- Mime Type Libraries --> |  | ||||||
|         <tika.version>1.18</tika.version> |  | ||||||
|         <jmime-magic.version>0.1.5</jmime-magic.version> |  | ||||||
|         <!-- instrumentation --> |         <!-- instrumentation --> | ||||||
|         <javaassist.version>3.21.0-GA</javaassist.version> |         <javaassist.version>3.21.0-GA</javaassist.version> | ||||||
|          |          | ||||||
|  | |||||||
| @ -0,0 +1,29 @@ | |||||||
|  | package com.baeldung.curltojava; | ||||||
|  | 
 | ||||||
|  | import java.io.BufferedInputStream; | ||||||
|  | import java.io.IOException; | ||||||
|  | import java.io.InputStream; | ||||||
|  | import java.util.logging.Level; | ||||||
|  | import java.util.logging.Logger; | ||||||
|  | 
 | ||||||
|  | public class JavaCurlExamples { | ||||||
|  | 
 | ||||||
|  |     public static String inputStreamToString(InputStream inputStream) { | ||||||
|  |         final int bufferSize = 8 * 1024; | ||||||
|  |         byte[] buffer = new byte[bufferSize]; | ||||||
|  |         final StringBuilder builder = new StringBuilder(); | ||||||
|  |         try (BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream, bufferSize)) { | ||||||
|  |             while (bufferedInputStream.read(buffer) != -1) { | ||||||
|  |                 builder.append(new String(buffer)); | ||||||
|  |             } | ||||||
|  |         } catch (IOException ex) { | ||||||
|  |             Logger.getLogger(JavaCurlExamples.class.getName()).log(Level.SEVERE, null, ex); | ||||||
|  |         } | ||||||
|  |         return builder.toString(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public static void consumeInputStream(InputStream inputStream) { | ||||||
|  |         inputStreamToString(inputStream); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | } | ||||||
| @ -0,0 +1,55 @@ | |||||||
|  | package com.baeldung.curltojava; | ||||||
|  | 
 | ||||||
|  | import java.io.File; | ||||||
|  | import java.io.IOException; | ||||||
|  | import java.io.InputStream; | ||||||
|  | import java.net.HttpURLConnection; | ||||||
|  | import java.net.URL; | ||||||
|  | 
 | ||||||
|  | import org.junit.Assert; | ||||||
|  | import org.junit.Test; | ||||||
|  | 
 | ||||||
|  | public class JavaCurlExamplesUnitTest { | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     public void givenCommand_whenCalled_thenProduceZeroExitCode() throws IOException { | ||||||
|  |         String command = "curl --location --request GET \"https://postman-echo.com/get?foo1=bar1&foo2=bar2\""; | ||||||
|  |         ProcessBuilder processBuilder = new ProcessBuilder(command.replaceAll("\"", "").split(" ")); | ||||||
|  |         processBuilder.directory(new File("/home/")); | ||||||
|  |         Process process = processBuilder.start(); | ||||||
|  |         InputStream inputStream = process.getInputStream(); | ||||||
|  |         // Consume the inputStream so the process can exit | ||||||
|  |         JavaCurlExamples.consumeInputStream(inputStream); | ||||||
|  |         int exitCode = process.exitValue(); | ||||||
|  |          | ||||||
|  |         Assert.assertEquals(0, exitCode); | ||||||
|  |     } | ||||||
|  |      | ||||||
|  |     @Test | ||||||
|  |     public void givenNewCommands_whenCalled_thenCheckIfIsAlive() throws IOException { | ||||||
|  |         String command = "curl --location --request GET \"https://postman-echo.com/get?foo1=bar1&foo2=bar2\""; | ||||||
|  |         ProcessBuilder processBuilder = new ProcessBuilder(command.replaceAll("\"", "").split(" ")); | ||||||
|  |         processBuilder.directory(new File("/home/")); | ||||||
|  |         Process process = processBuilder.start(); | ||||||
|  |          | ||||||
|  |         // Re-use processBuilder | ||||||
|  |         processBuilder.command(new String[]{"newCommand", "arguments"}); | ||||||
|  |          | ||||||
|  |         Assert.assertEquals(true, process.isAlive()); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     public void whenRequestGet_thenReturnSuccessResponseCode() throws IOException { | ||||||
|  |         String url = "https://postman-echo.com/get?foo1=bar1&foo2=bar2"; | ||||||
|  |         URL urlObj = new URL(url); | ||||||
|  |         HttpURLConnection connection = (HttpURLConnection) urlObj.openConnection(); | ||||||
|  |         connection.setDoOutput(true); | ||||||
|  |         connection.setInstanceFollowRedirects(false); | ||||||
|  |         connection.setRequestMethod("GET"); | ||||||
|  |         connection.connect(); | ||||||
|  |          | ||||||
|  |         Assert.assertEquals(HttpURLConnection.HTTP_OK, connection.getResponseCode()); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | } | ||||||
| @ -1,83 +0,0 @@ | |||||||
| package com.baeldung.string; |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| import org.apache.commons.lang3.StringUtils; |  | ||||||
| import org.junit.Test; |  | ||||||
| 
 |  | ||||||
| import static org.junit.Assert.assertFalse; |  | ||||||
| import static org.junit.Assert.assertTrue; |  | ||||||
| 
 |  | ||||||
| public class StringReplaceAndRemoveUnitTest { |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
|     @Test |  | ||||||
|     public void givenTestStrings_whenReplace_thenProcessedString() { |  | ||||||
| 
 |  | ||||||
|         String master = "Hello World Baeldung!"; |  | ||||||
|         String target = "Baeldung"; |  | ||||||
|         String replacement = "Java"; |  | ||||||
|         String processed = master.replace(target, replacement); |  | ||||||
|         assertTrue(processed.contains(replacement)); |  | ||||||
|         assertFalse(processed.contains(target)); |  | ||||||
| 
 |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     @Test |  | ||||||
|     public void givenTestStrings_whenReplaceAll_thenProcessedString() { |  | ||||||
| 
 |  | ||||||
|         String master2 = "Welcome to Baeldung, Hello World Baeldung"; |  | ||||||
|         String regexTarget= "(Baeldung)$"; |  | ||||||
|         String replacement = "Java"; |  | ||||||
|         String processed2 = master2.replaceAll(regexTarget, replacement); |  | ||||||
|         assertTrue(processed2.endsWith("Java")); |  | ||||||
| 
 |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     @Test |  | ||||||
|     public void givenTestStrings_whenStringBuilderMethods_thenProcessedString() { |  | ||||||
| 
 |  | ||||||
|         String master = "Hello World Baeldung!"; |  | ||||||
|         String target = "Baeldung"; |  | ||||||
|         String replacement = "Java"; |  | ||||||
| 
 |  | ||||||
|         int startIndex = master.indexOf(target); |  | ||||||
|         int stopIndex = startIndex + target.length(); |  | ||||||
| 
 |  | ||||||
|         StringBuilder builder = new StringBuilder(master); |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
|         builder.delete(startIndex, stopIndex); |  | ||||||
|         assertFalse(builder.toString().contains(target)); |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
|         builder.replace(startIndex, stopIndex, replacement); |  | ||||||
|         assertTrue(builder.toString().contains(replacement)); |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
|     @Test |  | ||||||
|     public void givenTestStrings_whenStringUtilsMethods_thenProcessedStrings() { |  | ||||||
| 
 |  | ||||||
|         String master = "Hello World Baeldung!"; |  | ||||||
|         String target = "Baeldung"; |  | ||||||
|         String replacement = "Java"; |  | ||||||
| 
 |  | ||||||
|         String processed = StringUtils.replace(master, target, replacement); |  | ||||||
|         assertTrue(processed.contains(replacement)); |  | ||||||
| 
 |  | ||||||
|         String master2 = "Hello World Baeldung!"; |  | ||||||
|         String target2 = "baeldung"; |  | ||||||
|         String processed2 = StringUtils.replaceIgnoreCase(master2, target2, replacement); |  | ||||||
|         assertFalse(processed2.contains(target)); |  | ||||||
| 
 |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| } |  | ||||||
| @ -47,3 +47,7 @@ | |||||||
| - [Dependency Injection for Kotlin with Injekt](https://www.baeldung.com/kotlin-dependency-injection-with-injekt) | - [Dependency Injection for Kotlin with Injekt](https://www.baeldung.com/kotlin-dependency-injection-with-injekt) | ||||||
| - [Implementing a Binary Tree in Kotlin](https://www.baeldung.com/kotlin-binary-tree) | - [Implementing a Binary Tree in Kotlin](https://www.baeldung.com/kotlin-binary-tree) | ||||||
| - [Generate a Random Alphanumeric String in Kotlin](https://www.baeldung.com/kotlin-random-alphanumeric-string) | - [Generate a Random Alphanumeric String in Kotlin](https://www.baeldung.com/kotlin-random-alphanumeric-string) | ||||||
|  | - [Kotlin Contracts](https://www.baeldung.com/kotlin-contracts) | ||||||
|  | - [Operator Overloading in Kotlin](https://www.baeldung.com/kotlin-operator-overloading) | ||||||
|  | - [Inline Classes in Kotlin](https://www.baeldung.com/kotlin-inline-classes) | ||||||
|  | - [Creating Java static final Equivalents in Kotlin](https://www.baeldung.com/kotlin-java-static-final) | ||||||
|  | |||||||
| @ -9,3 +9,4 @@ | |||||||
| - [Exclude Fields from Serialization in Gson](http://www.baeldung.com/gson-exclude-fields-serialization) | - [Exclude Fields from Serialization in Gson](http://www.baeldung.com/gson-exclude-fields-serialization) | ||||||
| - [Save Data to a JSON File with Gson](https://www.baeldung.com/gson-save-file) | - [Save Data to a JSON File with Gson](https://www.baeldung.com/gson-save-file) | ||||||
| - [Convert JSON to a Map Using Gson](https://www.baeldung.com/gson-json-to-map) | - [Convert JSON to a Map Using Gson](https://www.baeldung.com/gson-json-to-map) | ||||||
|  | - [Working with Primitive Values in Gson](https://www.baeldung.com/java-gson-primitives) | ||||||
|  | |||||||
| @ -18,4 +18,3 @@ | |||||||
| - [Hamcrest Text Matchers](http://www.baeldung.com/hamcrest-text-matchers) | - [Hamcrest Text Matchers](http://www.baeldung.com/hamcrest-text-matchers) | ||||||
| - [Quick Guide to the Guava RateLimiter](http://www.baeldung.com/guava-rate-limiter) | - [Quick Guide to the Guava RateLimiter](http://www.baeldung.com/guava-rate-limiter) | ||||||
| - [Hamcrest File Matchers](https://www.baeldung.com/hamcrest-file-matchers) | - [Hamcrest File Matchers](https://www.baeldung.com/hamcrest-file-matchers) | ||||||
| - [SHA-256 Hashing in Java](https://www.baeldung.com/sha-256-hashing-java) |  | ||||||
|  | |||||||
| @ -17,3 +17,5 @@ | |||||||
| - [Finding the Highest Value in a Java Map](https://www.baeldung.com/java-find-map-max) | - [Finding the Highest Value in a Java Map](https://www.baeldung.com/java-find-map-max) | ||||||
| - [Merging Two Maps with Java 8](https://www.baeldung.com/java-merge-maps) | - [Merging Two Maps with Java 8](https://www.baeldung.com/java-merge-maps) | ||||||
| - [How to Check If a Key Exists in a Map](https://www.baeldung.com/java-map-key-exists) | - [How to Check If a Key Exists in a Map](https://www.baeldung.com/java-map-key-exists) | ||||||
|  | - [Comparing Two HashMaps in Java](https://www.baeldung.com/java-compare-hashmaps) | ||||||
|  | - [Immutable Map Implementations in Java](https://www.baeldung.com/java-immutable-maps)  | ||||||
|  | |||||||
| @ -26,3 +26,5 @@ | |||||||
| - [Format ZonedDateTime to String](https://www.baeldung.com/java-format-zoned-datetime-string) | - [Format ZonedDateTime to String](https://www.baeldung.com/java-format-zoned-datetime-string) | ||||||
| - [Convert Between java.time.Instant and java.sql.Timestamp](https://www.baeldung.com/java-time-instant-to-java-sql-timestamp) | - [Convert Between java.time.Instant and java.sql.Timestamp](https://www.baeldung.com/java-time-instant-to-java-sql-timestamp) | ||||||
| - [Convert between String and Timestamp](https://www.baeldung.com/java-string-to-timestamp) | - [Convert between String and Timestamp](https://www.baeldung.com/java-string-to-timestamp) | ||||||
|  | - [A Guide to SimpleDateFormat](https://www.baeldung.com/java-simple-date-format) | ||||||
|  | - [ZoneOffset in Java](https://www.baeldung.com/java-zone-offset) | ||||||
| @ -8,6 +8,8 @@ import java.time.Duration; | |||||||
| import java.time.LocalDate; | import java.time.LocalDate; | ||||||
| import java.time.LocalDateTime; | import java.time.LocalDateTime; | ||||||
| import java.time.Period; | import java.time.Period; | ||||||
|  | import java.time.ZoneId; | ||||||
|  | import java.time.ZonedDateTime; | ||||||
| import java.time.temporal.ChronoUnit; | import java.time.temporal.ChronoUnit; | ||||||
| import java.util.Date; | import java.util.Date; | ||||||
| import java.util.Locale; | import java.util.Locale; | ||||||
|  | |||||||
| @ -13,3 +13,9 @@ | |||||||
| - [Find All Pairs of Numbers in an Array That Add Up to a Given Sum](http://www.baeldung.com/java-algorithm-number-pairs-sum) | - [Find All Pairs of Numbers in an Array That Add Up to a Given Sum](http://www.baeldung.com/java-algorithm-number-pairs-sum) | ||||||
| - [Java – Random Long, Float, Integer and Double](http://www.baeldung.com/java-generate-random-long-float-integer-double) | - [Java – Random Long, Float, Integer and Double](http://www.baeldung.com/java-generate-random-long-float-integer-double) | ||||||
| - [Using Math.sin with Degrees](https://www.baeldung.com/java-math-sin-degrees) | - [Using Math.sin with Degrees](https://www.baeldung.com/java-math-sin-degrees) | ||||||
|  | - [A Practical Guide to DecimalFormat](http://www.baeldung.com/java-decimalformat) | ||||||
|  | - [Calculating the nth Root in Java](https://www.baeldung.com/java-nth-root) | ||||||
|  | - [Convert Double to String, Removing Decimal Places](https://www.baeldung.com/java-double-to-string) | ||||||
|  | - [Changing the Order in a Sum Operation Can Produce Different Results?](https://www.baeldung.com/java-floating-point-sum-order) | ||||||
|  | - [Calculate the Area of a Circle in Java](https://www.baeldung.com/java-calculate-circle-area) | ||||||
|  | - [A Guide to the Java Math Class](https://www.baeldung.com/java-lang-math) | ||||||
| @ -14,3 +14,4 @@ | |||||||
| - [Primitive Type Streams in Java 8](http://www.baeldung.com/java-8-primitive-streams) | - [Primitive Type Streams in Java 8](http://www.baeldung.com/java-8-primitive-streams) | ||||||
| - [Stream Ordering in Java](https://www.baeldung.com/java-stream-ordering) | - [Stream Ordering in Java](https://www.baeldung.com/java-stream-ordering) | ||||||
| - [Introduction to Protonpack](https://www.baeldung.com/java-protonpack) | - [Introduction to Protonpack](https://www.baeldung.com/java-protonpack) | ||||||
|  | - [Java Stream Filter with Lambda Expression](https://www.baeldung.com/java-stream-filter-lambda) | ||||||
|  | |||||||
| @ -45,3 +45,7 @@ | |||||||
| - [Remove or Replace part of a String in Java](https://www.baeldung.com/java-remove-replace-string-part) | - [Remove or Replace part of a String in Java](https://www.baeldung.com/java-remove-replace-string-part) | ||||||
| - [Replace a Character at a Specific Index in a String in Java](https://www.baeldung.com/java-replace-character-at-index) | - [Replace a Character at a Specific Index in a String in Java](https://www.baeldung.com/java-replace-character-at-index) | ||||||
| - [Convert a Comma Separated String to a List in Java](https://www.baeldung.com/java-string-with-separator-to-list) | - [Convert a Comma Separated String to a List in Java](https://www.baeldung.com/java-string-with-separator-to-list) | ||||||
|  | - [Guide to java.util.Formatter](http://www.baeldung.com/java-string-formatter) | ||||||
|  | - [Add a Character to a String at a Given Position](https://www.baeldung.com/java-add-character-to-string) | ||||||
|  | - [Remove Leading and Trailing Characters from a String](https://www.baeldung.com/java-remove-trailing-characters) | ||||||
|  | - [Concatenating Strings In Java](https://www.baeldung.com/java-strings-concatenation) | ||||||
|  | |||||||
| @ -1,7 +1,6 @@ | |||||||
| package com.baeldung.string.formatter; | package com.baeldung.string.formatter; | ||||||
| 
 | 
 | ||||||
| import org.junit.BeforeClass; | import static org.junit.Assert.assertEquals; | ||||||
| import org.junit.Test; |  | ||||||
| 
 | 
 | ||||||
| import java.text.DateFormat; | import java.text.DateFormat; | ||||||
| import java.text.SimpleDateFormat; | import java.text.SimpleDateFormat; | ||||||
| @ -11,9 +10,11 @@ import java.time.ZoneId; | |||||||
| import java.time.format.DateTimeFormatter; | import java.time.format.DateTimeFormatter; | ||||||
| import java.util.Calendar; | import java.util.Calendar; | ||||||
| import java.util.Date; | import java.util.Date; | ||||||
|  | import java.util.Locale; | ||||||
| import java.util.TimeZone; | import java.util.TimeZone; | ||||||
| 
 | 
 | ||||||
| import static org.junit.Assert.assertEquals; | import org.junit.BeforeClass; | ||||||
|  | import org.junit.Test; | ||||||
| 
 | 
 | ||||||
| public class DateToStringFormatterUnitTest { | public class DateToStringFormatterUnitTest { | ||||||
| 
 | 
 | ||||||
| @ -40,7 +41,7 @@ public class DateToStringFormatterUnitTest { | |||||||
|     @Test |     @Test | ||||||
|     public void whenDateConvertedUsingDateFormatToString_thenCorrect() { |     public void whenDateConvertedUsingDateFormatToString_thenCorrect() { | ||||||
|         String formattedDate = DateFormat |         String formattedDate = DateFormat | ||||||
|           .getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT) |           .getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT, Locale.US) | ||||||
|           .format(date); |           .format(date); | ||||||
| 
 | 
 | ||||||
|         assertEquals(EXPECTED_STRING_DATE, formattedDate); |         assertEquals(EXPECTED_STRING_DATE, formattedDate); | ||||||
|  | |||||||
| @ -0,0 +1,19 @@ | |||||||
|  | package com.baeldung.string.interview; | ||||||
|  | 
 | ||||||
|  | import java.math.BigDecimal; | ||||||
|  | import java.text.NumberFormat; | ||||||
|  | import java.util.Locale; | ||||||
|  | import static org.junit.Assert.assertEquals; | ||||||
|  | 
 | ||||||
|  | import org.junit.Test; | ||||||
|  | 
 | ||||||
|  | public class LocaleUnitTest { | ||||||
|  |     @Test | ||||||
|  |     public void whenUsingLocal_thenCorrectResultsForDifferentLocale() { | ||||||
|  |         Locale usLocale = Locale.US; | ||||||
|  |         BigDecimal number = new BigDecimal(102_300.456d); | ||||||
|  |           | ||||||
|  |         NumberFormat usNumberFormat = NumberFormat.getCurrencyInstance(usLocale);  | ||||||
|  |         assertEquals(usNumberFormat.format(number), "$102,300.46"); | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -0,0 +1,29 @@ | |||||||
|  | package com.baeldung.string.interview; | ||||||
|  | 
 | ||||||
|  | import static org.assertj.core.api.Assertions.assertThat; | ||||||
|  | 
 | ||||||
|  | import java.util.Arrays; | ||||||
|  | 
 | ||||||
|  | import org.junit.Test; | ||||||
|  | 
 | ||||||
|  | public class StringAnagramUnitTest { | ||||||
|  |     public boolean isAnagram(String s1, String s2) { | ||||||
|  |         if(s1.length() != s2.length()) | ||||||
|  |             return false; | ||||||
|  |              | ||||||
|  |         char[] arr1 = s1.toCharArray(); | ||||||
|  |         char[] arr2 = s2.toCharArray(); | ||||||
|  |          | ||||||
|  |         Arrays.sort(arr1); | ||||||
|  |         Arrays.sort(arr2); | ||||||
|  |          | ||||||
|  |         return Arrays.equals(arr1, arr2); | ||||||
|  |     } | ||||||
|  |      | ||||||
|  |     @Test | ||||||
|  |     public void whenTestAnagrams_thenTestingCorrectly() { | ||||||
|  |         assertThat(isAnagram("car", "arc")).isTrue(); | ||||||
|  |         assertThat(isAnagram("west", "stew")).isTrue(); | ||||||
|  |         assertThat(isAnagram("west", "east")).isFalse(); | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -0,0 +1,20 @@ | |||||||
|  | package com.baeldung.string.interview; | ||||||
|  | 
 | ||||||
|  | import static org.junit.Assert.assertEquals; | ||||||
|  | 
 | ||||||
|  | import org.junit.Test; | ||||||
|  | 
 | ||||||
|  | public class StringChangeCaseUnitTest { | ||||||
|  |     @Test | ||||||
|  |     public void givenString_whenChangingToUppercase_thenCaseChanged() { | ||||||
|  |         String s = "Welcome to Baeldung!"; | ||||||
|  |         assertEquals("WELCOME TO BAELDUNG!", s.toUpperCase()); | ||||||
|  |     } | ||||||
|  |      | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     public void givenString_whenChangingToLowerrcase_thenCaseChanged() { | ||||||
|  |         String s = "Welcome to Baeldung!"; | ||||||
|  |         assertEquals("welcome to baeldung!", s.toLowerCase()); | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -0,0 +1,28 @@ | |||||||
|  | package com.baeldung.string.interview; | ||||||
|  | 
 | ||||||
|  | import static org.junit.Assert.assertEquals; | ||||||
|  | 
 | ||||||
|  | import org.junit.Test; | ||||||
|  | 
 | ||||||
|  | public class StringCountOccurrencesUnitTest { | ||||||
|  |     public int countOccurrences(String s, char c) { | ||||||
|  |         int count = 0; | ||||||
|  |         for (int i = 0; i < s.length(); i++) { | ||||||
|  |             if (s.charAt(i) == c) { | ||||||
|  |                 count++; | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |         return count; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     public void givenString_whenCountingFrequencyOfChar_thenCountCorrect() { | ||||||
|  |         assertEquals(3, countOccurrences("united states", 't')); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public void givenString_whenUsingJava8_thenCountingOfCharCorrect() { | ||||||
|  |         String str = "united states"; | ||||||
|  |         long count = str.chars().filter(ch -> (char)ch == 't').count(); | ||||||
|  |         assertEquals(3, count); | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -0,0 +1,14 @@ | |||||||
|  | package com.baeldung.string.interview; | ||||||
|  | 
 | ||||||
|  | import static org.junit.Assert.assertEquals; | ||||||
|  | 
 | ||||||
|  | import org.junit.Test; | ||||||
|  | 
 | ||||||
|  | public class StringFormatUnitTest { | ||||||
|  |     @Test | ||||||
|  |     public void givenString_whenUsingStringFormat_thenStringFormatted() { | ||||||
|  |         String title = "Baeldung";  | ||||||
|  |         String formatted = String.format("Title is %s", title); | ||||||
|  |         assertEquals(formatted, "Title is Baeldung"); | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -0,0 +1,17 @@ | |||||||
|  | package com.baeldung.string.interview; | ||||||
|  | 
 | ||||||
|  | import static org.assertj.core.api.Assertions.assertThat; | ||||||
|  | 
 | ||||||
|  | import org.junit.Test; | ||||||
|  | 
 | ||||||
|  | public class StringInternUnitTest { | ||||||
|  |     @Test | ||||||
|  |     public void whenCallingStringIntern_thenStringsInterned() { | ||||||
|  |         String s1 = "Baeldung"; | ||||||
|  |         String s2 = new String("Baeldung"); | ||||||
|  |         String s3 = new String("Baeldung").intern(); | ||||||
|  |           | ||||||
|  |         assertThat(s1 == s2).isFalse(); | ||||||
|  |         assertThat(s1 == s3).isTrue(); | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -0,0 +1,18 @@ | |||||||
|  | package com.baeldung.string.interview; | ||||||
|  | 
 | ||||||
|  | import java.util.StringJoiner; | ||||||
|  | import static org.junit.Assert.assertEquals; | ||||||
|  | 
 | ||||||
|  | import org.junit.Test; | ||||||
|  | 
 | ||||||
|  | public class StringJoinerUnitTest { | ||||||
|  |     @Test | ||||||
|  |     public void whenUsingStringJoiner_thenStringsJoined() { | ||||||
|  |         StringJoiner joiner = new StringJoiner(",", "[", "]"); | ||||||
|  |         joiner.add("Red") | ||||||
|  |           .add("Green") | ||||||
|  |           .add("Blue"); | ||||||
|  |           | ||||||
|  |         assertEquals(joiner.toString(), "[Red,Green,Blue]"); | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -0,0 +1,29 @@ | |||||||
|  | package com.baeldung.string.interview; | ||||||
|  | 
 | ||||||
|  | import static org.assertj.core.api.Assertions.assertThat; | ||||||
|  | 
 | ||||||
|  | import org.junit.Test; | ||||||
|  | 
 | ||||||
|  | public class StringPalindromeUnitTest { | ||||||
|  |      | ||||||
|  |     public boolean isPalindrome(String text) { | ||||||
|  |         int forward = 0; | ||||||
|  |         int backward = text.length() - 1; | ||||||
|  |         while (backward > forward) { | ||||||
|  |             char forwardChar = text.charAt(forward++); | ||||||
|  |             char backwardChar = text.charAt(backward--); | ||||||
|  |             if (forwardChar != backwardChar) | ||||||
|  |                 return false; | ||||||
|  |         } | ||||||
|  |         return true; | ||||||
|  |     } | ||||||
|  |      | ||||||
|  |     @Test | ||||||
|  |     public void givenIsPalindromeMethod_whenCheckingString_thenFindIfPalindrome() { | ||||||
|  |         assertThat(isPalindrome("madam")).isTrue(); | ||||||
|  |         assertThat(isPalindrome("radar")).isTrue(); | ||||||
|  |         assertThat(isPalindrome("level")).isTrue(); | ||||||
|  | 
 | ||||||
|  |         assertThat(isPalindrome("baeldung")).isFalse(); | ||||||
|  |     } | ||||||
|  | } | ||||||
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