Merge branch 'master' into BAEL-18235-v3

This commit is contained in:
Loredana Crusoveanu
2019-10-14 22:35:23 +03:00
committed by GitHub
154 changed files with 1372 additions and 373 deletions

2
.gitignore vendored
View File

@@ -30,7 +30,7 @@ out/
.DS_Store .DS_Store
# Maven # Maven
log/ log/*
target/ target/
# Gradle # Gradle

View File

@@ -14,4 +14,6 @@ This module contains articles about algorithms. Some classes of algorithms, e.g.
- [A Guide to the Folding Technique in Java](https://www.baeldung.com/folding-hashing-technique) - [A Guide to the Folding Technique in Java](https://www.baeldung.com/folding-hashing-technique)
- [Creating a Triangle with for Loops in Java](https://www.baeldung.com/java-print-triangle) - [Creating a Triangle with for Loops in Java](https://www.baeldung.com/java-print-triangle)
- [Efficient Word Frequency Calculator in Java](https://www.baeldung.com/java-word-frequency) - [Efficient Word Frequency Calculator in Java](https://www.baeldung.com/java-word-frequency)
- More articles: [[<-- prev]](/algorithms-miscellaneous-2) [[next -->]](/algorithms-miscellaneous-4) - [Interpolation Search in Java](https://www.baeldung.com/java-interpolation-search)
- [The K-Means Clustering Algorithm in Java](https://www.baeldung.com/java-k-means-clustering-algorithm)
- More articles: [[<-- prev]](/algorithms-miscellaneous-2) [[next -->]](/algorithms-miscellaneous-4)

View File

@@ -0,0 +1,54 @@
package com.baeldung.algorithms.breadthfirstsearch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.*;
public class BreadthFirstSearchAlgorithm {
private static final Logger LOGGER = LoggerFactory.getLogger(BreadthFirstSearchAlgorithm.class);
public static <T> Optional<Tree<T>> search(T value, Tree<T> root) {
Queue<Tree<T>> queue = new ArrayDeque<>();
queue.add(root);
Tree<T> currentNode;
while (!queue.isEmpty()) {
currentNode = queue.remove();
LOGGER.info("Visited node with value: {}", currentNode.getValue());
if (currentNode.getValue().equals(value)) {
return Optional.of(currentNode);
} else {
queue.addAll(currentNode.getChildren());
}
}
return Optional.empty();
}
public static <T> Optional<Node<T>> search(T value, Node<T> start) {
Queue<Node<T>> queue = new ArrayDeque<>();
queue.add(start);
Node<T> currentNode;
Set<Node<T>> alreadyVisited = new HashSet<>();
while (!queue.isEmpty()) {
currentNode = queue.remove();
LOGGER.info("Visited node with value: {}", currentNode.getValue());
if (currentNode.getValue().equals(value)) {
return Optional.of(currentNode);
} else {
alreadyVisited.add(currentNode);
queue.addAll(currentNode.getNeighbors());
queue.removeAll(alreadyVisited);
}
}
return Optional.empty();
}
}

View File

@@ -0,0 +1,31 @@
package com.baeldung.algorithms.breadthfirstsearch;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
public class Node<T> {
private T value;
private Set<Node<T>> neighbors;
public Node(T value) {
this.value = value;
this.neighbors = new HashSet<>();
}
public T getValue() {
return value;
}
public Set<Node<T>> getNeighbors() {
return Collections.unmodifiableSet(neighbors);
}
public void connect(Node<T> node) {
if (this == node) throw new IllegalArgumentException("Can't connect node to itself");
this.neighbors.add(node);
node.neighbors.add(this);
}
}

View File

@@ -0,0 +1,34 @@
package com.baeldung.algorithms.breadthfirstsearch;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Tree<T> {
private T value;
private List<Tree<T>> children;
private Tree(T value) {
this.value = value;
this.children = new ArrayList<>();
}
public static <T> Tree<T> of(T value) {
return new Tree<>(value);
}
public T getValue() {
return value;
}
public List<Tree<T>> getChildren() {
return Collections.unmodifiableList(children);
}
public Tree<T> addChild(T value) {
Tree<T> newChild = new Tree<>(value);
children.add(newChild);
return newChild;
}
}

View File

@@ -0,0 +1,86 @@
package com.baeldung.algorithms.breadthfirstsearch;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
class BreadthFirstSearchAlgorithmUnitTest {
private Tree<Integer> root;
private Tree<Integer> rootFirstChild;
private Tree<Integer> depthMostChild;
private Tree<Integer> rootSecondChild;
private Node<Integer> start;
private Node<Integer> firstNeighbor;
private Node<Integer> firstNeighborNeighbor;
private Node<Integer> secondNeighbor;
@Test
void givenTree_whenSearchTen_thenRoot() {
initTree();
assertThat(BreadthFirstSearchAlgorithm.search(10, root)).isPresent().contains(root);
}
@Test
void givenTree_whenSearchThree_thenDepthMostValue() {
initTree();
assertThat(BreadthFirstSearchAlgorithm.search(3, root)).isPresent().contains(depthMostChild);
}
@Test
void givenTree_whenSearchFour_thenRootSecondChild() {
initTree();
assertThat(BreadthFirstSearchAlgorithm.search(4, root)).isPresent().contains(rootSecondChild);
}
@Test
void givenTree_whenSearchFive_thenNotFound() {
initTree();
assertThat(BreadthFirstSearchAlgorithm.search(5, root)).isEmpty();
}
private void initTree() {
root = Tree.of(10);
rootFirstChild = root.addChild(2);
depthMostChild = rootFirstChild.addChild(3);
rootSecondChild = root.addChild(4);
}
@Test
void givenNode_whenSearchTen_thenStart() {
initNode();
assertThat(BreadthFirstSearchAlgorithm.search(10, firstNeighborNeighbor)).isPresent().contains(start);
}
@Test
void givenNode_whenSearchThree_thenNeighborNeighbor() {
initNode();
assertThat(BreadthFirstSearchAlgorithm.search(3, firstNeighborNeighbor)).isPresent().contains(firstNeighborNeighbor);
}
@Test
void givenNode_whenSearchFour_thenSecondNeighbor() {
initNode();
assertThat(BreadthFirstSearchAlgorithm.search(4, firstNeighborNeighbor)).isPresent().contains(secondNeighbor);
}
@Test
void givenNode_whenSearchFive_thenNotFound() {
initNode();
assertThat(BreadthFirstSearchAlgorithm.search(5, firstNeighborNeighbor)).isEmpty();
}
private void initNode() {
start = new Node<>(10);
firstNeighbor = new Node<>(2);
start.connect(firstNeighbor);
firstNeighborNeighbor = new Node<>(3);
firstNeighbor.connect(firstNeighborNeighbor);
firstNeighborNeighbor.connect(start);
secondNeighbor = new Node<>(4);
start.connect(secondNeighbor);
}
}

View File

@@ -12,3 +12,7 @@ This module contains articles about sorting algorithms.
- [Shell Sort in Java](https://www.baeldung.com/java-shell-sort) - [Shell Sort in Java](https://www.baeldung.com/java-shell-sort)
- [Counting Sort in Java](https://www.baeldung.com/java-counting-sort) - [Counting Sort in Java](https://www.baeldung.com/java-counting-sort)
- [Sorting Strings by Contained Numbers in Java](https://www.baeldung.com/java-sort-strings-contained-numbers) - [Sorting Strings by Contained Numbers in Java](https://www.baeldung.com/java-sort-strings-contained-numbers)
- [How an In-Place Sorting Algorithm Works](https://www.baeldung.com/java-in-place-sorting)
- [Selection Sort in Java](https://www.baeldung.com/java-selection-sort)
- [Sorting Strings by Contained Numbers in Java](https://www.baeldung.com/java-sort-strings-contained-numbers)
- [Radix Sort in Java](https://www.baeldung.com/java-radix-sort)

View File

@@ -1,7 +1,6 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <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/maven-v4_0_0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>animal-sniffer-mvn-plugin</artifactId> <artifactId>animal-sniffer-mvn-plugin</artifactId>
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
<name>animal-sniffer-mvn-plugin</name> <name>animal-sniffer-mvn-plugin</name>

View File

@@ -14,12 +14,6 @@
</parent> </parent>
<dependencies> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>org.slf4j</groupId> <groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId> <artifactId>slf4j-simple</artifactId>
@@ -46,15 +40,6 @@
<build> <build>
<plugins> <plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${compiler-plugin.version}</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin> <plugin>
<groupId>org.apache.avro</groupId> <groupId>org.apache.avro</groupId>
<artifactId>avro-maven-plugin</artifactId> <artifactId>avro-maven-plugin</artifactId>
@@ -79,8 +64,6 @@
</build> </build>
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<compiler-plugin.version>3.5</compiler-plugin.version>
<avro.version>1.8.2</avro.version> <avro.version>1.8.2</avro.version>
<slf4j.version>1.7.25</slf4j.version> <slf4j.version>1.7.25</slf4j.version>
</properties> </properties>

View File

@@ -1,7 +1,6 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <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"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>apache-fop</artifactId> <artifactId>apache-fop</artifactId>
<version>0.1-SNAPSHOT</version> <version>0.1-SNAPSHOT</version>
<name>apache-fop</name> <name>apache-fop</name>

View File

@@ -19,25 +19,7 @@
<artifactId>geode-core</artifactId> <artifactId>geode-core</artifactId>
<version>${geode.core}</version> <version>${geode.core}</version>
</dependency> </dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
</dependencies> </dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
<properties> <properties>
<geode.core>1.6.0</geode.core> <geode.core>1.6.0</geode.core>

View File

@@ -1,7 +1,6 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <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"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>apache-meecrowave</artifactId> <artifactId>apache-meecrowave</artifactId>
<version>0.0.1</version> <version>0.0.1</version>
<name>apache-meecrowave</name> <name>apache-meecrowave</name>
@@ -38,13 +37,6 @@
<version>${meecrowave-junit.version}</version> <version>${meecrowave-junit.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@@ -5,7 +5,6 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>org.baeldung.examples.olingo2</groupId> <groupId>org.baeldung.examples.olingo2</groupId>
<artifactId>olingo2</artifactId> <artifactId>olingo2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>olingo2</name> <name>olingo2</name>
<description>Sample Olingo 2 Project</description> <description>Sample Olingo 2 Project</description>

View File

@@ -24,8 +24,6 @@
</dependencies> </dependencies>
<properties> <properties>
<maven.compiler.target>1.8</maven.compiler.target> <pulsar-client.version>2.1.1-incubating</pulsar-client.version>
<maven.compiler.source>1.8</maven.compiler.source>
<pulsar-client.version>2.1.1-incubating</pulsar-client.version>
</properties> </properties>
</project> </project>

View File

@@ -4,4 +4,6 @@ This module contains articles about Apache Shiro
### Relevant articles: ### Relevant articles:
- [Introduction to Apache Shiro](https://www.baeldung.com/apache-shiro) - [Introduction to Apache Shiro](https://www.baeldung.com/apache-shiro)
- [Permissions-Based Access Control with Apache Shiro](https://www.baeldung.com/apache-shiro-access-control)

View File

@@ -1,7 +1,6 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <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"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>apache-solrj</artifactId> <artifactId>apache-solrj</artifactId>
<version>0.0.1-SNAPSHOT</version> <version>0.0.1-SNAPSHOT</version>
<name>apache-solrj</name> <name>apache-solrj</name>

View File

@@ -6,3 +6,5 @@ This module contains articles about Apache Spark
- [Introduction to Apache Spark](https://www.baeldung.com/apache-spark) - [Introduction to Apache Spark](https://www.baeldung.com/apache-spark)
- [Building a Data Pipeline with Kafka, Spark Streaming and Cassandra](https://www.baeldung.com/kafka-spark-data-pipeline) - [Building a Data Pipeline with Kafka, Spark Streaming and Cassandra](https://www.baeldung.com/kafka-spark-data-pipeline)
- [Machine Learning with Spark MLlib](https://www.baeldung.com/spark-mlib-machine-learning)

View File

@@ -2,7 +2,6 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>apache-spark</artifactId> <artifactId>apache-spark</artifactId>
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
<name>apache-spark</name> <name>apache-spark</name>
@@ -59,15 +58,6 @@
<build> <build>
<plugins> <plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin> <plugin>
<artifactId>maven-assembly-plugin</artifactId> <artifactId>maven-assembly-plugin</artifactId>
<executions> <executions>
@@ -95,7 +85,6 @@
<org.apache.spark.spark-streaming-kafka.version>2.3.0</org.apache.spark.spark-streaming-kafka.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.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> <com.datastax.spark.spark-cassandra-connector-java.version>1.5.2</com.datastax.spark.spark-cassandra-connector-java.version>
<maven-compiler-plugin.version>3.2</maven-compiler-plugin.version>
</properties> </properties>
</project> </project>

View File

@@ -1,7 +1,6 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <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/maven-v4_0_0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<version>0.1-SNAPSHOT</version> <version>0.1-SNAPSHOT</version>
<artifactId>apache-velocity</artifactId> <artifactId>apache-velocity</artifactId>
<name>apache-velocity</name> <name>apache-velocity</name>

View File

@@ -2,7 +2,6 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <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"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>aws-lambda</artifactId> <artifactId>aws-lambda</artifactId>
<version>0.1.0-SNAPSHOT</version> <version>0.1.0-SNAPSHOT</version>
<name>aws-lambda</name> <name>aws-lambda</name>
@@ -88,7 +87,6 @@
<properties> <properties>
<json-simple.version>1.1.1</json-simple.version> <json-simple.version>1.1.1</json-simple.version>
<org.json.version>20180130</org.json.version>
<commons-io.version>2.5</commons-io.version> <commons-io.version>2.5</commons-io.version>
<aws-lambda-java-events.version>1.3.0</aws-lambda-java-events.version> <aws-lambda-java-events.version>1.3.0</aws-lambda-java-events.version>
<aws-lambda-java-core.version>1.2.0</aws-lambda-java-core.version> <aws-lambda-java-core.version>1.2.0</aws-lambda-java-core.version>

View File

@@ -1,7 +1,6 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <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"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>aws</artifactId> <artifactId>aws</artifactId>
<version>0.1.0-SNAPSHOT</version> <version>0.1.0-SNAPSHOT</version>
<name>aws</name> <name>aws</name>

View File

@@ -30,7 +30,6 @@
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId> <artifactId>spring-boot-autoconfigure</artifactId>
<version>${spring-boot.version}</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>

View File

@@ -2,7 +2,6 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <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"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>azure</artifactId> <artifactId>azure</artifactId>
<version>0.1</version> <version>0.1</version>
<name>azure</name> <name>azure</name>

View File

@@ -11,4 +11,5 @@ This module contains articles about core Groovy concepts
- [Integrating Groovy into Java Applications](https://www.baeldung.com/groovy-java-applications) - [Integrating Groovy into Java Applications](https://www.baeldung.com/groovy-java-applications)
- [Concatenate Strings with Groovy](https://www.baeldung.com/groovy-concatenate-strings) - [Concatenate Strings with Groovy](https://www.baeldung.com/groovy-concatenate-strings)
- [Metaprogramming in Groovy](https://www.baeldung.com/groovy-metaprogramming) - [Metaprogramming in Groovy](https://www.baeldung.com/groovy-metaprogramming)
- [[<-- Prev]](/core-groovy) - [A Quick Guide to Working with Web Services in Groovy](https://www.baeldung.com/groovy-web-services)
- [[<-- Prev]](/core-groovy)

View File

@@ -7,4 +7,6 @@ This module contains articles about the Java ArrayList collection
- [Guide to the Java ArrayList](http://www.baeldung.com/java-arraylist) - [Guide to the Java ArrayList](http://www.baeldung.com/java-arraylist)
- [Add Multiple Items to an Java ArrayList](http://www.baeldung.com/java-add-items-array-list) - [Add Multiple Items to an Java ArrayList](http://www.baeldung.com/java-add-items-array-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)
- [Multi Dimensional ArrayList in Java](https://www.baeldung.com/java-multi-dimensional-arraylist) - [Multi Dimensional ArrayList in Java](https://www.baeldung.com/java-multi-dimensional-arraylist)
- [Removing an Element From an ArrayList](https://www.baeldung.com/java-arraylist-remove-element)

View File

@@ -0,0 +1,19 @@
=========
## Core Java Concurrency Advanced Examples
This module contains articles about advanced topics about multithreading with core Java.
### Relevant Articles:
- [Semaphores in Java](https://www.baeldung.com/java-semaphore)
- [Daemon Threads in Java](https://www.baeldung.com/java-daemon-thread)
- [Priority-based Job Scheduling in Java](https://www.baeldung.com/java-priority-job-schedule)
- [Brief Introduction to Java Thread.yield()](https://www.baeldung.com/java-thread-yield)
- [Print Even and Odd Numbers Using 2 Threads](https://www.baeldung.com/java-even-odd-numbers-with-2-threads)
- [Java CyclicBarrier vs CountDownLatch](https://www.baeldung.com/java-cyclicbarrier-countdownlatch)
- [Guide to the Fork/Join Framework in Java](https://www.baeldung.com/java-fork-join)
- [Guide to ThreadLocalRandom in Java](https://www.baeldung.com/java-thread-local-random)
- [The Thread.join() Method in Java](https://www.baeldung.com/java-thread-join)
- [Passing Parameters to Java Threads](https://www.baeldung.com/java-thread-parameters)
[[<-- previous]](/core-java-modules/core-java-concurrency-advanced)[[next -->]](/core-java-modules/core-java-concurrency-advanced-3)

View File

@@ -0,0 +1,59 @@
<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-concurrency-advanced-2</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>core-java-concurrency-advanced-2</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-java</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-java</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>${jmh-core.version}</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>${jmh-generator-annprocess.version}</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>core-java-concurrency-advanced-2</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<properties>
<jmh-core.version>1.19</jmh-core.version>
<jmh-generator-annprocess.version>1.19</jmh-generator-annprocess.version>
<assertj.version>3.6.1</assertj.version>
</properties>
</project>

View File

@@ -4,6 +4,7 @@ import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier; import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
public class CyclicBarrierResetExample { public class CyclicBarrierResetExample {
@@ -36,6 +37,11 @@ public class CyclicBarrierResetExample {
}); });
} }
es.shutdown(); es.shutdown();
try {
es.awaitTermination(1, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
return updateCount.get(); return updateCount.get();
} }

View File

@@ -0,0 +1,23 @@
package com.baeldung.concurrent.daemon;
public class NewThread extends Thread {
public void run() {
long startTime = System.currentTimeMillis();
while (true) {
for (int i = 0; i < 10; i++) {
System.out.println(this.getName() + ": New Thread is running..." + i);
try {
//Wait for one sec so it doesn't print too fast
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// prevent the Thread to run forever. It will finish it's execution after 2 seconds
if (System.currentTimeMillis() - startTime > 2000) {
Thread.currentThread().interrupt();
break;
}
}
}
}

View File

@@ -1,4 +1,4 @@
package com.baeldung.concurrent.parameter; package com.baeldung.concurrent.parameters;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import java.util.stream.IntStream; import java.util.stream.IntStream;

View File

@@ -1,4 +1,4 @@
package com.baeldung.concurrent.parameter; package com.baeldung.concurrent.parameters;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import java.util.stream.IntStream; import java.util.stream.IntStream;

View File

@@ -1,14 +1,13 @@
package com.baeldung.parameters; package com.baeldung.concurrent.parameters;
import com.baeldung.concurrent.parameter.AverageCalculator; import static org.junit.Assert.assertEquals;
import org.junit.Test;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import java.util.stream.IntStream; import java.util.stream.IntStream;
import static org.junit.Assert.assertEquals; import org.junit.Test;
public class ParameterizedThreadUnitTest { public class ParameterizedThreadUnitTest {

View File

@@ -1,4 +1,4 @@
package com.baeldung.java8; package com.baeldung.forkjoin;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;

View File

@@ -0,0 +1,9 @@
=========
## Core Java Concurrency Advanced Examples
This module contains articles about advanced topics about multithreading with core Java.
### Relevant Articles:
[[<-- previous]](/core-java-modules/core-java-concurrency-advanced-2)

View File

@@ -0,0 +1,33 @@
<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-concurrency-advanced-3</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>core-java-concurrency-advanced-3</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-java</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-java</relativePath>
</parent>
<dependencies>
</dependencies>
<build>
<finalName>core-java-concurrency-advanced-3</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<properties>
</properties>
</project>

View File

@@ -1,26 +0,0 @@
*.class
0.*
#folders#
/target
/neoDb*
/data
/src/main/webapp/WEB-INF/classes
*/META-INF/*
.resourceCache
# Packaged files #
*.jar
*.war
*.ear
# Files generated by integration tests
*.txt
backup-pom.xml
/bin/
/temp
#IntelliJ specific
.idea/
*.iml

View File

@@ -1,25 +1,16 @@
## Core Java Concurrency Advanced Examples ## Core Java Concurrency Advanced Examples
This module contains articles about advanced concurrency in core Java. This module contains articles about advanced topics about multithreading with core Java.
### Relevant Articles: ### Relevant Articles:
- [Introduction to Thread Pools in Java](http://www.baeldung.com/thread-pool-java-and-guava) - [Introduction to Thread Pools in Java](https://www.baeldung.com/thread-pool-java-and-guava)
- [Guide to CountDownLatch in Java](http://www.baeldung.com/java-countdown-latch) - [Guide to CountDownLatch in Java](https://www.baeldung.com/java-countdown-latch)
- [Guide to java.util.concurrent.Locks](http://www.baeldung.com/java-concurrent-locks) - [Guide to java.util.concurrent.Locks](https://www.baeldung.com/java-concurrent-locks)
- [An Introduction to ThreadLocal in Java](http://www.baeldung.com/java-threadlocal) - [An Introduction to ThreadLocal in Java](https://www.baeldung.com/java-threadlocal)
- [LongAdder and LongAccumulator in Java](http://www.baeldung.com/java-longadder-and-longaccumulator) - [LongAdder and LongAccumulator in Java](https://www.baeldung.com/java-longadder-and-longaccumulator)
- [The Dining Philosophers Problem in Java](http://www.baeldung.com/java-dining-philoshophers) - [The Dining Philosophers Problem in Java](https://www.baeldung.com/java-dining-philoshophers)
- [Guide to the Java Phaser](http://www.baeldung.com/java-phaser) - [Guide to the Java Phaser](https://www.baeldung.com/java-phaser)
- [An Introduction to Atomic Variables in Java](http://www.baeldung.com/java-atomic-variables) - [An Introduction to Atomic Variables in Java](https://www.baeldung.com/java-atomic-variables)
- [CyclicBarrier in Java](http://www.baeldung.com/java-cyclic-barrier) - [CyclicBarrier in Java](https://www.baeldung.com/java-cyclic-barrier)
- [Guide to the Volatile Keyword in Java](http://www.baeldung.com/java-volatile) - [Guide to the Volatile Keyword in Java](https://www.baeldung.com/java-volatile)
- [Semaphores in Java](http://www.baeldung.com/java-semaphore) - More Articles: [[next -->]](/core-java-modules/core-java-concurrency-advanced-2)
- [Daemon Threads in Java](http://www.baeldung.com/java-daemon-thread)
- [Priority-based Job Scheduling in Java](http://www.baeldung.com/java-priority-job-schedule)
- [Brief Introduction to Java Thread.yield()](https://www.baeldung.com/java-thread-yield)
- [Print Even and Odd Numbers Using 2 Threads](https://www.baeldung.com/java-even-odd-numbers-with-2-threads)
- [Java CyclicBarrier vs CountDownLatch](https://www.baeldung.com/java-cyclicbarrier-countdownlatch)
- [Guide to the Fork/Join Framework in Java](http://www.baeldung.com/java-fork-join)
- [Guide to ThreadLocalRandom in Java](http://www.baeldung.com/java-thread-local-random)
- [The Thread.join() Method in Java](http://www.baeldung.com/java-thread-join)
- [Passing Parameters to Java Threads](https://www.baeldung.com/java-thread-parameters)

View File

@@ -47,16 +47,6 @@
<version>${avaitility.version}</version> <version>${avaitility.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>${jmh-core.version}</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>${jmh-generator-annprocess.version}</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
@@ -78,8 +68,6 @@
<!-- testing --> <!-- testing -->
<assertj.version>3.6.1</assertj.version> <assertj.version>3.6.1</assertj.version>
<avaitility.version>1.7.0</avaitility.version> <avaitility.version>1.7.0</avaitility.version>
<jmh-core.version>1.19</jmh-core.version>
<jmh-generator-annprocess.version>1.19</jmh-generator-annprocess.version>
</properties> </properties>
</project> </project>

View File

@@ -4,6 +4,7 @@ import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier; import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
public class CyclicBarrierCompletionMethodExample { public class CyclicBarrierCompletionMethodExample {
@@ -35,6 +36,11 @@ public class CyclicBarrierCompletionMethodExample {
}); });
} }
es.shutdown(); es.shutdown();
try {
es.awaitTermination(1, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
return updateCount.get(); return updateCount.get();
} }

View File

@@ -11,3 +11,5 @@ This module contains articles about core java exceptions
- [Difference Between Throw and Throws in Java](https://www.baeldung.com/java-throw-throws) - [Difference Between Throw and Throws in Java](https://www.baeldung.com/java-throw-throws)
- [“Sneaky Throws” in Java](https://www.baeldung.com/java-sneaky-throws) - [“Sneaky Throws” in Java](https://www.baeldung.com/java-sneaky-throws)
- [The StackOverflowError in Java](https://www.baeldung.com/java-stack-overflow-error) - [The StackOverflowError in Java](https://www.baeldung.com/java-stack-overflow-error)
- [Checked and Unchecked Exceptions in Java](https://www.baeldung.com/java-checked-unchecked-exceptions)

View File

@@ -0,0 +1,6 @@
### Relevant Articles:
- [Create a File in a Specific Directory in Java](https://www.baeldung.com/java-create-file-in-directory)
- [A Guide to the Java FileReader Class](https://www.baeldung.com/java-filereader)

View File

@@ -41,4 +41,5 @@ This module contains articles about core Java input and output (IO)
- [List Files in a Directory in Java](https://www.baeldung.com/java-list-directory-files) - [List Files in a Directory in Java](https://www.baeldung.com/java-list-directory-files)
- [Java InputStream to Byte Array and ByteBuffer](https://www.baeldung.com/convert-input-stream-to-array-of-bytes) - [Java InputStream to Byte Array and ByteBuffer](https://www.baeldung.com/convert-input-stream-to-array-of-bytes)
- [Introduction to the Java NIO Selector](https://www.baeldung.com/java-nio-selector) - [Introduction to the Java NIO Selector](https://www.baeldung.com/java-nio-selector)
- [How to Avoid the Java FileNotFoundException When Loading Resources](https://www.baeldung.com/java-classpath-resource-cannot-be-opened)
- [[More -->]](/core-java-modules/core-java-io-2) - [[More -->]](/core-java-modules/core-java-io-2)

View File

@@ -0,0 +1,4 @@
### Relevant Articles:
- [Java Naming and Directory Interface Overview](https://www.baeldung.com/jndi)

View File

@@ -4,3 +4,5 @@
### Relevant Articles: ### Relevant Articles:
- [Method Inlining in the JVM](https://www.baeldung.com/jvm-method-inlining) - [Method Inlining in the JVM](https://www.baeldung.com/jvm-method-inlining)
- [A Guide to System.exit()](https://www.baeldung.com/java-system-exit)
- [Guide to System.gc()](https://www.baeldung.com/java-system-gc)

View File

@@ -4,4 +4,5 @@ This module contains articles about core features in the Java language
### Relevant Articles: ### Relevant Articles:
- [Java Primitives versus Objects](https://www.baeldung.com/java-primitives-vs-objects) - [Java Primitives versus Objects](https://www.baeldung.com/java-primitives-vs-objects)
- [[<-- Prev]](/core-java-modules/core-java-lang) - [Command-Line Arguments in Java](https://www.baeldung.com/java-command-line-arguments)
- [[<-- Prev]](/core-java-modules/core-java-lang)

View File

@@ -14,4 +14,5 @@ This module contains articles about Object-oriented programming (OOP) in Java
- [A Guide to Constructors in Java](https://www.baeldung.com/java-constructors) - [A Guide to Constructors in Java](https://www.baeldung.com/java-constructors)
- [Composition, Aggregation, and Association in Java](https://www.baeldung.com/java-composition-aggregation-association) - [Composition, Aggregation, and Association in Java](https://www.baeldung.com/java-composition-aggregation-association)
- [Static and Default Methods in Interfaces in Java](https://www.baeldung.com/java-static-default-methods) - [Static and Default Methods in Interfaces in Java](https://www.baeldung.com/java-static-default-methods)
- [[<-- Prev]](/core-java-modules/core-java-lang-oop)[[More -->]](/core-java-modules/core-java-lang-oop-3) - [Java Copy Constructor](https://www.baeldung.com/java-copy-constructor)
- [[<-- Prev]](/core-java-modules/core-java-lang-oop)[[More -->]](/core-java-modules/core-java-lang-oop-3)

View File

@@ -25,7 +25,7 @@ public class DeepCopyUnitTest {
} }
@Test @Test
public void whenModifyingOriginalObject_thenConstructorCopyShouldNotChange() { public void whenModifyingOriginalObject_thenCopyShouldNotChange() {
Address address = new Address("Downing St 10", "London", "England"); Address address = new Address("Downing St 10", "London", "England");
User pm = new User("Prime", "Minister", address); User pm = new User("Prime", "Minister", address);
User deepCopy = new User(pm); User deepCopy = new User(pm);
@@ -125,4 +125,13 @@ public class DeepCopyUnitTest {
end = System.currentTimeMillis(); end = System.currentTimeMillis();
System.out.println("Cloning with Jackson took " + (end - start) + " milliseconds."); System.out.println("Cloning with Jackson took " + (end - start) + " milliseconds.");
} }
@Test
public void whenModifyingOriginalObject_ThenCopyShouldChange() {
Address address = new Address("Downing St 10", "London", "England");
User pm = new User("Prime", "Minister", address);
User shallowCopy = new User(pm.getFirstName(), pm.getLastName(), pm.getAddress());
address.setCountry("Great Britain");
assertThat(shallowCopy.getAddress().getCountry()).isEqualTo(pm.getAddress().getCountry());
}
} }

View File

@@ -5,3 +5,5 @@
- [Java instanceof Operator](https://www.baeldung.com/java-instanceof) - [Java instanceof Operator](https://www.baeldung.com/java-instanceof)
- [A Guide to Increment and Decrement Unary Operators in Java](https://www.baeldung.com/java-unary-operators) - [A Guide to Increment and Decrement Unary Operators in Java](https://www.baeldung.com/java-unary-operators)
- [Java Compound Operators](https://www.baeldung.com/java-compound-operators) - [Java Compound Operators](https://www.baeldung.com/java-compound-operators)
- [The XOR Operator in Java](https://www.baeldung.com/java-xor-operator)

View File

@@ -35,4 +35,11 @@ public class Generics {
return list; return list;
} }
public <T> List<T> genericMethod(List<T> list) {
return list.stream().collect(Collectors.toList());
}
public List<Object> withErasure(List<Object> list) {
return list.stream().collect(Collectors.toList());
}
} }

View File

@@ -5,3 +5,4 @@ This module contains articles about core Java non-blocking input and output (IO)
## Relevant Articles: ## Relevant Articles:
- [Determine File Creation Date in Java](https://www.baeldung.com/java-file-creation-date) - [Determine File Creation Date in Java](https://www.baeldung.com/java-file-creation-date)
- [Find the Number of Lines in a File Using Jav](https://www.baeldung.com/java-file-number-of-lines)

View File

@@ -14,3 +14,5 @@ This module contains articles about core Java Security
- [Enabling TLS v1.2 in Java 7](https://www.baeldung.com/java-7-tls-v12) - [Enabling TLS v1.2 in Java 7](https://www.baeldung.com/java-7-tls-v12)
- [The Java SecureRandom Class](https://www.baeldung.com/java-secure-random) - [The Java SecureRandom Class](https://www.baeldung.com/java-secure-random)
- [An Introduction to Java SASL](https://www.baeldung.com/java-sasl) - [An Introduction to Java SASL](https://www.baeldung.com/java-sasl)
- [A Guide to Java GSS API](https://www.baeldung.com/java-gss)

View File

@@ -0,0 +1,9 @@
## Java String Conversions
This module contains articles about string conversions from/to another type.
### Relevant Articles:
- [Java String Conversions](https://www.baeldung.com/java-string-conversions)
- [Convert String to Byte Array and Reverse in Java](https://www.baeldung.com/java-string-to-byte-array)
- [Convert Char Array to String](https://www.baeldung.com/java-char-array-to-string)
- More articles: [[<-- prev]](/core-java-string-conversions)

View File

@@ -0,0 +1,47 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-string-conversions-2</artifactId>
<version>0.1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>core-java-string-conversions-2</name>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-java</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-java</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>${org.hamcrest.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>core-java-string-conversions-2</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>

View File

@@ -1,4 +1,4 @@
package com.baeldung.datetime; package com.baeldung.stringconversions;
import java.time.LocalDateTime; import java.time.LocalDateTime;

View File

@@ -1,4 +1,4 @@
package com.baeldung.string.charArrayToString; package com.baeldung.chararraytostring;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;

View File

@@ -1,4 +1,4 @@
package com.baeldung.java.conversion; package com.baeldung.stringconversions;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
@@ -13,8 +13,6 @@ import java.util.GregorianCalendar;
import org.junit.Test; import org.junit.Test;
import com.baeldung.datetime.UseLocalDateTime;
public class StringConversionUnitTest { public class StringConversionUnitTest {
@Test @Test

View File

@@ -1,4 +1,4 @@
package com.baeldung.string.conversion; package com.baeldung.stringtobytearray;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;

View File

@@ -1,4 +1,4 @@
package com.baeldung.string.conversion; package com.baeldung.stringtobytearray;
import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;

View File

@@ -0,0 +1,16 @@
## Java String Conversions
This module contains articles about string conversions from/to another type.
### Relevant Articles:
- [Converting String to Stream of chars](https://www.baeldung.com/java-string-to-stream)
- [Converting Strings to Enums in Java](https://www.baeldung.com/java-string-to-enum)
- [Convert a String to Title Case](https://www.baeldung.com/java-string-title-case)
- [Convert java.util.Date to String](https://www.baeldung.com/java-util-date-to-string)
- [Converting a Stack Trace to a String in Java](https://www.baeldung.com/java-stacktrace-to-string)
- [Image to Base64 String Conversion](https://www.baeldung.com/java-base64-image-string)
- [Convert a Comma Separated String to a List in Java](https://www.baeldung.com/java-string-with-separator-to-list)
- [Converting Java String to Double](https://www.baeldung.com/java-string-to-double)
- [Convert Char to String in Java](https://www.baeldung.com/java-convert-char-to-string)
- [Convert String to int or Integer in Java](https://www.baeldung.com/java-convert-string-to-int-or-integer)
- More articles: [[next -->]](/core-java-string-conversions-2)

View File

@@ -0,0 +1,67 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-string-conversions</artifactId>
<version>0.1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>core-java-string-conversions</name>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-java</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-java</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>com.ibm.icu</groupId>
<artifactId>icu4j</artifactId>
<version>${icu4j.version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons-io.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
<!-- test scoped -->
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>${org.hamcrest.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>core-java-string-conversions</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<properties>
<icu4j.version>61.1</icu4j.version>
<assertj.version>3.6.1</assertj.version>
</properties>
</project>

View File

@@ -1,4 +1,4 @@
package com.baeldung.enums; package com.baeldung.stringtoenum;
public enum PizzaStatusEnum { public enum PizzaStatusEnum {
ORDERED(5) { ORDERED(5) {

View File

@@ -1,12 +1,11 @@
package com.baeldung.string; package com.baeldung.titlecase;
import java.util.Arrays;
import java.util.stream.Collectors;
import org.apache.commons.lang3.text.WordUtils;
import com.ibm.icu.lang.UCharacter; import com.ibm.icu.lang.UCharacter;
import com.ibm.icu.text.BreakIterator; import com.ibm.icu.text.BreakIterator;
import org.apache.commons.lang3.text.WordUtils;
import java.util.Arrays;
import java.util.stream.Collectors;
public class TitleCaseConverter { public class TitleCaseConverter {

View File

@@ -1,4 +1,4 @@
package com.baeldung; package com.baeldung.chartostring;
import org.junit.Test; import org.junit.Test;

View File

@@ -1,6 +1,7 @@
package com.baeldung.string.formatter; package com.baeldung.datetostring;
import static org.junit.Assert.assertEquals; import org.junit.BeforeClass;
import org.junit.Test;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
@@ -13,8 +14,7 @@ import java.util.Date;
import java.util.Locale; import java.util.Locale;
import java.util.TimeZone; import java.util.TimeZone;
import org.junit.BeforeClass; import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class DateToStringFormatterUnitTest { public class DateToStringFormatterUnitTest {

View File

@@ -1,13 +1,13 @@
package com.baeldung.fileToBase64StringConversion; package com.baeldung.filetobase64conversion;
import static org.junit.Assert.assertTrue; import org.apache.commons.io.FileUtils;
import org.junit.Test;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.Base64; import java.util.Base64;
import org.apache.commons.io.FileUtils; import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class FileToBase64StringConversionUnitTest { public class FileToBase64StringConversionUnitTest {

View File

@@ -1,4 +1,4 @@
package com.baeldung.string.todouble; package com.baeldung.stringtodouble;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;

View File

@@ -1,10 +1,10 @@
package com.baeldung.enums; package com.baeldung.stringtoenum;
import static junit.framework.TestCase.assertTrue; import static junit.framework.TestCase.assertTrue;
import org.junit.Test; import org.junit.Test;
public class PizzaUnitTest { public class StringToEnumUnitTest {
@Test @Test
public void whenConvertedIntoEnum_thenGetsConvertedCorrectly() { public void whenConvertedIntoEnum_thenGetsConvertedCorrectly() {

View File

@@ -1,4 +1,4 @@
package com.baeldung; package com.baeldung.stringtoint;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;

View File

@@ -1,135 +1,134 @@
package com.baeldung; package com.baeldung.stringtolist;
import static org.junit.Assert.assertEquals; import com.google.common.base.Function;
import com.google.common.base.Splitter;
import java.util.ArrayList; import com.google.common.collect.Lists;
import java.util.Arrays; import org.apache.commons.lang3.StringUtils;
import java.util.List; import org.junit.Test;
import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.ArrayList;
import java.util.Arrays;
import org.apache.commons.lang3.StringUtils; import java.util.List;
import org.junit.Test; import java.util.stream.Collectors;
import java.util.stream.Stream;
import com.google.common.base.Function;
import com.google.common.base.Splitter; import static org.junit.Assert.assertEquals;
import com.google.common.collect.Lists;
public class ConvertStringToListUnitTest {
public class ConvertStringToListUnitTest {
private final String countries = "Russia,Germany,England,France,Italy";
private final String countries = "Russia,Germany,England,France,Italy"; private final String ranks = "1,2,3,4,5, 6,7";
private final String ranks = "1,2,3,4,5, 6,7"; private final String emptyStrings = ",,,,,";
private final String emptyStrings = ",,,,,"; private final List<String> expectedCountriesList = Arrays.asList("Russia", "Germany", "England", "France", "Italy");
private final List<String> expectedCountriesList = Arrays.asList("Russia", "Germany", "England", "France", "Italy"); private final List<Integer> expectedRanksList = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
private final List<Integer> expectedRanksList = Arrays.asList(1, 2, 3, 4, 5, 6, 7); private final List<String> expectedEmptyStringsList = Arrays.asList("", "", "", "", "", "");
private final List<String> expectedEmptyStringsList = Arrays.asList("", "", "", "", "", "");
@Test
@Test public void givenString_thenGetListOfStringByJava() {
public void givenString_thenGetListOfStringByJava() { List<String> convertedCountriesList = Arrays.asList(countries.split(",", -1));
List<String> convertedCountriesList = Arrays.asList(countries.split(",", -1));
assertEquals(expectedCountriesList, convertedCountriesList);
assertEquals(expectedCountriesList, convertedCountriesList); }
}
@Test
@Test public void givenString_thenGetListOfStringByApache() {
public void givenString_thenGetListOfStringByApache() { List<String> convertedCountriesList = Arrays.asList(StringUtils.splitPreserveAllTokens(countries, ","));
List<String> convertedCountriesList = Arrays.asList(StringUtils.splitPreserveAllTokens(countries, ","));
assertEquals(expectedCountriesList, convertedCountriesList);
assertEquals(expectedCountriesList, convertedCountriesList); }
}
@Test
@Test public void givenString_thenGetListOfStringByGuava() {
public void givenString_thenGetListOfStringByGuava() { List<String> convertedCountriesList = Splitter.on(",")
List<String> convertedCountriesList = Splitter.on(",") .trimResults()
.trimResults() .splitToList(countries);
.splitToList(countries);
assertEquals(expectedCountriesList, convertedCountriesList);
assertEquals(expectedCountriesList, convertedCountriesList); }
}
@Test
@Test public void givenString_thenGetListOfStringByJava8() {
public void givenString_thenGetListOfStringByJava8() { List<String> convertedCountriesList = Stream.of(countries.split(",", -1))
List<String> convertedCountriesList = Stream.of(countries.split(",", -1)) .collect(Collectors.toList());
.collect(Collectors.toList());
assertEquals(expectedCountriesList, convertedCountriesList);
assertEquals(expectedCountriesList, convertedCountriesList); }
}
@Test
@Test public void givenString_thenGetListOfIntegerByJava() {
public void givenString_thenGetListOfIntegerByJava() { String[] convertedRankArray = ranks.split(",");
String[] convertedRankArray = ranks.split(","); List<Integer> convertedRankList = new ArrayList<Integer>();
List<Integer> convertedRankList = new ArrayList<Integer>(); for (String number : convertedRankArray) {
for (String number : convertedRankArray) { convertedRankList.add(Integer.parseInt(number.trim()));
convertedRankList.add(Integer.parseInt(number.trim())); }
}
assertEquals(expectedRanksList, convertedRankList);
assertEquals(expectedRanksList, convertedRankList); }
}
@Test
@Test public void givenString_thenGetListOfIntegerByGuava() {
public void givenString_thenGetListOfIntegerByGuava() { List<Integer> convertedRankList = Lists.transform(Splitter.on(",")
List<Integer> convertedRankList = Lists.transform(Splitter.on(",") .trimResults()
.trimResults() .splitToList(ranks), new Function<String, Integer>() {
.splitToList(ranks), new Function<String, Integer>() { @Override
@Override public Integer apply(String input) {
public Integer apply(String input) { return Integer.parseInt(input.trim());
return Integer.parseInt(input.trim()); }
} });
});
assertEquals(expectedRanksList, convertedRankList);
assertEquals(expectedRanksList, convertedRankList); }
}
@Test
@Test public void givenString_thenGetListOfIntegerByJava8() {
public void givenString_thenGetListOfIntegerByJava8() { List<Integer> convertedRankList = Stream.of(ranks.split(","))
List<Integer> convertedRankList = Stream.of(ranks.split(",")) .map(String::trim)
.map(String::trim) .map(Integer::parseInt)
.map(Integer::parseInt) .collect(Collectors.toList());
.collect(Collectors.toList());
assertEquals(expectedRanksList, convertedRankList);
assertEquals(expectedRanksList, convertedRankList); }
}
@Test
@Test public void givenString_thenGetListOfIntegerByApache() {
public void givenString_thenGetListOfIntegerByApache() { String[] convertedRankArray = StringUtils.split(ranks, ",");
String[] convertedRankArray = StringUtils.split(ranks, ","); List<Integer> convertedRankList = new ArrayList<Integer>();
List<Integer> convertedRankList = new ArrayList<Integer>(); for (String number : convertedRankArray) {
for (String number : convertedRankArray) { convertedRankList.add(Integer.parseInt(number.trim()));
convertedRankList.add(Integer.parseInt(number.trim())); }
}
assertEquals(expectedRanksList, convertedRankList);
assertEquals(expectedRanksList, convertedRankList); }
}
@Test
@Test public void givenEmptyStrings_thenGetListOfStringByJava() {
public void givenEmptyStrings_thenGetListOfStringByJava() { List<String> convertedEmptyStringsList = Arrays.asList(emptyStrings.split(",", -1));
List<String> convertedEmptyStringsList = Arrays.asList(emptyStrings.split(",", -1));
assertEquals(expectedEmptyStringsList, convertedEmptyStringsList);
assertEquals(expectedEmptyStringsList, convertedEmptyStringsList); }
}
@Test
@Test public void givenEmptyStrings_thenGetListOfStringByApache() {
public void givenEmptyStrings_thenGetListOfStringByApache() { List<String> convertedEmptyStringsList = Arrays.asList(StringUtils.splitPreserveAllTokens(emptyStrings, ","));
List<String> convertedEmptyStringsList = Arrays.asList(StringUtils.splitPreserveAllTokens(emptyStrings, ","));
assertEquals(expectedEmptyStringsList, convertedEmptyStringsList);
assertEquals(expectedEmptyStringsList, convertedEmptyStringsList); }
}
@Test
@Test public void givenEmptyStrings_thenGetListOfStringByGuava() {
public void givenEmptyStrings_thenGetListOfStringByGuava() { List<String> convertedEmptyStringsList = Splitter.on(",")
List<String> convertedEmptyStringsList = Splitter.on(",") .trimResults()
.trimResults() .splitToList(emptyStrings);
.splitToList(emptyStrings);
assertEquals(expectedEmptyStringsList, convertedEmptyStringsList);
assertEquals(expectedEmptyStringsList, convertedEmptyStringsList); }
}
@Test
@Test public void givenEmptyStrings_thenGetListOfStringByJava8() {
public void givenEmptyStrings_thenGetListOfStringByJava8() { List<String> convertedEmptyStringsList = Stream.of(emptyStrings.split(",", -1))
List<String> convertedEmptyStringsList = Stream.of(emptyStrings.split(",", -1)) .collect(Collectors.toList());
.collect(Collectors.toList());
assertEquals(expectedEmptyStringsList, convertedEmptyStringsList);
assertEquals(expectedEmptyStringsList, convertedEmptyStringsList); }
}
}
}

View File

@@ -1,4 +1,4 @@
package com.baeldung.string; package com.baeldung.stringtostream;
import org.junit.Test; import org.junit.Test;

View File

@@ -1,4 +1,4 @@
package com.baeldung.string; package com.baeldung.titlecase;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;

View File

Before

Width:  |  Height:  |  Size: 2.8 KiB

After

Width:  |  Height:  |  Size: 2.8 KiB

View File

@@ -1,6 +1,6 @@
package com.baeldung.builder package com.baeldung.builder
class FoodOrder( class FoodOrder private constructor(
val bread: String?, val bread: String?,
val condiments: String?, val condiments: String?,
val meat: String?, val meat: String?,

View File

@@ -6,9 +6,9 @@ import static org.junit.Assert.assertThat;
import java.io.IOException; import java.io.IOException;
import java.security.GeneralSecurityException; import java.security.GeneralSecurityException;
import java.security.cert.X509Certificate;
import javax.net.ssl.SSLContext; import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLPeerUnverifiedException;
import org.apache.http.HttpResponse; import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException; import org.apache.http.client.ClientProtocolException;
@@ -46,7 +46,7 @@ public class RestClientLiveManualTest {
// old httpClient will throw UnsupportedOperationException // old httpClient will throw UnsupportedOperationException
@Ignore @Ignore
@Test @Test
public final void givenAcceptingAllCertificates_whenHttpsUrlIsConsumed_thenOk_1() throws GeneralSecurityException { public final void givenAcceptingAllCertificates_whenHttpsUrlIsConsumed_thenOk() throws GeneralSecurityException {
final HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(); final HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
final CloseableHttpClient httpClient = (CloseableHttpClient) requestFactory.getHttpClient(); final CloseableHttpClient httpClient = (CloseableHttpClient) requestFactory.getHttpClient();
@@ -92,7 +92,7 @@ public class RestClientLiveManualTest {
} }
@Test @Test
public final void givenAcceptingAllCertificatesUsing4_4_whenHttpsUrlIsConsumedUsingRestTemplate_thenCorrect() throws ClientProtocolException, IOException { public final void givenAcceptingAllCertificatesUsing4_4_whenUsingRestTemplate_thenCorrect() throws ClientProtocolException, IOException {
final CloseableHttpClient httpClient = HttpClients.custom().setSSLHostnameVerifier(new NoopHostnameVerifier()).build(); final CloseableHttpClient httpClient = HttpClients.custom().setSSLHostnameVerifier(new NoopHostnameVerifier()).build();
final HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(); final HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
requestFactory.setHttpClient(httpClient); requestFactory.setHttpClient(httpClient);
@@ -101,4 +101,12 @@ public class RestClientLiveManualTest {
assertThat(response.getStatusCode().value(), equalTo(200)); assertThat(response.getStatusCode().value(), equalTo(200));
} }
@Test(expected = SSLPeerUnverifiedException.class)
public void whenHttpsUrlIsConsumed_thenException() throws ClientProtocolException, IOException {
CloseableHttpClient httpClient = HttpClients.createDefault();
String urlOverHttps = "https://localhost:8082/httpclient-simple";
HttpGet getMethod = new HttpGet(urlOverHttps);
HttpResponse response = httpClient.execute(getMethod);
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
}
} }

Some files were not shown because too many files have changed in this diff Show More