diff --git a/.gitignore b/.gitignore
index 180462a32f..50cb889e5b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -19,6 +19,7 @@
.idea/
*.iml
*.iws
+out/
# Mac
.DS_Store
@@ -27,6 +28,9 @@
log/
target/
+# Gradle
+.gradle/
+
spring-openid/src/main/resources/application.properties
.recommenders/
/spring-hibernate4/nbproject/
@@ -62,3 +66,15 @@ jmeter/src/main/resources/*-JMeter.csv
**/dist
**/tmp
**/out-tsc
+**/nbproject/
+**/nb-configuration.xml
+core-scala/.cache-main
+core-scala/.cache-tests
+
+
+persistence-modules/hibernate5/transaction.log
+apache-avro/src/main/java/com/baeldung/avro/model/
+jta/transaction-logs/
+software-security/sql-injection-samples/derby.log
+spring-soap/src/main/java/com/baeldung/springsoap/gen/
+/report-*.json
\ No newline at end of file
diff --git a/.travis.yml b/.travis.yml
index 5e2d690b4e..5e86714a89 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -4,7 +4,7 @@ before_install:
- echo "MAVEN_OPTS='-Xmx2048M -Xss128M -XX:+CMSClassUnloadingEnabled -XX:+UseG1GC -XX:-UseGCOverheadLimit'" > ~/.mavenrc
install: skip
-script: travis_wait 60 mvn -q install -Pdefault
+script: travis_wait 60 mvn -q install -Pdefault-first,default-second -Dgib.enabled=true
sudo: required
diff --git a/JGit/pom.xml b/JGit/pom.xml
index 176d55d321..deae1e45e3 100644
--- a/JGit/pom.xml
+++ b/JGit/pom.xml
@@ -5,9 +5,9 @@
com.baeldung
JGit
1.0-SNAPSHOT
+ JGit
jar
http://maven.apache.org
- JGit
com.baeldung
diff --git a/JGit/src/main/java/com/baeldung/jgit/porcelain/Log.java b/JGit/src/main/java/com/baeldung/jgit/porcelain/Log.java
index cb476b9d9e..a50028a9ae 100644
--- a/JGit/src/main/java/com/baeldung/jgit/porcelain/Log.java
+++ b/JGit/src/main/java/com/baeldung/jgit/porcelain/Log.java
@@ -28,14 +28,14 @@ public class Log {
System.out.println("Had " + count + " commits overall on current branch");
logs = git.log()
- .add(repository.resolve("remotes/origin/testbranch"))
+ .add(repository.resolve(git.getRepository().getFullBranch()))
.call();
count = 0;
for (RevCommit rev : logs) {
System.out.println("Commit: " + rev /* + ", name: " + rev.getName() + ", id: " + rev.getId().getName() */);
count++;
}
- System.out.println("Had " + count + " commits overall on test-branch");
+ System.out.println("Had " + count + " commits overall on "+git.getRepository().getFullBranch());
logs = git.log()
.all()
diff --git a/JGit/src/test/java/com/baeldung/jgit/JGitBugIntegrationTest.java b/JGit/src/test/java/com/baeldung/jgit/JGitBugIntegrationTest.java
index ed7168b2c2..ad34890996 100644
--- a/JGit/src/test/java/com/baeldung/jgit/JGitBugIntegrationTest.java
+++ b/JGit/src/test/java/com/baeldung/jgit/JGitBugIntegrationTest.java
@@ -1,3 +1,5 @@
+package com.baeldung.jgit;
+
import com.baeldung.jgit.helper.Helper;
import org.eclipse.jgit.lib.ObjectLoader;
import org.eclipse.jgit.lib.ObjectReader;
diff --git a/README.md b/README.md
index d20968b455..1030cbb09c 100644
--- a/README.md
+++ b/README.md
@@ -2,41 +2,42 @@
The "REST with Spring" Classes
==============================
-Here's the Master Class of REST With Spring (price changes permanently next Friday):
-**[>> THE REST WITH SPRING MASTER CLASS](http://www.baeldung.com/rest-with-spring-course?utm_source=github&utm_medium=social&utm_content=tutorials&utm_campaign=rws#master-class)**
+Here's the Master Class of REST With Spring (along with the newly announced Boot 2 material):
+**[>> THE REST WITH SPRING - MASTER CLASS](http://www.baeldung.com/rest-with-spring-course?utm_source=github&utm_medium=social&utm_content=tutorials&utm_campaign=rws#master-class)**
And here's the Master Class of Learn Spring Security:
-**[>> LEARN SPRING SECURITY MASTER CLASS](http://www.baeldung.com/learn-spring-security-course?utm_source=github&utm_medium=social&utm_content=tutorials&utm_campaign=lss#master-class)**
+**[>> LEARN SPRING SECURITY - MASTER CLASS](http://www.baeldung.com/learn-spring-security-course?utm_source=github&utm_medium=social&utm_content=tutorials&utm_campaign=lss#master-class)**
-Spring Tutorials
+Java and Spring Tutorials
================
-This project is **a collection of small and focused tutorials** each covering a single and well defined area of development.
-Most of the tutorial projects are focused on the `Spring Framework` (and `Spring Security`).
+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 Security.
In additional to Spring, the following technologies are in focus: `core Java`, `Jackson`, `HttpClient`, `Guava`.
+
Building the project
====================
-To do the full build, do: `mvn install -Pdefault -Dgib.enabled=false`
+To do the full build, do: `mvn clean install`
-Working with the code in Eclipse
-================================
-Any IDE can be used to work with the projects, but if you're using Eclipse, consider the following.
-
-- import the included **formatter** in Eclipse:
-`https://github.com/eugenp/tutorials/tree/master/eclipse`
+Building a single module
+====================
+To build a specific module run the command: `mvn clean install` in the module directory
+
+
+Running a Spring Boot module
+====================
+To run a Spring Boot module run the command: `mvn spring-boot:run` in the module directory
+
+#Running Tests
+
+The command `mvn clean install` will run the unit tests in a module.
+To run the integration tests, use the command `mvn clean install -Pintegration-lite-first`
+
-CI - Jenkins
-================================
-This tutorials project is being built **[>> HERE](https://rest-security.ci.cloudbees.com/job/tutorials-unit/)**
-### Relevant Articles:
-================================
-- [Apache Maven Standard Directory Layout](http://www.baeldung.com/maven-directory-structure)
-- [Apache Maven Tutorial](http://www.baeldung.com/maven)
-- [Designing a User Friendly Java Library](http://www.baeldung.com/design-a-user-friendly-java-library)
diff --git a/Twitter4J/pom.xml b/Twitter4J/pom.xml
index 80236a2cd4..b82b9f6778 100644
--- a/Twitter4J/pom.xml
+++ b/Twitter4J/pom.xml
@@ -1,13 +1,9 @@
4.0.0
-
- com.mabsisa
Twitter4J
- jar
- 1.0-SNAPSHOT
Twitter4J
- http://maven.apache.org
+ jar
com.baeldung
@@ -23,27 +19,6 @@
-
- ${project.artifactId}
-
-
- src/main/resources
-
-
-
-
- org.apache.maven.plugins
- maven-surefire-plugin
- ${maven-surefire-plugin.version}
-
-
- **/ApplicationTest.java
-
-
-
-
-
-
4.0.6
diff --git a/activejdbc/pom.xml b/activejdbc/pom.xml
deleted file mode 100644
index 0c8047f10b..0000000000
--- a/activejdbc/pom.xml
+++ /dev/null
@@ -1,135 +0,0 @@
-
- 4.0.0
- com.baeldung
- activejdbc
- 1.0-SNAPSHOT
- jar
- activejdbc
- http://maven.apache.org
-
-
- parent-modules
- com.baeldung
- 1.0.0-SNAPSHOT
-
-
-
-
- org.javalite
- activejdbc
- ${activejdbc.version}
-
-
- opensymphony
- oscache
-
-
-
-
- mysql
- mysql-connector-java
- ${mysql.connector.version}
-
-
- org.slf4j
- slf4j-simple
- ${org.slf4j.version}
-
-
-
-
-
-
- org.apache.maven.plugins
- maven-compiler-plugin
- ${maven-compiler-plugin.version}
-
- ${java.version}
- ${java.version}
- UTF-8
-
-
-
- org.javalite
- activejdbc-instrumentation
- ${activejdbc.version}
-
-
- process-classes
-
- instrument
-
-
-
-
-
- org.javalite
- db-migrator-maven-plugin
- ${activejdbc.version}
-
- ${project.basedir}/src/main/resources/database.properties
- ${environments}
-
-
-
- mysql
- mysql-connector-java
- ${mysql.connector.version}
-
-
-
-
- org.apache.maven.plugins
- maven-surefire-plugin
- ${maven-surefire-plugin.version}
-
- brief
- true
- false
-
- **/*Spec*.java
- **/*Test*.java
-
-
- **/helpers/*
- **/*$*
-
-
-
-
-
-
-
-
- snapshots1
- JavaLite Snapshots1
- http://repo.javalite.io/
-
- true
- always
- warn
-
-
-
-
-
-
- snapshots2
- JavaLite Snapshots2
- http://repo.javalite.io/
-
- true
- always
- warn
-
-
-
-
-
- 1.4.13
- development.test,development
- 5.1.34
-
-
-
\ No newline at end of file
diff --git a/activejdbc/src/main/java/com/baeldung/model/Employee.java b/activejdbc/src/main/java/com/baeldung/model/Employee.java
deleted file mode 100644
index b7fa8aaf1f..0000000000
--- a/activejdbc/src/main/java/com/baeldung/model/Employee.java
+++ /dev/null
@@ -1,19 +0,0 @@
-package com.baeldung.model;
-
-
-import org.javalite.activejdbc.Model;
-
-public class Employee extends Model {
-
- public Employee(){
-
- }
-
- public Employee(String firstName, String lastName, String gender, String createdBy) {
- set("first_name1",firstName);
- set("last_name",lastName);
- set("gender",gender);
- set("created_by",createdBy);
- }
-
-}
diff --git a/activejdbc/src/test/java/com/baeldung/ActiveJDBCAppTest.java b/activejdbc/src/test/java/com/baeldung/ActiveJDBCAppTest.java
deleted file mode 100644
index 316dc34712..0000000000
--- a/activejdbc/src/test/java/com/baeldung/ActiveJDBCAppTest.java
+++ /dev/null
@@ -1,51 +0,0 @@
-package com.baeldung;
-
-import com.baeldung.model.Employee;
-import com.baeldung.model.Role;
-import org.javalite.activejdbc.test.DBSpec;
-import org.junit.Test;
-
-import java.util.List;
-
-public class ActiveJDBCAppTest extends DBSpec
-{
- @Test
- public void ifEmployeeCreated_thenIsValid() {
- Employee employee = new Employee("B", "N", "M", "BN");
- the(employee).shouldBe("valid");
- }
-
- @Test
- public void ifEmployeeCreatedWithRoles_thenShouldPersist() {
- Employee employee = new Employee("B", "N", "M", "BN");
- employee.saveIt();
- employee.add(new Role("Java Developer","BN"));
- employee.add(new Role("Lead Java Developer","BN"));
- a(Role.count()).shouldBeEqual(2);
- List roles = employee.getAll(Role.class).orderBy("created_at");
- the(roles.get(0).getRoleName()).shouldBeEqual("Java Developer");
- the(roles.get(1).getRoleName()).shouldBeEqual("Lead Java Developer");
- }
-
- @Test
- public void ifEmployeeCreatedWithRoles_whenNameUpdated_thenShouldShowNewName() {
- Employee employee = new Employee("Binesh", "N", "M", "BN");
- employee.saveIt();
- employee.add(new Role("Java Developer","BN"));
- employee.add(new Role("Lead Java Developer","BN"));
- employee = Employee.findFirst("first_name = ?", "Binesh");
- employee.set("last_name","Narayanan").saveIt();
- Employee updated = Employee.findFirst("first_name = ?", "Binesh");
- the(updated.getLastName()).shouldBeEqual("Narayanan");
- }
-
- @Test
- public void ifEmployeeCreatedWithRoles_whenDeleted_thenShouldNotBeFound() {
- Employee employee = new Employee("Binesh", "N", "M", "BN");
- employee.saveIt();
- employee.add(new Role("Java Developer","BN"));
- employee.delete();
- employee = Employee.findFirst("first_name = ?", "Binesh");
- the(employee).shouldBeNull();
- }
-}
diff --git a/akka-http/README.md b/akka-http/README.md
new file mode 100644
index 0000000000..3831b5079f
--- /dev/null
+++ b/akka-http/README.md
@@ -0,0 +1,3 @@
+## Relevant articles:
+
+- [Introduction to Akka HTTP](https://www.baeldung.com/akka-http)
diff --git a/akka-http/pom.xml b/akka-http/pom.xml
new file mode 100644
index 0000000000..6d73f2f2e6
--- /dev/null
+++ b/akka-http/pom.xml
@@ -0,0 +1,47 @@
+
+
+
+ 4.0.0
+ akka-http
+ akka-http
+
+
+ parent-modules
+ com.baeldung
+ 1.0.0-SNAPSHOT
+
+
+
+
+ com.typesafe.akka
+ akka-http_2.12
+ ${akka.http.version}
+
+
+ com.typesafe.akka
+ akka-stream_2.12
+ ${akka.stream.version}
+
+
+ com.typesafe.akka
+ akka-http-jackson_2.12
+ ${akka.http.version}
+
+
+ com.typesafe.akka
+ akka-http-testkit_2.12
+ ${akka.http.version}
+ test
+
+
+
+
+ UTF-8
+ UTF-8
+ 10.0.11
+ 2.5.11
+
+
diff --git a/akka-http/src/main/java/com/baeldung/akkahttp/User.java b/akka-http/src/main/java/com/baeldung/akkahttp/User.java
new file mode 100644
index 0000000000..43c21eca62
--- /dev/null
+++ b/akka-http/src/main/java/com/baeldung/akkahttp/User.java
@@ -0,0 +1,26 @@
+package com.baeldung.akkahttp;
+
+public class User {
+
+ private final Long id;
+
+ private final String name;
+
+ public User() {
+ this.name = "";
+ this.id = null;
+ }
+
+ public User(Long id, String name) {
+ this.name = name;
+ this.id = id;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public Long getId() {
+ return id;
+ }
+}
\ No newline at end of file
diff --git a/akka-http/src/main/java/com/baeldung/akkahttp/UserActor.java b/akka-http/src/main/java/com/baeldung/akkahttp/UserActor.java
new file mode 100644
index 0000000000..431014a88b
--- /dev/null
+++ b/akka-http/src/main/java/com/baeldung/akkahttp/UserActor.java
@@ -0,0 +1,41 @@
+package com.baeldung.akkahttp;
+
+import akka.actor.AbstractActor;
+import akka.actor.Props;
+import akka.japi.pf.FI;
+import com.baeldung.akkahttp.UserMessages.ActionPerformed;
+import com.baeldung.akkahttp.UserMessages.CreateUserMessage;
+import com.baeldung.akkahttp.UserMessages.GetUserMessage;
+
+
+class UserActor extends AbstractActor {
+
+ private UserService userService = new UserService();
+
+ static Props props() {
+ return Props.create(UserActor.class);
+ }
+
+ @Override
+ public Receive createReceive() {
+ return receiveBuilder()
+ .match(CreateUserMessage.class, handleCreateUser())
+ .match(GetUserMessage.class, handleGetUser())
+ .build();
+ }
+
+ private FI.UnitApply handleCreateUser() {
+ return createUserMessageMessage -> {
+ userService.createUser(createUserMessageMessage.getUser());
+ sender().tell(new ActionPerformed(String.format("User %s created.", createUserMessageMessage.getUser()
+ .getName())), getSelf());
+ };
+ }
+
+ private FI.UnitApply handleGetUser() {
+ return getUserMessageMessage -> {
+ sender().tell(userService.getUser(getUserMessageMessage.getUserId()), getSelf());
+ };
+ }
+
+}
diff --git a/akka-http/src/main/java/com/baeldung/akkahttp/UserMessages.java b/akka-http/src/main/java/com/baeldung/akkahttp/UserMessages.java
new file mode 100644
index 0000000000..995b92bcb0
--- /dev/null
+++ b/akka-http/src/main/java/com/baeldung/akkahttp/UserMessages.java
@@ -0,0 +1,49 @@
+package com.baeldung.akkahttp;
+
+import java.io.Serializable;
+
+public interface UserMessages {
+
+ class ActionPerformed implements Serializable {
+
+ private static final long serialVersionUID = 1L;
+
+ private final String description;
+
+ public ActionPerformed(String description) {
+ this.description = description;
+ }
+
+ public String getDescription() {
+ return description;
+ }
+ }
+
+ class CreateUserMessage implements Serializable {
+
+ private static final long serialVersionUID = 1L;
+ private final User user;
+
+ public CreateUserMessage(User user) {
+ this.user = user;
+ }
+
+ public User getUser() {
+ return user;
+ }
+ }
+
+ class GetUserMessage implements Serializable {
+ private static final long serialVersionUID = 1L;
+ private final Long userId;
+
+ public GetUserMessage(Long userId) {
+ this.userId = userId;
+ }
+
+ public Long getUserId() {
+ return userId;
+ }
+ }
+
+}
diff --git a/akka-http/src/main/java/com/baeldung/akkahttp/UserServer.java b/akka-http/src/main/java/com/baeldung/akkahttp/UserServer.java
new file mode 100644
index 0000000000..0c1dbd1f60
--- /dev/null
+++ b/akka-http/src/main/java/com/baeldung/akkahttp/UserServer.java
@@ -0,0 +1,70 @@
+package com.baeldung.akkahttp;
+
+import java.util.Optional;
+import java.util.concurrent.CompletionStage;
+import java.util.concurrent.TimeUnit;
+
+import akka.actor.ActorRef;
+import akka.actor.ActorSystem;
+import akka.http.javadsl.marshallers.jackson.Jackson;
+import akka.http.javadsl.model.StatusCodes;
+import akka.http.javadsl.server.HttpApp;
+import akka.http.javadsl.server.Route;
+import akka.pattern.PatternsCS;
+import akka.util.Timeout;
+import com.baeldung.akkahttp.UserMessages.ActionPerformed;
+import com.baeldung.akkahttp.UserMessages.CreateUserMessage;
+import com.baeldung.akkahttp.UserMessages.GetUserMessage;
+import scala.concurrent.duration.Duration;
+import static akka.http.javadsl.server.PathMatchers.*;
+
+class UserServer extends HttpApp {
+
+ private final ActorRef userActor;
+
+ Timeout timeout = new Timeout(Duration.create(5, TimeUnit.SECONDS));
+
+ UserServer(ActorRef userActor) {
+ this.userActor = userActor;
+ }
+
+ @Override
+ public Route routes() {
+ return path("users", this::postUser)
+ .orElse(path(segment("users").slash(longSegment()), id ->
+ route(getUser(id))));
+ }
+
+ private Route getUser(Long id) {
+ return get(() -> {
+ CompletionStage> user = PatternsCS.ask(userActor, new GetUserMessage(id), timeout)
+ .thenApply(obj -> (Optional) obj);
+
+ return onSuccess(() -> user, performed -> {
+ if (performed.isPresent())
+ return complete(StatusCodes.OK, performed.get(), Jackson.marshaller());
+ else
+ return complete(StatusCodes.NOT_FOUND);
+ });
+ });
+ }
+
+ private Route postUser() {
+ return route(post(() -> entity(Jackson.unmarshaller(User.class), user -> {
+ CompletionStage userCreated = PatternsCS.ask(userActor, new CreateUserMessage(user), timeout)
+ .thenApply(obj -> (ActionPerformed) obj);
+
+ return onSuccess(() -> userCreated, performed -> {
+ return complete(StatusCodes.CREATED, performed, Jackson.marshaller());
+ });
+ })));
+ }
+
+ public static void main(String[] args) throws Exception {
+ ActorSystem system = ActorSystem.create("userServer");
+ ActorRef userActor = system.actorOf(UserActor.props(), "userActor");
+ UserServer server = new UserServer(userActor);
+ server.startServer("localhost", 8080, system);
+ }
+
+}
diff --git a/akka-http/src/main/java/com/baeldung/akkahttp/UserService.java b/akka-http/src/main/java/com/baeldung/akkahttp/UserService.java
new file mode 100644
index 0000000000..50dc1e1b28
--- /dev/null
+++ b/akka-http/src/main/java/com/baeldung/akkahttp/UserService.java
@@ -0,0 +1,35 @@
+package com.baeldung.akkahttp;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Optional;
+
+public class UserService {
+
+ private final static List users = new ArrayList<>();
+
+ static {
+ users.add(new User(1l, "Alice"));
+ users.add(new User(2l, "Bob"));
+ users.add(new User(3l, "Chris"));
+ users.add(new User(4l, "Dick"));
+ users.add(new User(5l, "Eve"));
+ users.add(new User(6l, "Finn"));
+ }
+
+ public Optional getUser(Long id) {
+ return users.stream()
+ .filter(user -> user.getId()
+ .equals(id))
+ .findFirst();
+ }
+
+ public void createUser(User user) {
+ users.add(user);
+ }
+
+ public List getUsers(){
+ return users;
+ }
+
+}
diff --git a/akka-http/src/test/java/com/baeldung/akkahttp/UserServerUnitTest.java b/akka-http/src/test/java/com/baeldung/akkahttp/UserServerUnitTest.java
new file mode 100644
index 0000000000..1170a2d761
--- /dev/null
+++ b/akka-http/src/test/java/com/baeldung/akkahttp/UserServerUnitTest.java
@@ -0,0 +1,50 @@
+package com.baeldung.akkahttp;
+
+import akka.actor.ActorRef;
+import akka.actor.ActorSystem;
+import akka.http.javadsl.model.ContentTypes;
+import akka.http.javadsl.model.HttpEntities;
+import akka.http.javadsl.model.HttpRequest;
+import akka.http.javadsl.testkit.JUnitRouteTest;
+import akka.http.javadsl.testkit.TestRoute;
+import org.junit.Test;
+
+public class UserServerUnitTest extends JUnitRouteTest {
+
+ ActorSystem system = ActorSystem.create("helloAkkaHttpServer");
+
+ ActorRef userActorRef = system.actorOf(UserActor.props(), "userActor");
+
+ TestRoute appRoute = testRoute(new UserServer(userActorRef).routes());
+
+ @Test
+ public void whenRequest_thenActorResponds() {
+
+ appRoute.run(HttpRequest.GET("/users/1"))
+ .assertEntity(alice())
+ .assertStatusCode(200);
+
+ appRoute.run(HttpRequest.GET("/users/42"))
+ .assertStatusCode(404);
+
+ appRoute.run(HttpRequest.DELETE("/users/1"))
+ .assertStatusCode(200);
+
+ appRoute.run(HttpRequest.DELETE("/users/42"))
+ .assertStatusCode(200);
+
+ appRoute.run(HttpRequest.POST("/users")
+ .withEntity(HttpEntities.create(ContentTypes.APPLICATION_JSON, zaphod())))
+ .assertStatusCode(201);
+
+ }
+
+ private String alice() {
+ return "{\"id\":1,\"name\":\"Alice\"}";
+ }
+
+ private String zaphod() {
+ return "{\"id\":42,\"name\":\"Zaphod\"}";
+ }
+
+}
diff --git a/akka-streams/pom.xml b/akka-streams/pom.xml
index a885753ce2..7719bb7351 100644
--- a/akka-streams/pom.xml
+++ b/akka-streams/pom.xml
@@ -14,18 +14,19 @@
com.typesafe.akka
- akka-stream_2.11
+ akka-stream_${scala.version}
${akkastreams.version}
com.typesafe.akka
- akka-stream-testkit_2.11
+ akka-stream-testkit_${scala.version}
${akkastreams.version}
2.5.2
+ 2.11
\ No newline at end of file
diff --git a/algorithms-genetic/.gitignore b/algorithms-genetic/.gitignore
new file mode 100644
index 0000000000..30b2b7442c
--- /dev/null
+++ b/algorithms-genetic/.gitignore
@@ -0,0 +1,4 @@
+/target/
+.settings/
+.classpath
+.project
\ No newline at end of file
diff --git a/algorithms-genetic/README.md b/algorithms-genetic/README.md
new file mode 100644
index 0000000000..39f8d59eee
--- /dev/null
+++ b/algorithms-genetic/README.md
@@ -0,0 +1,6 @@
+## Relevant articles:
+
+- [Introduction to Jenetics Library](http://www.baeldung.com/jenetics)
+- [Ant Colony Optimization](http://www.baeldung.com/java-ant-colony-optimization)
+- [Design a Genetic Algorithm in Java](https://www.baeldung.com/java-genetic-algorithm)
+- [The Traveling Salesman Problem in Java](https://www.baeldung.com/java-simulated-annealing-for-traveling-salesman)
diff --git a/algorithms-genetic/pom.xml b/algorithms-genetic/pom.xml
new file mode 100644
index 0000000000..56f6a31525
--- /dev/null
+++ b/algorithms-genetic/pom.xml
@@ -0,0 +1,64 @@
+
+ 4.0.0
+ algorithms-genetic
+ 0.0.1-SNAPSHOT
+ algorithms-genetic
+
+
+ com.baeldung
+ parent-modules
+ 1.0.0-SNAPSHOT
+
+
+
+
+ org.apache.commons
+ commons-math3
+ ${commons-math3.version}
+
+
+ commons-codec
+ commons-codec
+ ${commons-codec.version}
+
+
+ org.projectlombok
+ lombok
+ ${lombok.version}
+ provided
+
+
+ io.jenetics
+ jenetics
+ ${io.jenetics.version}
+
+
+ org.assertj
+ assertj-core
+ ${org.assertj.core.version}
+ test
+
+
+
+
+
+
+
+ org.codehaus.mojo
+ exec-maven-plugin
+ ${exec-maven-plugin.version}
+
+
+
+
+
+
+ 3.6.1
+ 3.7.0
+ 3.9.0
+ 1.11
+
+
+
+
diff --git a/algorithms-genetic/src/main/java/com/baeldung/algorithms/RunAlgorithm.java b/algorithms-genetic/src/main/java/com/baeldung/algorithms/RunAlgorithm.java
new file mode 100644
index 0000000000..779cb9b970
--- /dev/null
+++ b/algorithms-genetic/src/main/java/com/baeldung/algorithms/RunAlgorithm.java
@@ -0,0 +1,38 @@
+package com.baeldung.algorithms;
+
+import java.util.Scanner;
+
+import com.baeldung.algorithms.ga.annealing.SimulatedAnnealing;
+import com.baeldung.algorithms.ga.ant_colony.AntColonyOptimization;
+import com.baeldung.algorithms.ga.binary.SimpleGeneticAlgorithm;
+
+public class RunAlgorithm {
+
+ public static void main(String[] args) throws InstantiationException, IllegalAccessException {
+ Scanner in = new Scanner(System.in);
+ System.out.println("Run algorithm:");
+ System.out.println("1 - Simulated Annealing");
+ System.out.println("2 - Simple Genetic Algorithm");
+ System.out.println("3 - Ant Colony");
+ int decision = in.nextInt();
+ switch (decision) {
+ case 1:
+ System.out.println(
+ "Optimized distance for travel: " + SimulatedAnnealing.simulateAnnealing(10, 10000, 0.9995));
+ break;
+ case 2:
+ SimpleGeneticAlgorithm ga = new SimpleGeneticAlgorithm();
+ ga.runAlgorithm(50, "1011000100000100010000100000100111001000000100000100000000001111");
+ break;
+ case 3:
+ AntColonyOptimization antColony = new AntColonyOptimization(21);
+ antColony.startAntOptimization();
+ break;
+ default:
+ System.out.println("Unknown option");
+ break;
+ }
+ in.close();
+ }
+
+}
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/ga/annealing/City.java b/algorithms-genetic/src/main/java/com/baeldung/algorithms/ga/annealing/City.java
similarity index 100%
rename from algorithms/src/main/java/com/baeldung/algorithms/ga/annealing/City.java
rename to algorithms-genetic/src/main/java/com/baeldung/algorithms/ga/annealing/City.java
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/ga/annealing/SimulatedAnnealing.java b/algorithms-genetic/src/main/java/com/baeldung/algorithms/ga/annealing/SimulatedAnnealing.java
similarity index 100%
rename from algorithms/src/main/java/com/baeldung/algorithms/ga/annealing/SimulatedAnnealing.java
rename to algorithms-genetic/src/main/java/com/baeldung/algorithms/ga/annealing/SimulatedAnnealing.java
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/ga/annealing/Travel.java b/algorithms-genetic/src/main/java/com/baeldung/algorithms/ga/annealing/Travel.java
similarity index 100%
rename from algorithms/src/main/java/com/baeldung/algorithms/ga/annealing/Travel.java
rename to algorithms-genetic/src/main/java/com/baeldung/algorithms/ga/annealing/Travel.java
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/ga/ant_colony/Ant.java b/algorithms-genetic/src/main/java/com/baeldung/algorithms/ga/ant_colony/Ant.java
similarity index 100%
rename from algorithms/src/main/java/com/baeldung/algorithms/ga/ant_colony/Ant.java
rename to algorithms-genetic/src/main/java/com/baeldung/algorithms/ga/ant_colony/Ant.java
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/ga/ant_colony/AntColonyOptimization.java b/algorithms-genetic/src/main/java/com/baeldung/algorithms/ga/ant_colony/AntColonyOptimization.java
similarity index 100%
rename from algorithms/src/main/java/com/baeldung/algorithms/ga/ant_colony/AntColonyOptimization.java
rename to algorithms-genetic/src/main/java/com/baeldung/algorithms/ga/ant_colony/AntColonyOptimization.java
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/ga/binary/Individual.java b/algorithms-genetic/src/main/java/com/baeldung/algorithms/ga/binary/Individual.java
similarity index 100%
rename from algorithms/src/main/java/com/baeldung/algorithms/ga/binary/Individual.java
rename to algorithms-genetic/src/main/java/com/baeldung/algorithms/ga/binary/Individual.java
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/ga/binary/Population.java b/algorithms-genetic/src/main/java/com/baeldung/algorithms/ga/binary/Population.java
similarity index 100%
rename from algorithms/src/main/java/com/baeldung/algorithms/ga/binary/Population.java
rename to algorithms-genetic/src/main/java/com/baeldung/algorithms/ga/binary/Population.java
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/ga/binary/SimpleGeneticAlgorithm.java b/algorithms-genetic/src/main/java/com/baeldung/algorithms/ga/binary/SimpleGeneticAlgorithm.java
similarity index 100%
rename from algorithms/src/main/java/com/baeldung/algorithms/ga/binary/SimpleGeneticAlgorithm.java
rename to algorithms-genetic/src/main/java/com/baeldung/algorithms/ga/binary/SimpleGeneticAlgorithm.java
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/ga/jenetics/Knapsack.java b/algorithms-genetic/src/main/java/com/baeldung/algorithms/ga/jenetics/Knapsack.java
similarity index 100%
rename from algorithms/src/main/java/com/baeldung/algorithms/ga/jenetics/Knapsack.java
rename to algorithms-genetic/src/main/java/com/baeldung/algorithms/ga/jenetics/Knapsack.java
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/ga/jenetics/KnapsackFF.java b/algorithms-genetic/src/main/java/com/baeldung/algorithms/ga/jenetics/KnapsackFF.java
similarity index 100%
rename from algorithms/src/main/java/com/baeldung/algorithms/ga/jenetics/KnapsackFF.java
rename to algorithms-genetic/src/main/java/com/baeldung/algorithms/ga/jenetics/KnapsackFF.java
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/ga/jenetics/KnapsackItem.java b/algorithms-genetic/src/main/java/com/baeldung/algorithms/ga/jenetics/KnapsackItem.java
similarity index 100%
rename from algorithms/src/main/java/com/baeldung/algorithms/ga/jenetics/KnapsackItem.java
rename to algorithms-genetic/src/main/java/com/baeldung/algorithms/ga/jenetics/KnapsackItem.java
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/ga/jenetics/SimpleGeneticAlgorithm.java b/algorithms-genetic/src/main/java/com/baeldung/algorithms/ga/jenetics/SimpleGeneticAlgorithm.java
similarity index 100%
rename from algorithms/src/main/java/com/baeldung/algorithms/ga/jenetics/SimpleGeneticAlgorithm.java
rename to algorithms-genetic/src/main/java/com/baeldung/algorithms/ga/jenetics/SimpleGeneticAlgorithm.java
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/ga/jenetics/SpringsteenProblem.java b/algorithms-genetic/src/main/java/com/baeldung/algorithms/ga/jenetics/SpringsteenProblem.java
similarity index 100%
rename from algorithms/src/main/java/com/baeldung/algorithms/ga/jenetics/SpringsteenProblem.java
rename to algorithms-genetic/src/main/java/com/baeldung/algorithms/ga/jenetics/SpringsteenProblem.java
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/ga/jenetics/SpringsteenRecord.java b/algorithms-genetic/src/main/java/com/baeldung/algorithms/ga/jenetics/SpringsteenRecord.java
similarity index 100%
rename from algorithms/src/main/java/com/baeldung/algorithms/ga/jenetics/SpringsteenRecord.java
rename to algorithms-genetic/src/main/java/com/baeldung/algorithms/ga/jenetics/SpringsteenRecord.java
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/ga/jenetics/SubsetSum.java b/algorithms-genetic/src/main/java/com/baeldung/algorithms/ga/jenetics/SubsetSum.java
similarity index 100%
rename from algorithms/src/main/java/com/baeldung/algorithms/ga/jenetics/SubsetSum.java
rename to algorithms-genetic/src/main/java/com/baeldung/algorithms/ga/jenetics/SubsetSum.java
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/ga/jenetics/TravelingSalesman.java b/algorithms-genetic/src/main/java/com/baeldung/algorithms/ga/jenetics/TravelingSalesman.java
similarity index 100%
rename from algorithms/src/main/java/com/baeldung/algorithms/ga/jenetics/TravelingSalesman.java
rename to algorithms-genetic/src/main/java/com/baeldung/algorithms/ga/jenetics/TravelingSalesman.java
diff --git a/activejdbc/src/main/resources/logback.xml b/algorithms-genetic/src/main/resources/logback.xml
similarity index 100%
rename from activejdbc/src/main/resources/logback.xml
rename to algorithms-genetic/src/main/resources/logback.xml
diff --git a/algorithms/src/test/java/com/baeldung/algorithms/AntColonyOptimizationLongRunningUnitTest.java b/algorithms-genetic/src/test/java/com/baeldung/algorithms/AntColonyOptimizationLongRunningUnitTest.java
similarity index 100%
rename from algorithms/src/test/java/com/baeldung/algorithms/AntColonyOptimizationLongRunningUnitTest.java
rename to algorithms-genetic/src/test/java/com/baeldung/algorithms/AntColonyOptimizationLongRunningUnitTest.java
diff --git a/algorithms/src/test/java/com/baeldung/algorithms/BinaryGeneticAlgorithmLongRunningUnitTest.java b/algorithms-genetic/src/test/java/com/baeldung/algorithms/BinaryGeneticAlgorithmLongRunningUnitTest.java
similarity index 100%
rename from algorithms/src/test/java/com/baeldung/algorithms/BinaryGeneticAlgorithmLongRunningUnitTest.java
rename to algorithms-genetic/src/test/java/com/baeldung/algorithms/BinaryGeneticAlgorithmLongRunningUnitTest.java
diff --git a/algorithms/src/test/java/com/baeldung/algorithms/SimulatedAnnealingLongRunningUnitTest.java b/algorithms-genetic/src/test/java/com/baeldung/algorithms/SimulatedAnnealingLongRunningUnitTest.java
similarity index 100%
rename from algorithms/src/test/java/com/baeldung/algorithms/SimulatedAnnealingLongRunningUnitTest.java
rename to algorithms-genetic/src/test/java/com/baeldung/algorithms/SimulatedAnnealingLongRunningUnitTest.java
diff --git a/algorithms-miscellaneous-1/.gitignore b/algorithms-miscellaneous-1/.gitignore
new file mode 100644
index 0000000000..30b2b7442c
--- /dev/null
+++ b/algorithms-miscellaneous-1/.gitignore
@@ -0,0 +1,4 @@
+/target/
+.settings/
+.classpath
+.project
\ No newline at end of file
diff --git a/algorithms-miscellaneous-1/README.md b/algorithms-miscellaneous-1/README.md
new file mode 100644
index 0000000000..479c2792f6
--- /dev/null
+++ b/algorithms-miscellaneous-1/README.md
@@ -0,0 +1,18 @@
+## Relevant articles:
+
+- [Validating Input With Finite Automata in Java](http://www.baeldung.com/java-finite-automata)
+- [Example of Hill Climbing Algorithm](http://www.baeldung.com/java-hill-climbing-algorithm)
+- [Monte Carlo Tree Search for Tic-Tac-Toe Game](http://www.baeldung.com/java-monte-carlo-tree-search)
+- [Binary Search Algorithm in Java](http://www.baeldung.com/java-binary-search)
+- [Introduction to Minimax Algorithm](http://www.baeldung.com/java-minimax-algorithm)
+- [How to Calculate Levenshtein Distance in Java?](http://www.baeldung.com/java-levenshtein-distance)
+- [How to Find the Kth Largest Element in Java](http://www.baeldung.com/java-kth-largest-element)
+- [Multi-Swarm Optimization Algorithm in Java](http://www.baeldung.com/java-multi-swarm-algorithm)
+- [String Search Algorithms for Large Texts](http://www.baeldung.com/java-full-text-search-algorithms)
+- [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)
+- [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 the Longest Substring without Repeating Characters](https://www.baeldung.com/java-longest-substring-without-repeated-characters)
+- [Permutations of an Array in Java](https://www.baeldung.com/java-array-permutations)
+- [Generate Combinations in Java](https://www.baeldung.com/java-combinations-algorithm)
diff --git a/algorithms-miscellaneous-1/pom.xml b/algorithms-miscellaneous-1/pom.xml
new file mode 100644
index 0000000000..30130208f8
--- /dev/null
+++ b/algorithms-miscellaneous-1/pom.xml
@@ -0,0 +1,88 @@
+
+ 4.0.0
+ algorithms-miscellaneous-1
+ 0.0.1-SNAPSHOT
+ algorithms-miscellaneous-1
+
+
+ com.baeldung
+ parent-modules
+ 1.0.0-SNAPSHOT
+
+
+
+
+ org.apache.commons
+ commons-math3
+ ${commons-math3.version}
+
+
+ com.google.guava
+ guava
+ ${guava.version}
+
+
+ commons-codec
+ commons-codec
+ ${commons-codec.version}
+
+
+ org.projectlombok
+ lombok
+ ${lombok.version}
+ provided
+
+
+ org.assertj
+ assertj-core
+ ${org.assertj.core.version}
+ test
+
+
+ com.github.dpaukov
+ combinatoricslib3
+ 3.3.0
+
+
+
+
+
+
+
+ org.codehaus.mojo
+ exec-maven-plugin
+ ${exec-maven-plugin.version}
+
+
+
+
+
+
+
+
+ org.codehaus.mojo
+ cobertura-maven-plugin
+ 2.7
+
+
+
+ com/baeldung/algorithms/dijkstra/*
+
+
+ com/baeldung/algorithms/dijkstra/*
+
+
+
+
+
+
+
+
+ 3.6.1
+ 3.9.0
+ 1.11
+ 27.0.1-jre
+
+
+
\ No newline at end of file
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/automata/FiniteStateMachine.java b/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/automata/FiniteStateMachine.java
similarity index 100%
rename from algorithms/src/main/java/com/baeldung/algorithms/automata/FiniteStateMachine.java
rename to algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/automata/FiniteStateMachine.java
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/automata/RtFiniteStateMachine.java b/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/automata/RtFiniteStateMachine.java
similarity index 100%
rename from algorithms/src/main/java/com/baeldung/algorithms/automata/RtFiniteStateMachine.java
rename to algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/automata/RtFiniteStateMachine.java
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/automata/RtState.java b/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/automata/RtState.java
similarity index 100%
rename from algorithms/src/main/java/com/baeldung/algorithms/automata/RtState.java
rename to algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/automata/RtState.java
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/automata/RtTransition.java b/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/automata/RtTransition.java
similarity index 100%
rename from algorithms/src/main/java/com/baeldung/algorithms/automata/RtTransition.java
rename to algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/automata/RtTransition.java
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/automata/State.java b/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/automata/State.java
similarity index 100%
rename from algorithms/src/main/java/com/baeldung/algorithms/automata/State.java
rename to algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/automata/State.java
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/automata/Transition.java b/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/automata/Transition.java
similarity index 100%
rename from algorithms/src/main/java/com/baeldung/algorithms/automata/Transition.java
rename to algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/automata/Transition.java
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/binarysearch/BinarySearch.java b/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/binarysearch/BinarySearch.java
similarity index 100%
rename from algorithms/src/main/java/com/baeldung/algorithms/binarysearch/BinarySearch.java
rename to algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/binarysearch/BinarySearch.java
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/hillclimbing/HillClimbing.java b/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/hillclimbing/HillClimbing.java
similarity index 100%
rename from algorithms/src/main/java/com/baeldung/algorithms/hillclimbing/HillClimbing.java
rename to algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/hillclimbing/HillClimbing.java
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/hillclimbing/State.java b/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/hillclimbing/State.java
similarity index 100%
rename from algorithms/src/main/java/com/baeldung/algorithms/hillclimbing/State.java
rename to algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/hillclimbing/State.java
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/kthlargest/FindKthLargest.java b/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/kthlargest/FindKthLargest.java
similarity index 98%
rename from algorithms/src/main/java/com/baeldung/algorithms/kthlargest/FindKthLargest.java
rename to algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/kthlargest/FindKthLargest.java
index 822abdae02..7054979ada 100644
--- a/algorithms/src/main/java/com/baeldung/algorithms/kthlargest/FindKthLargest.java
+++ b/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/kthlargest/FindKthLargest.java
@@ -98,7 +98,7 @@ public class FindKthLargest {
private int randomPartition(Integer arr[], int left, int right) {
int n = right - left + 1;
- int pivot = (int) (Math.random()) % n;
+ int pivot = (int) (Math.random() * n);
swap(arr, left + pivot, right);
return partition(arr, left, right);
}
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/mcts/montecarlo/MonteCarloTreeSearch.java b/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/mcts/montecarlo/MonteCarloTreeSearch.java
similarity index 100%
rename from algorithms/src/main/java/com/baeldung/algorithms/mcts/montecarlo/MonteCarloTreeSearch.java
rename to algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/mcts/montecarlo/MonteCarloTreeSearch.java
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/mcts/montecarlo/State.java b/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/mcts/montecarlo/State.java
similarity index 100%
rename from algorithms/src/main/java/com/baeldung/algorithms/mcts/montecarlo/State.java
rename to algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/mcts/montecarlo/State.java
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/mcts/montecarlo/UCT.java b/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/mcts/montecarlo/UCT.java
similarity index 100%
rename from algorithms/src/main/java/com/baeldung/algorithms/mcts/montecarlo/UCT.java
rename to algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/mcts/montecarlo/UCT.java
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/mcts/tictactoe/Board.java b/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/mcts/tictactoe/Board.java
similarity index 100%
rename from algorithms/src/main/java/com/baeldung/algorithms/mcts/tictactoe/Board.java
rename to algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/mcts/tictactoe/Board.java
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/mcts/tictactoe/Position.java b/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/mcts/tictactoe/Position.java
similarity index 100%
rename from algorithms/src/main/java/com/baeldung/algorithms/mcts/tictactoe/Position.java
rename to algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/mcts/tictactoe/Position.java
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/mcts/tree/Node.java b/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/mcts/tree/Node.java
similarity index 100%
rename from algorithms/src/main/java/com/baeldung/algorithms/mcts/tree/Node.java
rename to algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/mcts/tree/Node.java
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/mcts/tree/Tree.java b/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/mcts/tree/Tree.java
similarity index 100%
rename from algorithms/src/main/java/com/baeldung/algorithms/mcts/tree/Tree.java
rename to algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/mcts/tree/Tree.java
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/middleelementlookup/MiddleElementLookup.java b/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/middleelementlookup/MiddleElementLookup.java
similarity index 100%
rename from algorithms/src/main/java/com/baeldung/algorithms/middleelementlookup/MiddleElementLookup.java
rename to algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/middleelementlookup/MiddleElementLookup.java
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/middleelementlookup/Node.java b/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/middleelementlookup/Node.java
similarity index 100%
rename from algorithms/src/main/java/com/baeldung/algorithms/middleelementlookup/Node.java
rename to algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/middleelementlookup/Node.java
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/minimax/GameOfBones.java b/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/minimax/GameOfBones.java
similarity index 100%
rename from algorithms/src/main/java/com/baeldung/algorithms/minimax/GameOfBones.java
rename to algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/minimax/GameOfBones.java
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/minimax/MiniMax.java b/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/minimax/MiniMax.java
similarity index 100%
rename from algorithms/src/main/java/com/baeldung/algorithms/minimax/MiniMax.java
rename to algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/minimax/MiniMax.java
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/minimax/Node.java b/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/minimax/Node.java
similarity index 100%
rename from algorithms/src/main/java/com/baeldung/algorithms/minimax/Node.java
rename to algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/minimax/Node.java
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/minimax/Tree.java b/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/minimax/Tree.java
similarity index 100%
rename from algorithms/src/main/java/com/baeldung/algorithms/minimax/Tree.java
rename to algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/minimax/Tree.java
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/multiswarm/Constants.java b/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/multiswarm/Constants.java
similarity index 100%
rename from algorithms/src/main/java/com/baeldung/algorithms/multiswarm/Constants.java
rename to algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/multiswarm/Constants.java
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/multiswarm/FitnessFunction.java b/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/multiswarm/FitnessFunction.java
similarity index 100%
rename from algorithms/src/main/java/com/baeldung/algorithms/multiswarm/FitnessFunction.java
rename to algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/multiswarm/FitnessFunction.java
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/multiswarm/Multiswarm.java b/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/multiswarm/Multiswarm.java
similarity index 100%
rename from algorithms/src/main/java/com/baeldung/algorithms/multiswarm/Multiswarm.java
rename to algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/multiswarm/Multiswarm.java
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/multiswarm/Particle.java b/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/multiswarm/Particle.java
similarity index 100%
rename from algorithms/src/main/java/com/baeldung/algorithms/multiswarm/Particle.java
rename to algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/multiswarm/Particle.java
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/multiswarm/Swarm.java b/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/multiswarm/Swarm.java
similarity index 100%
rename from algorithms/src/main/java/com/baeldung/algorithms/multiswarm/Swarm.java
rename to algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/multiswarm/Swarm.java
diff --git a/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/permutation/Permutation.java b/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/permutation/Permutation.java
new file mode 100644
index 0000000000..7fedd78ffb
--- /dev/null
+++ b/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/permutation/Permutation.java
@@ -0,0 +1,123 @@
+package com.baeldung.algorithms.permutation;
+
+import java.util.Arrays;
+import java.util.Collections;
+
+public class Permutation {
+
+ public static void printAllRecursive(T[] elements, char delimiter) {
+ printAllRecursive(elements.length, elements, delimiter);
+ }
+
+ public static 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 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 > 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 void printRandom(T[] elements, char delimiter) {
+
+ Collections.shuffle(Arrays.asList(elements));
+ printArray(elements, delimiter);
+ }
+
+ private static void swap(T[] elements, int a, int b) {
+
+ T tmp = elements[a];
+ elements[a] = elements[b];
+ elements[b] = tmp;
+ }
+
+ private static 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, ';');
+ }
+}
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/string/EnglishAlphabetLetters.java b/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/string/EnglishAlphabetLetters.java
similarity index 100%
rename from algorithms/src/main/java/com/baeldung/algorithms/string/EnglishAlphabetLetters.java
rename to algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/string/EnglishAlphabetLetters.java
diff --git a/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/string/LongestSubstringNonRepeatingCharacters.java b/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/string/LongestSubstringNonRepeatingCharacters.java
new file mode 100644
index 0000000000..cd1f3e94d5
--- /dev/null
+++ b/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/string/LongestSubstringNonRepeatingCharacters.java
@@ -0,0 +1,54 @@
+package com.baeldung.algorithms.string;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+public class LongestSubstringNonRepeatingCharacters {
+
+ public static String getUniqueCharacterSubstringBruteForce(String input) {
+ String output = "";
+ for (int start = 0; start < input.length(); start++) {
+ Set visited = new HashSet<>();
+ int end = start;
+ for (; end < input.length(); end++) {
+ char currChar = input.charAt(end);
+ if (visited.contains(currChar)) {
+ break;
+ } else {
+ visited.add(currChar);
+ }
+ }
+ if (output.length() < end - start + 1) {
+ output = input.substring(start, end);
+ }
+ }
+ return output;
+ }
+
+ public static String getUniqueCharacterSubstring(String input) {
+ Map visited = new HashMap<>();
+ String output = "";
+ for (int start = 0, end = 0; end < input.length(); end++) {
+ char currChar = input.charAt(end);
+ if (visited.containsKey(currChar)) {
+ start = Math.max(visited.get(currChar) + 1, start);
+ }
+ if (output.length() < end - start + 1) {
+ output = input.substring(start, end + 1);
+ }
+ visited.put(currChar, end);
+ }
+ return output;
+ }
+
+ public static void main(String[] args) {
+ if(args.length > 0) {
+ System.out.println(getUniqueCharacterSubstring(args[0]));
+ } else {
+ System.err.println("This program expects command-line input. Please try again!");
+ }
+ }
+
+}
diff --git a/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/string/SubstringPalindrome.java b/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/string/SubstringPalindrome.java
new file mode 100644
index 0000000000..b3d142eb07
--- /dev/null
+++ b/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/string/SubstringPalindrome.java
@@ -0,0 +1,90 @@
+package com.baeldung.algorithms.string;
+
+import java.util.HashSet;
+import java.util.Set;
+
+public class SubstringPalindrome {
+
+ public Set findAllPalindromesUsingCenter(String input) {
+ final Set palindromes = new HashSet<>();
+ if (input == null || input.isEmpty()) {
+ return palindromes;
+ }
+ if (input.length() == 1) {
+ palindromes.add(input);
+ return palindromes;
+ }
+ for (int i = 0; i < input.length(); i++) {
+ palindromes.addAll(findPalindromes(input, i, i + 1));
+ palindromes.addAll(findPalindromes(input, i, i));
+ }
+ return palindromes;
+ }
+
+ private Set findPalindromes(String input, int low, int high) {
+ Set result = new HashSet<>();
+ while (low >= 0 && high < input.length() && input.charAt(low) == input.charAt(high)) {
+ result.add(input.substring(low, high + 1));
+ low--;
+ high++;
+ }
+ return result;
+ }
+
+ public Set findAllPalindromesUsingBruteForceApproach(String input) {
+ Set palindromes = new HashSet<>();
+ if (input == null || input.isEmpty()) {
+ return palindromes;
+ }
+ if (input.length() == 1) {
+ palindromes.add(input);
+ return palindromes;
+ }
+ for (int i = 0; i < input.length(); i++) {
+ for (int j = i + 1; j <= input.length(); j++)
+ if (isPalindrome(input.substring(i, j))) {
+ palindromes.add(input.substring(i, j));
+ }
+ }
+ return palindromes;
+ }
+
+ private boolean isPalindrome(String input) {
+ StringBuilder plain = new StringBuilder(input);
+ StringBuilder reverse = plain.reverse();
+ return (reverse.toString()).equals(input);
+ }
+
+ public Set findAllPalindromesUsingManachersAlgorithm(String input) {
+ Set palindromes = new HashSet<>();
+ String formattedInput = "@" + input + "#";
+ char inputCharArr[] = formattedInput.toCharArray();
+ int max;
+ int radius[][] = new int[2][input.length() + 1];
+ for (int j = 0; j <= 1; j++) {
+ radius[j][0] = max = 0;
+ int i = 1;
+ while (i <= input.length()) {
+ palindromes.add(Character.toString(inputCharArr[i]));
+ while (inputCharArr[i - max - 1] == inputCharArr[i + j + max])
+ max++;
+ radius[j][i] = max;
+ int k = 1;
+ while ((radius[j][i - k] != max - k) && (k < max)) {
+ radius[j][i + k] = Math.min(radius[j][i - k], max - k);
+ k++;
+ }
+ max = Math.max(max - k, 0);
+ i += k;
+ }
+ }
+ for (int i = 1; i <= input.length(); i++) {
+ for (int j = 0; j <= 1; j++) {
+ for (max = radius[j][i]; max > 0; max--) {
+ palindromes.add(input.substring(i - max - 1, max + j + i - 1));
+ }
+ }
+ }
+ return palindromes;
+ }
+}
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/string/search/StringSearchAlgorithms.java b/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/string/search/StringSearchAlgorithms.java
similarity index 100%
rename from algorithms/src/main/java/com/baeldung/algorithms/string/search/StringSearchAlgorithms.java
rename to algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/string/search/StringSearchAlgorithms.java
diff --git a/algorithms/src/main/resources/logback.xml b/algorithms-miscellaneous-1/src/main/resources/logback.xml
similarity index 100%
rename from algorithms/src/main/resources/logback.xml
rename to algorithms-miscellaneous-1/src/main/resources/logback.xml
diff --git a/algorithms/src/test/java/com/baeldung/algorithms/HillClimbingAlgorithmUnitTest.java b/algorithms-miscellaneous-1/src/test/java/com/baeldung/algorithms/HillClimbingAlgorithmUnitTest.java
similarity index 100%
rename from algorithms/src/test/java/com/baeldung/algorithms/HillClimbingAlgorithmUnitTest.java
rename to algorithms-miscellaneous-1/src/test/java/com/baeldung/algorithms/HillClimbingAlgorithmUnitTest.java
diff --git a/algorithms/src/test/java/com/baeldung/algorithms/MiddleElementLookupUnitTest.java b/algorithms-miscellaneous-1/src/test/java/com/baeldung/algorithms/MiddleElementLookupUnitTest.java
similarity index 100%
rename from algorithms/src/test/java/com/baeldung/algorithms/MiddleElementLookupUnitTest.java
rename to algorithms-miscellaneous-1/src/test/java/com/baeldung/algorithms/MiddleElementLookupUnitTest.java
diff --git a/algorithms/src/test/java/com/baeldung/algorithms/RtFiniteStateMachineLongRunningUnitTest.java b/algorithms-miscellaneous-1/src/test/java/com/baeldung/algorithms/RtFiniteStateMachineLongRunningUnitTest.java
similarity index 100%
rename from algorithms/src/test/java/com/baeldung/algorithms/RtFiniteStateMachineLongRunningUnitTest.java
rename to algorithms-miscellaneous-1/src/test/java/com/baeldung/algorithms/RtFiniteStateMachineLongRunningUnitTest.java
diff --git a/algorithms/src/test/java/com/baeldung/algorithms/StringSearchAlgorithmsUnitTest.java b/algorithms-miscellaneous-1/src/test/java/com/baeldung/algorithms/StringSearchAlgorithmsUnitTest.java
similarity index 100%
rename from algorithms/src/test/java/com/baeldung/algorithms/StringSearchAlgorithmsUnitTest.java
rename to algorithms-miscellaneous-1/src/test/java/com/baeldung/algorithms/StringSearchAlgorithmsUnitTest.java
diff --git a/algorithms/src/test/java/com/baeldung/algorithms/binarysearch/BinarySearchUnitTest.java b/algorithms-miscellaneous-1/src/test/java/com/baeldung/algorithms/binarysearch/BinarySearchUnitTest.java
similarity index 100%
rename from algorithms/src/test/java/com/baeldung/algorithms/binarysearch/BinarySearchUnitTest.java
rename to algorithms-miscellaneous-1/src/test/java/com/baeldung/algorithms/binarysearch/BinarySearchUnitTest.java
diff --git a/algorithms/src/test/java/com/baeldung/algorithms/kthlargest/FindKthLargestUnitTest.java b/algorithms-miscellaneous-1/src/test/java/com/baeldung/algorithms/kthlargest/FindKthLargestUnitTest.java
similarity index 100%
rename from algorithms/src/test/java/com/baeldung/algorithms/kthlargest/FindKthLargestUnitTest.java
rename to algorithms-miscellaneous-1/src/test/java/com/baeldung/algorithms/kthlargest/FindKthLargestUnitTest.java
diff --git a/algorithms/src/test/java/com/baeldung/algorithms/mcts/MCTSUnitTest.java b/algorithms-miscellaneous-1/src/test/java/com/baeldung/algorithms/mcts/MCTSUnitTest.java
similarity index 100%
rename from algorithms/src/test/java/com/baeldung/algorithms/mcts/MCTSUnitTest.java
rename to algorithms-miscellaneous-1/src/test/java/com/baeldung/algorithms/mcts/MCTSUnitTest.java
diff --git a/algorithms/src/test/java/com/baeldung/algorithms/minimax/MinimaxUnitTest.java b/algorithms-miscellaneous-1/src/test/java/com/baeldung/algorithms/minimax/MinimaxUnitTest.java
similarity index 100%
rename from algorithms/src/test/java/com/baeldung/algorithms/minimax/MinimaxUnitTest.java
rename to algorithms-miscellaneous-1/src/test/java/com/baeldung/algorithms/minimax/MinimaxUnitTest.java
diff --git a/algorithms/src/test/java/com/baeldung/algorithms/multiswarm/LolFitnessFunction.java b/algorithms-miscellaneous-1/src/test/java/com/baeldung/algorithms/multiswarm/LolFitnessFunction.java
similarity index 100%
rename from algorithms/src/test/java/com/baeldung/algorithms/multiswarm/LolFitnessFunction.java
rename to algorithms-miscellaneous-1/src/test/java/com/baeldung/algorithms/multiswarm/LolFitnessFunction.java
diff --git a/algorithms/src/test/java/com/baeldung/algorithms/multiswarm/MultiswarmUnitTest.java b/algorithms-miscellaneous-1/src/test/java/com/baeldung/algorithms/multiswarm/MultiswarmUnitTest.java
similarity index 100%
rename from algorithms/src/test/java/com/baeldung/algorithms/multiswarm/MultiswarmUnitTest.java
rename to algorithms-miscellaneous-1/src/test/java/com/baeldung/algorithms/multiswarm/MultiswarmUnitTest.java
diff --git a/algorithms/src/test/java/com/baeldung/algorithms/string/EnglishAlphabetLettersUnitTest.java b/algorithms-miscellaneous-1/src/test/java/com/baeldung/algorithms/string/EnglishAlphabetLettersUnitTest.java
similarity index 100%
rename from algorithms/src/test/java/com/baeldung/algorithms/string/EnglishAlphabetLettersUnitTest.java
rename to algorithms-miscellaneous-1/src/test/java/com/baeldung/algorithms/string/EnglishAlphabetLettersUnitTest.java
diff --git a/algorithms-miscellaneous-1/src/test/java/com/baeldung/algorithms/string/LongestSubstringNonRepeatingCharactersUnitTest.java b/algorithms-miscellaneous-1/src/test/java/com/baeldung/algorithms/string/LongestSubstringNonRepeatingCharactersUnitTest.java
new file mode 100644
index 0000000000..9f1e6a2519
--- /dev/null
+++ b/algorithms-miscellaneous-1/src/test/java/com/baeldung/algorithms/string/LongestSubstringNonRepeatingCharactersUnitTest.java
@@ -0,0 +1,31 @@
+package com.baeldung.algorithms.string;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static com.baeldung.algorithms.string.LongestSubstringNonRepeatingCharacters.getUniqueCharacterSubstring;
+import static com.baeldung.algorithms.string.LongestSubstringNonRepeatingCharacters.getUniqueCharacterSubstringBruteForce;
+
+public class LongestSubstringNonRepeatingCharactersUnitTest {
+
+ @Test
+ void givenString_whenGetUniqueCharacterSubstringBruteForceCalled_thenResultFoundAsExpectedUnitTest() {
+ assertEquals("", getUniqueCharacterSubstringBruteForce(""));
+ assertEquals("A", getUniqueCharacterSubstringBruteForce("A"));
+ assertEquals("ABCDEF", getUniqueCharacterSubstringBruteForce("AABCDEF"));
+ assertEquals("ABCDEF", getUniqueCharacterSubstringBruteForce("ABCDEFF"));
+ assertEquals("NGISAWE", getUniqueCharacterSubstringBruteForce("CODINGISAWESOME"));
+ assertEquals("be coding", getUniqueCharacterSubstringBruteForce("always be coding"));
+ }
+
+ @Test
+ void givenString_whenGetUniqueCharacterSubstringCalled_thenResultFoundAsExpectedUnitTest() {
+ assertEquals("", getUniqueCharacterSubstring(""));
+ assertEquals("A", getUniqueCharacterSubstring("A"));
+ assertEquals("ABCDEF", getUniqueCharacterSubstring("AABCDEF"));
+ assertEquals("ABCDEF", getUniqueCharacterSubstring("ABCDEFF"));
+ assertEquals("NGISAWE", getUniqueCharacterSubstring("CODINGISAWESOME"));
+ assertEquals("be coding", getUniqueCharacterSubstring("always be coding"));
+ }
+
+}
diff --git a/algorithms-miscellaneous-1/src/test/java/com/baeldung/algorithms/string/SubstringPalindromeUnitTest.java b/algorithms-miscellaneous-1/src/test/java/com/baeldung/algorithms/string/SubstringPalindromeUnitTest.java
new file mode 100644
index 0000000000..8d225f67fa
--- /dev/null
+++ b/algorithms-miscellaneous-1/src/test/java/com/baeldung/algorithms/string/SubstringPalindromeUnitTest.java
@@ -0,0 +1,83 @@
+package com.baeldung.algorithms.string;
+
+import static org.junit.Assert.assertEquals;
+import java.util.HashSet;
+import java.util.Set;
+import org.junit.Test;
+
+public class SubstringPalindromeUnitTest {
+
+ private static final String INPUT_BUBBLE = "bubble";
+ private static final String INPUT_CIVIC = "civic";
+ private static final String INPUT_INDEED = "indeed";
+ private static final String INPUT_ABABAC = "ababac";
+
+ Set EXPECTED_PALINDROME_BUBBLE = new HashSet() {
+ {
+ add("b");
+ add("u");
+ add("l");
+ add("e");
+ add("bb");
+ add("bub");
+ }
+ };
+
+ Set EXPECTED_PALINDROME_CIVIC = new HashSet() {
+ {
+ add("civic");
+ add("ivi");
+ add("i");
+ add("c");
+ add("v");
+ }
+ };
+
+ Set EXPECTED_PALINDROME_INDEED = new HashSet() {
+ {
+ add("i");
+ add("n");
+ add("d");
+ add("e");
+ add("ee");
+ add("deed");
+ }
+ };
+
+ Set EXPECTED_PALINDROME_ABABAC = new HashSet() {
+ {
+ add("a");
+ add("b");
+ add("c");
+ add("aba");
+ add("bab");
+ add("ababa");
+ }
+ };
+
+ private SubstringPalindrome palindrome = new SubstringPalindrome();
+
+ @Test
+ public void whenUsingManachersAlgorithm_thenFindsAllPalindromes() {
+ assertEquals(EXPECTED_PALINDROME_BUBBLE, palindrome.findAllPalindromesUsingManachersAlgorithm(INPUT_BUBBLE));
+ assertEquals(EXPECTED_PALINDROME_INDEED, palindrome.findAllPalindromesUsingManachersAlgorithm(INPUT_INDEED));
+ assertEquals(EXPECTED_PALINDROME_CIVIC, palindrome.findAllPalindromesUsingManachersAlgorithm(INPUT_CIVIC));
+ assertEquals(EXPECTED_PALINDROME_ABABAC, palindrome.findAllPalindromesUsingManachersAlgorithm(INPUT_ABABAC));
+ }
+
+ @Test
+ public void whenUsingCenterApproach_thenFindsAllPalindromes() {
+ assertEquals(EXPECTED_PALINDROME_BUBBLE, palindrome.findAllPalindromesUsingCenter(INPUT_BUBBLE));
+ assertEquals(EXPECTED_PALINDROME_INDEED, palindrome.findAllPalindromesUsingCenter(INPUT_INDEED));
+ assertEquals(EXPECTED_PALINDROME_CIVIC, palindrome.findAllPalindromesUsingCenter(INPUT_CIVIC));
+ assertEquals(EXPECTED_PALINDROME_ABABAC, palindrome.findAllPalindromesUsingCenter(INPUT_ABABAC));
+ }
+
+ @Test
+ public void whenUsingBruteForceApproach_thenFindsAllPalindromes() {
+ assertEquals(EXPECTED_PALINDROME_BUBBLE, palindrome.findAllPalindromesUsingBruteForceApproach(INPUT_BUBBLE));
+ assertEquals(EXPECTED_PALINDROME_INDEED, palindrome.findAllPalindromesUsingBruteForceApproach(INPUT_INDEED));
+ assertEquals(EXPECTED_PALINDROME_CIVIC, palindrome.findAllPalindromesUsingBruteForceApproach(INPUT_CIVIC));
+ assertEquals(EXPECTED_PALINDROME_ABABAC, palindrome.findAllPalindromesUsingBruteForceApproach(INPUT_ABABAC));
+ }
+}
diff --git a/algorithms/src/test/java/com/baeldung/algorithms/support/MayFailRule.java b/algorithms-miscellaneous-1/src/test/java/com/baeldung/algorithms/support/MayFailRule.java
similarity index 100%
rename from algorithms/src/test/java/com/baeldung/algorithms/support/MayFailRule.java
rename to algorithms-miscellaneous-1/src/test/java/com/baeldung/algorithms/support/MayFailRule.java
diff --git a/algorithms-miscellaneous-2/.gitignore b/algorithms-miscellaneous-2/.gitignore
new file mode 100644
index 0000000000..30b2b7442c
--- /dev/null
+++ b/algorithms-miscellaneous-2/.gitignore
@@ -0,0 +1,4 @@
+/target/
+.settings/
+.classpath
+.project
\ No newline at end of file
diff --git a/algorithms-miscellaneous-2/README.md b/algorithms-miscellaneous-2/README.md
new file mode 100644
index 0000000000..462644dddb
--- /dev/null
+++ b/algorithms-miscellaneous-2/README.md
@@ -0,0 +1,19 @@
+## Relevant articles:
+
+- [Dijkstra Algorithm in Java](http://www.baeldung.com/java-dijkstra)
+- [Introduction to Cobertura](http://www.baeldung.com/cobertura)
+- [Test a Linked List for Cyclicity](http://www.baeldung.com/java-linked-list-cyclicity)
+- [Introduction to JGraphT](http://www.baeldung.com/jgrapht)
+- [A Maze Solver in Java](http://www.baeldung.com/java-solve-maze)
+- [Create a Sudoku Solver in Java](http://www.baeldung.com/java-sudoku)
+- [Displaying Money Amounts in Words](http://www.baeldung.com/java-money-into-words)
+- [A Collaborative Filtering Recommendation System in Java](http://www.baeldung.com/java-collaborative-filtering-recommendations)
+- [Check If Two Rectangles Overlap In Java](https://www.baeldung.com/java-check-if-two-rectangles-overlap)
+- [Calculate the Distance Between Two Points in Java](https://www.baeldung.com/java-distance-between-two-points)
+- [Find the Intersection of Two Lines in Java](https://www.baeldung.com/java-intersection-of-two-lines)
+- [Round Up to the Nearest Hundred](https://www.baeldung.com/java-round-up-nearest-hundred)
+- [Calculate Percentage in Java](https://www.baeldung.com/java-calculate-percentage)
+- [Converting Between Byte Arrays and Hexadecimal Strings in Java](https://www.baeldung.com/java-byte-arrays-hex-strings)
+- [Convert Latitude and Longitude to a 2D Point in Java](https://www.baeldung.com/java-convert-latitude-longitude)
+- [Reversing a Binary Tree in Java](https://www.baeldung.com/java-reversing-a-binary-tree)
+- [Find If Two Numbers Are Relatively Prime in Java](https://www.baeldung.com/java-two-relatively-prime-numbers)
diff --git a/algorithms-miscellaneous-2/pom.xml b/algorithms-miscellaneous-2/pom.xml
new file mode 100644
index 0000000000..e85dd456a3
--- /dev/null
+++ b/algorithms-miscellaneous-2/pom.xml
@@ -0,0 +1,96 @@
+
+ 4.0.0
+ algorithms-miscellaneous-2
+ 0.0.1-SNAPSHOT
+ algorithms-miscellaneous-2
+
+
+ com.baeldung
+ parent-modules
+ 1.0.0-SNAPSHOT
+
+
+
+
+ org.apache.commons
+ commons-math3
+ ${commons-math3.version}
+
+
+ commons-codec
+ commons-codec
+ ${commons-codec.version}
+
+
+ org.projectlombok
+ lombok
+ ${lombok.version}
+ provided
+
+
+ org.jgrapht
+ jgrapht-core
+ ${org.jgrapht.core.version}
+
+
+ org.jgrapht
+ jgrapht-ext
+ ${org.jgrapht.ext.version}
+
+
+ pl.allegro.finance
+ tradukisto
+ ${tradukisto.version}
+
+
+ org.assertj
+ assertj-core
+ ${org.assertj.core.version}
+ test
+
+
+
+
+
+
+
+ org.codehaus.mojo
+ exec-maven-plugin
+ ${exec-maven-plugin.version}
+
+
+
+
+
+
+
+
+ org.codehaus.mojo
+ cobertura-maven-plugin
+ ${cobertura-maven-plugin.version}
+
+
+
+ com/baeldung/algorithms/dijkstra/*
+
+
+ com/baeldung/algorithms/dijkstra/*
+
+
+
+
+
+
+
+
+ 3.6.1
+ 1.0.1
+ 1.0.1
+ 1.0.1
+ 3.9.0
+ 1.11
+ 2.7
+
+
+
\ No newline at end of file
diff --git a/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/RunAlgorithm.java b/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/RunAlgorithm.java
new file mode 100644
index 0000000000..a1a096bc30
--- /dev/null
+++ b/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/RunAlgorithm.java
@@ -0,0 +1,28 @@
+package com.baeldung.algorithms;
+
+import java.util.Scanner;
+
+import com.baeldung.algorithms.slope_one.SlopeOne;
+
+public class RunAlgorithm {
+
+ public static void main(String[] args) throws InstantiationException, IllegalAccessException {
+ Scanner in = new Scanner(System.in);
+ System.out.println("1 - Slope One");
+ System.out.println("2 - Dijkstra");
+ int decision = in.nextInt();
+ switch (decision) {
+ case 1:
+ SlopeOne.slopeOne(3);
+ break;
+ case 2:
+ System.out.println("Please run the DijkstraAlgorithmLongRunningUnitTest.");
+ break;
+ default:
+ System.out.println("Unknown option");
+ break;
+ }
+ in.close();
+ }
+
+}
diff --git a/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/conversion/HexStringConverter.java b/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/conversion/HexStringConverter.java
new file mode 100644
index 0000000000..d3e251d3fd
--- /dev/null
+++ b/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/conversion/HexStringConverter.java
@@ -0,0 +1,110 @@
+package com.baeldung.algorithms.conversion;
+
+import java.math.BigInteger;
+
+import javax.xml.bind.DatatypeConverter;
+
+import org.apache.commons.codec.DecoderException;
+import org.apache.commons.codec.binary.Hex;
+
+import com.google.common.io.BaseEncoding;
+
+public class HexStringConverter {
+
+ /**
+ * Create a byte Array from String of hexadecimal digits using Character conversion
+ * @param hexString - Hexadecimal digits as String
+ * @return Desired byte Array
+ */
+ public byte[] decodeHexString(String hexString) {
+ if (hexString.length() % 2 == 1) {
+ throw new IllegalArgumentException("Invalid hexadecimal String supplied.");
+ }
+ byte[] bytes = new byte[hexString.length() / 2];
+
+ for (int i = 0; i < hexString.length(); i += 2) {
+ bytes[i / 2] = hexToByte(hexString.substring(i, i + 2));
+ }
+ return bytes;
+ }
+
+ /**
+ * Create a String of hexadecimal digits from a byte Array using Character conversion
+ * @param byteArray - The byte Array
+ * @return Desired String of hexadecimal digits in lower case
+ */
+ public String encodeHexString(byte[] byteArray) {
+ StringBuffer hexStringBuffer = new StringBuffer();
+ for (int i = 0; i < byteArray.length; i++) {
+ hexStringBuffer.append(byteToHex(byteArray[i]));
+ }
+ return hexStringBuffer.toString();
+ }
+
+ public String byteToHex(byte num) {
+ char[] hexDigits = new char[2];
+ hexDigits[0] = Character.forDigit((num >> 4) & 0xF, 16);
+ hexDigits[1] = Character.forDigit((num & 0xF), 16);
+ return new String(hexDigits);
+ }
+
+ public byte hexToByte(String hexString) {
+ int firstDigit = toDigit(hexString.charAt(0));
+ int secondDigit = toDigit(hexString.charAt(1));
+ return (byte) ((firstDigit << 4) + secondDigit);
+ }
+
+ private int toDigit(char hexChar) {
+ int digit = Character.digit(hexChar, 16);
+ if(digit == -1) {
+ throw new IllegalArgumentException("Invalid Hexadecimal Character: "+ hexChar);
+ }
+ return digit;
+ }
+
+ public String encodeUsingBigIntegerToString(byte[] bytes) {
+ BigInteger bigInteger = new BigInteger(1, bytes);
+ return bigInteger.toString(16);
+ }
+
+ public String encodeUsingBigIntegerStringFormat(byte[] bytes) {
+ BigInteger bigInteger = new BigInteger(1, bytes);
+ return String.format("%0" + (bytes.length << 1) + "x", bigInteger);
+ }
+
+ public byte[] decodeUsingBigInteger(String hexString) {
+ byte[] byteArray = new BigInteger(hexString, 16).toByteArray();
+ if (byteArray[0] == 0) {
+ byte[] output = new byte[byteArray.length - 1];
+ System.arraycopy(byteArray, 1, output, 0, output.length);
+ return output;
+ }
+ return byteArray;
+ }
+
+ public String encodeUsingDataTypeConverter(byte[] bytes) {
+ return DatatypeConverter.printHexBinary(bytes);
+ }
+
+ public byte[] decodeUsingDataTypeConverter(String hexString) {
+ return DatatypeConverter.parseHexBinary(hexString);
+ }
+
+ public String encodeUsingApacheCommons(byte[] bytes) throws DecoderException {
+ return Hex.encodeHexString(bytes);
+ }
+
+ public byte[] decodeUsingApacheCommons(String hexString) throws DecoderException {
+ return Hex.decodeHex(hexString);
+ }
+
+ public String encodeUsingGuava(byte[] bytes) {
+ return BaseEncoding.base16()
+ .encode(bytes);
+ }
+
+ public byte[] decodeUsingGuava(String hexString) {
+ return BaseEncoding.base16()
+ .decode(hexString.toUpperCase());
+ }
+}
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/editdistance/EditDistanceBase.java b/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/editdistance/EditDistanceBase.java
similarity index 95%
rename from algorithms/src/main/java/com/baeldung/algorithms/editdistance/EditDistanceBase.java
rename to algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/editdistance/EditDistanceBase.java
index ec66621928..4df1de9994 100644
--- a/algorithms/src/main/java/com/baeldung/algorithms/editdistance/EditDistanceBase.java
+++ b/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/editdistance/EditDistanceBase.java
@@ -1,15 +1,15 @@
-package com.baeldung.algorithms.editdistance;
-
-import java.util.Arrays;
-
-public class EditDistanceBase {
-
- static int costOfSubstitution(char a, char b) {
- return a == b ? 0 : 1;
- }
-
- static int min(int... numbers) {
- return Arrays.stream(numbers)
- .min().orElse(Integer.MAX_VALUE);
- }
-}
+package com.baeldung.algorithms.editdistance;
+
+import java.util.Arrays;
+
+public class EditDistanceBase {
+
+ static int costOfSubstitution(char a, char b) {
+ return a == b ? 0 : 1;
+ }
+
+ static int min(int... numbers) {
+ return Arrays.stream(numbers)
+ .min().orElse(Integer.MAX_VALUE);
+ }
+}
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/editdistance/EditDistanceDynamicProgramming.java b/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/editdistance/EditDistanceDynamicProgramming.java
similarity index 96%
rename from algorithms/src/main/java/com/baeldung/algorithms/editdistance/EditDistanceDynamicProgramming.java
rename to algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/editdistance/EditDistanceDynamicProgramming.java
index 1f8824c4f4..10ce43bf5f 100644
--- a/algorithms/src/main/java/com/baeldung/algorithms/editdistance/EditDistanceDynamicProgramming.java
+++ b/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/editdistance/EditDistanceDynamicProgramming.java
@@ -1,26 +1,26 @@
-package com.baeldung.algorithms.editdistance;
-
-public class EditDistanceDynamicProgramming extends EditDistanceBase {
-
- static int calculate(String x, String y) {
- int[][] dp = new int[x.length() + 1][y.length() + 1];
-
- for (int i = 0; i <= x.length(); i++) {
- for (int j = 0; j <= y.length(); j++) {
- if (i == 0)
- dp[i][j] = j;
-
- else if (j == 0)
- dp[i][j] = i;
-
- else {
- dp[i][j] = min(dp[i - 1][j - 1]
- + costOfSubstitution(x.charAt(i - 1), y.charAt(j - 1)),
- dp[i - 1][j] + 1, dp[i][j - 1] + 1);
- }
- }
- }
-
- return dp[x.length()][y.length()];
- }
-}
+package com.baeldung.algorithms.editdistance;
+
+public class EditDistanceDynamicProgramming extends EditDistanceBase {
+
+ static int calculate(String x, String y) {
+ int[][] dp = new int[x.length() + 1][y.length() + 1];
+
+ for (int i = 0; i <= x.length(); i++) {
+ for (int j = 0; j <= y.length(); j++) {
+ if (i == 0)
+ dp[i][j] = j;
+
+ else if (j == 0)
+ dp[i][j] = i;
+
+ else {
+ dp[i][j] = min(dp[i - 1][j - 1]
+ + costOfSubstitution(x.charAt(i - 1), y.charAt(j - 1)),
+ dp[i - 1][j] + 1, dp[i][j - 1] + 1);
+ }
+ }
+ }
+
+ return dp[x.length()][y.length()];
+ }
+}
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/editdistance/EditDistanceRecursive.java b/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/editdistance/EditDistanceRecursive.java
similarity index 96%
rename from algorithms/src/main/java/com/baeldung/algorithms/editdistance/EditDistanceRecursive.java
rename to algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/editdistance/EditDistanceRecursive.java
index 8ed48dc554..fc907c45f8 100644
--- a/algorithms/src/main/java/com/baeldung/algorithms/editdistance/EditDistanceRecursive.java
+++ b/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/editdistance/EditDistanceRecursive.java
@@ -1,21 +1,21 @@
-package com.baeldung.algorithms.editdistance;
-
-public class EditDistanceRecursive extends EditDistanceBase {
-
- static int calculate(String x, String y) {
-
- if (x.isEmpty()) {
- return y.length();
- }
-
- if (y.isEmpty()) {
- return x.length();
- }
-
- int substitution = calculate(x.substring(1), y.substring(1)) + costOfSubstitution(x.charAt(0), y.charAt(0));
- int insertion = calculate(x, y.substring(1)) + 1;
- int deletion = calculate(x.substring(1), y) + 1;
-
- return min(substitution, insertion, deletion);
- }
-}
+package com.baeldung.algorithms.editdistance;
+
+public class EditDistanceRecursive extends EditDistanceBase {
+
+ static int calculate(String x, String y) {
+
+ if (x.isEmpty()) {
+ return y.length();
+ }
+
+ if (y.isEmpty()) {
+ return x.length();
+ }
+
+ int substitution = calculate(x.substring(1), y.substring(1)) + costOfSubstitution(x.charAt(0), y.charAt(0));
+ int insertion = calculate(x, y.substring(1)) + 1;
+ int deletion = calculate(x.substring(1), y) + 1;
+
+ return min(substitution, insertion, deletion);
+ }
+}
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/ga/dijkstra/Dijkstra.java b/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/ga/dijkstra/Dijkstra.java
similarity index 100%
rename from algorithms/src/main/java/com/baeldung/algorithms/ga/dijkstra/Dijkstra.java
rename to algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/ga/dijkstra/Dijkstra.java
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/ga/dijkstra/Graph.java b/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/ga/dijkstra/Graph.java
similarity index 100%
rename from algorithms/src/main/java/com/baeldung/algorithms/ga/dijkstra/Graph.java
rename to algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/ga/dijkstra/Graph.java
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/ga/dijkstra/Node.java b/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/ga/dijkstra/Node.java
similarity index 100%
rename from algorithms/src/main/java/com/baeldung/algorithms/ga/dijkstra/Node.java
rename to algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/ga/dijkstra/Node.java
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/linkedlist/CycleDetectionBruteForce.java b/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/linkedlist/CycleDetectionBruteForce.java
similarity index 96%
rename from algorithms/src/main/java/com/baeldung/algorithms/linkedlist/CycleDetectionBruteForce.java
rename to algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/linkedlist/CycleDetectionBruteForce.java
index 1df425ad2e..907bd9042d 100644
--- a/algorithms/src/main/java/com/baeldung/algorithms/linkedlist/CycleDetectionBruteForce.java
+++ b/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/linkedlist/CycleDetectionBruteForce.java
@@ -1,38 +1,38 @@
-package com.baeldung.algorithms.linkedlist;
-
-public class CycleDetectionBruteForce {
-
- public static CycleDetectionResult detectCycle(Node head) {
- if (head == null) {
- return new CycleDetectionResult<>(false, null);
- }
-
- Node it1 = head;
- int nodesTraversedByOuter = 0;
- while (it1 != null && it1.next != null) {
- it1 = it1.next;
- nodesTraversedByOuter++;
-
- int x = nodesTraversedByOuter;
- Node it2 = head;
- int noOfTimesCurrentNodeVisited = 0;
-
- while (x > 0) {
- it2 = it2.next;
-
- if (it2 == it1) {
- noOfTimesCurrentNodeVisited++;
- }
-
- if (noOfTimesCurrentNodeVisited == 2) {
- return new CycleDetectionResult<>(true, it1);
- }
-
- x--;
- }
- }
-
- return new CycleDetectionResult<>(false, null);
- }
-
-}
+package com.baeldung.algorithms.linkedlist;
+
+public class CycleDetectionBruteForce {
+
+ public static CycleDetectionResult detectCycle(Node head) {
+ if (head == null) {
+ return new CycleDetectionResult<>(false, null);
+ }
+
+ Node it1 = head;
+ int nodesTraversedByOuter = 0;
+ while (it1 != null && it1.next != null) {
+ it1 = it1.next;
+ nodesTraversedByOuter++;
+
+ int x = nodesTraversedByOuter;
+ Node it2 = head;
+ int noOfTimesCurrentNodeVisited = 0;
+
+ while (x > 0) {
+ it2 = it2.next;
+
+ if (it2 == it1) {
+ noOfTimesCurrentNodeVisited++;
+ }
+
+ if (noOfTimesCurrentNodeVisited == 2) {
+ return new CycleDetectionResult<>(true, it1);
+ }
+
+ x--;
+ }
+ }
+
+ return new CycleDetectionResult<>(false, null);
+ }
+
+}
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/linkedlist/CycleDetectionByFastAndSlowIterators.java b/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/linkedlist/CycleDetectionByFastAndSlowIterators.java
similarity index 96%
rename from algorithms/src/main/java/com/baeldung/algorithms/linkedlist/CycleDetectionByFastAndSlowIterators.java
rename to algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/linkedlist/CycleDetectionByFastAndSlowIterators.java
index ab088de44a..2817f6f783 100644
--- a/algorithms/src/main/java/com/baeldung/algorithms/linkedlist/CycleDetectionByFastAndSlowIterators.java
+++ b/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/linkedlist/CycleDetectionByFastAndSlowIterators.java
@@ -1,25 +1,25 @@
-package com.baeldung.algorithms.linkedlist;
-
-public class CycleDetectionByFastAndSlowIterators {
-
- public static CycleDetectionResult detectCycle(Node head) {
- if (head == null) {
- return new CycleDetectionResult<>(false, null);
- }
-
- Node slow = head;
- Node fast = head;
-
- while (fast != null && fast.next != null) {
- slow = slow.next;
- fast = fast.next.next;
-
- if (slow == fast) {
- return new CycleDetectionResult<>(true, fast);
- }
- }
-
- return new CycleDetectionResult<>(false, null);
- }
-
-}
+package com.baeldung.algorithms.linkedlist;
+
+public class CycleDetectionByFastAndSlowIterators {
+
+ public static CycleDetectionResult detectCycle(Node head) {
+ if (head == null) {
+ return new CycleDetectionResult<>(false, null);
+ }
+
+ Node slow = head;
+ Node fast = head;
+
+ while (fast != null && fast.next != null) {
+ slow = slow.next;
+ fast = fast.next.next;
+
+ if (slow == fast) {
+ return new CycleDetectionResult<>(true, fast);
+ }
+ }
+
+ return new CycleDetectionResult<>(false, null);
+ }
+
+}
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/linkedlist/CycleDetectionByHashing.java b/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/linkedlist/CycleDetectionByHashing.java
similarity index 96%
rename from algorithms/src/main/java/com/baeldung/algorithms/linkedlist/CycleDetectionByHashing.java
rename to algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/linkedlist/CycleDetectionByHashing.java
index 90d5ecd711..fba4cad2e6 100644
--- a/algorithms/src/main/java/com/baeldung/algorithms/linkedlist/CycleDetectionByHashing.java
+++ b/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/linkedlist/CycleDetectionByHashing.java
@@ -1,27 +1,27 @@
-package com.baeldung.algorithms.linkedlist;
-
-import java.util.HashSet;
-import java.util.Set;
-
-public class CycleDetectionByHashing {
-
- public static CycleDetectionResult detectCycle(Node head) {
- if (head == null) {
- return new CycleDetectionResult<>(false, null);
- }
-
- Set> set = new HashSet<>();
- Node node = head;
-
- while (node != null) {
- if (set.contains(node)) {
- return new CycleDetectionResult<>(true, node);
- }
- set.add(node);
- node = node.next;
- }
-
- return new CycleDetectionResult<>(false, null);
- }
-
-}
+package com.baeldung.algorithms.linkedlist;
+
+import java.util.HashSet;
+import java.util.Set;
+
+public class CycleDetectionByHashing {
+
+ public static CycleDetectionResult detectCycle(Node head) {
+ if (head == null) {
+ return new CycleDetectionResult<>(false, null);
+ }
+
+ Set> set = new HashSet<>();
+ Node node = head;
+
+ while (node != null) {
+ if (set.contains(node)) {
+ return new CycleDetectionResult<>(true, node);
+ }
+ set.add(node);
+ node = node.next;
+ }
+
+ return new CycleDetectionResult<>(false, null);
+ }
+
+}
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/linkedlist/CycleDetectionResult.java b/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/linkedlist/CycleDetectionResult.java
similarity index 96%
rename from algorithms/src/main/java/com/baeldung/algorithms/linkedlist/CycleDetectionResult.java
rename to algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/linkedlist/CycleDetectionResult.java
index e7556311b3..4e258ec2ef 100644
--- a/algorithms/src/main/java/com/baeldung/algorithms/linkedlist/CycleDetectionResult.java
+++ b/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/linkedlist/CycleDetectionResult.java
@@ -1,12 +1,12 @@
-package com.baeldung.algorithms.linkedlist;
-
-public class CycleDetectionResult {
- boolean cycleExists;
- Node node;
-
- public CycleDetectionResult(boolean cycleExists, Node node) {
- super();
- this.cycleExists = cycleExists;
- this.node = node;
- }
-}
+package com.baeldung.algorithms.linkedlist;
+
+public class CycleDetectionResult {
+ boolean cycleExists;
+ Node node;
+
+ public CycleDetectionResult(boolean cycleExists, Node node) {
+ super();
+ this.cycleExists = cycleExists;
+ this.node = node;
+ }
+}
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/linkedlist/CycleRemovalBruteForce.java b/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/linkedlist/CycleRemovalBruteForce.java
similarity index 96%
rename from algorithms/src/main/java/com/baeldung/algorithms/linkedlist/CycleRemovalBruteForce.java
rename to algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/linkedlist/CycleRemovalBruteForce.java
index a2bfaee9a1..216ebcdde3 100644
--- a/algorithms/src/main/java/com/baeldung/algorithms/linkedlist/CycleRemovalBruteForce.java
+++ b/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/linkedlist/CycleRemovalBruteForce.java
@@ -1,56 +1,56 @@
-package com.baeldung.algorithms.linkedlist;
-
-public class CycleRemovalBruteForce {
-
- public static boolean detectAndRemoveCycle(Node head) {
- CycleDetectionResult result = CycleDetectionByFastAndSlowIterators.detectCycle(head);
-
- if (result.cycleExists) {
- removeCycle(result.node, head);
- }
-
- return result.cycleExists;
- }
-
- /**
- * @param loopNodeParam - reference to the node where Flyods cycle
- * finding algorithm ends, i.e. the fast and the slow iterators
- * meet.
- * @param head - reference to the head of the list
- */
- private static void removeCycle(Node loopNodeParam, Node head) {
- Node it = head;
-
- while (it != null) {
- if (isNodeReachableFromLoopNode(it, loopNodeParam)) {
- Node loopStart = it;
- findEndNodeAndBreakCycle(loopStart);
- break;
- }
- it = it.next;
- }
- }
-
- private static boolean isNodeReachableFromLoopNode(Node it, Node loopNodeParam) {
- Node loopNode = loopNodeParam;
-
- do {
- if (it == loopNode) {
- return true;
- }
- loopNode = loopNode.next;
- } while (loopNode.next != loopNodeParam);
-
- return false;
- }
-
- private static void findEndNodeAndBreakCycle(Node loopStartParam) {
- Node loopStart = loopStartParam;
-
- while (loopStart.next != loopStartParam) {
- loopStart = loopStart.next;
- }
-
- loopStart.next = null;
- }
-}
+package com.baeldung.algorithms.linkedlist;
+
+public class CycleRemovalBruteForce {
+
+ public static boolean detectAndRemoveCycle(Node head) {
+ CycleDetectionResult result = CycleDetectionByFastAndSlowIterators.detectCycle(head);
+
+ if (result.cycleExists) {
+ removeCycle(result.node, head);
+ }
+
+ return result.cycleExists;
+ }
+
+ /**
+ * @param loopNodeParam - reference to the node where Flyods cycle
+ * finding algorithm ends, i.e. the fast and the slow iterators
+ * meet.
+ * @param head - reference to the head of the list
+ */
+ private static void removeCycle(Node loopNodeParam, Node head) {
+ Node it = head;
+
+ while (it != null) {
+ if (isNodeReachableFromLoopNode(it, loopNodeParam)) {
+ Node loopStart = it;
+ findEndNodeAndBreakCycle(loopStart);
+ break;
+ }
+ it = it.next;
+ }
+ }
+
+ private static boolean isNodeReachableFromLoopNode(Node it, Node loopNodeParam) {
+ Node loopNode = loopNodeParam;
+
+ do {
+ if (it == loopNode) {
+ return true;
+ }
+ loopNode = loopNode.next;
+ } while (loopNode.next != loopNodeParam);
+
+ return false;
+ }
+
+ private static void findEndNodeAndBreakCycle(Node loopStartParam) {
+ Node loopStart = loopStartParam;
+
+ while (loopStart.next != loopStartParam) {
+ loopStart = loopStart.next;
+ }
+
+ loopStart.next = null;
+ }
+}
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/linkedlist/CycleRemovalByCountingLoopNodes.java b/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/linkedlist/CycleRemovalByCountingLoopNodes.java
similarity index 96%
rename from algorithms/src/main/java/com/baeldung/algorithms/linkedlist/CycleRemovalByCountingLoopNodes.java
rename to algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/linkedlist/CycleRemovalByCountingLoopNodes.java
index d8db37fc4c..f961feb97d 100644
--- a/algorithms/src/main/java/com/baeldung/algorithms/linkedlist/CycleRemovalByCountingLoopNodes.java
+++ b/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/linkedlist/CycleRemovalByCountingLoopNodes.java
@@ -1,44 +1,44 @@
-package com.baeldung.algorithms.linkedlist;
-
-public class CycleRemovalByCountingLoopNodes {
-
- public static boolean detectAndRemoveCycle(Node head) {
- CycleDetectionResult result = CycleDetectionByFastAndSlowIterators.detectCycle(head);
-
- if (result.cycleExists) {
- removeCycle(result.node, head);
- }
-
- return result.cycleExists;
- }
-
- private static void removeCycle(Node loopNodeParam, Node head) {
- int cycleLength = calculateCycleLength(loopNodeParam);
- Node cycleLengthAdvancedIterator = head;
- Node it = head;
-
- for (int i = 0; i < cycleLength; i++) {
- cycleLengthAdvancedIterator = cycleLengthAdvancedIterator.next;
- }
-
- while (it.next != cycleLengthAdvancedIterator.next) {
- it = it.next;
- cycleLengthAdvancedIterator = cycleLengthAdvancedIterator.next;
- }
-
- cycleLengthAdvancedIterator.next = null;
- }
-
- private static int calculateCycleLength(Node loopNodeParam) {
- Node loopNode = loopNodeParam;
- int length = 1;
-
- while (loopNode.next != loopNodeParam) {
- length++;
- loopNode = loopNode.next;
- }
-
- return length;
- }
-
-}
+package com.baeldung.algorithms.linkedlist;
+
+public class CycleRemovalByCountingLoopNodes {
+
+ public static boolean detectAndRemoveCycle(Node head) {
+ CycleDetectionResult result = CycleDetectionByFastAndSlowIterators.detectCycle(head);
+
+ if (result.cycleExists) {
+ removeCycle(result.node, head);
+ }
+
+ return result.cycleExists;
+ }
+
+ private static void removeCycle(Node loopNodeParam, Node head) {
+ int cycleLength = calculateCycleLength(loopNodeParam);
+ Node cycleLengthAdvancedIterator = head;
+ Node it = head;
+
+ for (int i = 0; i < cycleLength; i++) {
+ cycleLengthAdvancedIterator = cycleLengthAdvancedIterator.next;
+ }
+
+ while (it.next != cycleLengthAdvancedIterator.next) {
+ it = it.next;
+ cycleLengthAdvancedIterator = cycleLengthAdvancedIterator.next;
+ }
+
+ cycleLengthAdvancedIterator.next = null;
+ }
+
+ private static int calculateCycleLength(Node loopNodeParam) {
+ Node loopNode = loopNodeParam;
+ int length = 1;
+
+ while (loopNode.next != loopNodeParam) {
+ length++;
+ loopNode = loopNode.next;
+ }
+
+ return length;
+ }
+
+}
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/linkedlist/CycleRemovalWithoutCountingLoopNodes.java b/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/linkedlist/CycleRemovalWithoutCountingLoopNodes.java
similarity index 96%
rename from algorithms/src/main/java/com/baeldung/algorithms/linkedlist/CycleRemovalWithoutCountingLoopNodes.java
rename to algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/linkedlist/CycleRemovalWithoutCountingLoopNodes.java
index b979f7f677..1e41c832db 100644
--- a/algorithms/src/main/java/com/baeldung/algorithms/linkedlist/CycleRemovalWithoutCountingLoopNodes.java
+++ b/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/linkedlist/CycleRemovalWithoutCountingLoopNodes.java
@@ -1,27 +1,27 @@
-package com.baeldung.algorithms.linkedlist;
-
-public class CycleRemovalWithoutCountingLoopNodes {
-
- public static boolean detectAndRemoveCycle(Node head) {
- CycleDetectionResult result = CycleDetectionByFastAndSlowIterators.detectCycle(head);
-
- if (result.cycleExists) {
- removeCycle(result.node, head);
- }
-
- return result.cycleExists;
- }
-
- private static void removeCycle(Node meetingPointParam, Node head) {
- Node loopNode = meetingPointParam;
- Node it = head;
-
- while (loopNode.next != it.next) {
- it = it.next;
- loopNode = loopNode.next;
- }
-
- loopNode.next = null;
- }
-
-}
+package com.baeldung.algorithms.linkedlist;
+
+public class CycleRemovalWithoutCountingLoopNodes {
+
+ public static boolean detectAndRemoveCycle(Node head) {
+ CycleDetectionResult result = CycleDetectionByFastAndSlowIterators.detectCycle(head);
+
+ if (result.cycleExists) {
+ removeCycle(result.node, head);
+ }
+
+ return result.cycleExists;
+ }
+
+ private static void removeCycle(Node meetingPointParam, Node head) {
+ Node loopNode = meetingPointParam;
+ Node it = head;
+
+ while (loopNode.next != it.next) {
+ it = it.next;
+ loopNode = loopNode.next;
+ }
+
+ loopNode.next = null;
+ }
+
+}
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/linkedlist/Node.java b/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/linkedlist/Node.java
similarity index 95%
rename from algorithms/src/main/java/com/baeldung/algorithms/linkedlist/Node.java
rename to algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/linkedlist/Node.java
index 4add22c77d..9573bcd981 100644
--- a/algorithms/src/main/java/com/baeldung/algorithms/linkedlist/Node.java
+++ b/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/linkedlist/Node.java
@@ -1,38 +1,38 @@
-package com.baeldung.algorithms.linkedlist;
-
-public class Node {
- T data;
- Node next;
-
- public static Node createNewNode(T data, Node next) {
- Node node = new Node();
- node.data = data;
- node.next = next;
- return node;
- }
-
- public static void traverseList(Node root) {
- if (root == null) {
- return;
- }
-
- Node node = root;
- while (node != null) {
- System.out.println(node.data);
- node = node.next;
- }
- }
-
- public static Node getTail(Node root) {
- if (root == null) {
- return null;
- }
-
- Node node = root;
- while (node.next != null) {
- node = node.next;
- }
- return node;
- }
-
+package com.baeldung.algorithms.linkedlist;
+
+public class Node {
+ T data;
+ Node next;
+
+ public static Node createNewNode(T data, Node next) {
+ Node node = new Node();
+ node.data = data;
+ node.next = next;
+ return node;
+ }
+
+ public static void traverseList(Node root) {
+ if (root == null) {
+ return;
+ }
+
+ Node node = root;
+ while (node != null) {
+ System.out.println(node.data);
+ node = node.next;
+ }
+ }
+
+ public static Node getTail(Node root) {
+ if (root == null) {
+ return null;
+ }
+
+ Node node = root;
+ while (node.next != null) {
+ node = node.next;
+ }
+ return node;
+ }
+
}
\ No newline at end of file
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/maze/solver/BFSMazeSolver.java b/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/maze/solver/BFSMazeSolver.java
similarity index 96%
rename from algorithms/src/main/java/com/baeldung/algorithms/maze/solver/BFSMazeSolver.java
rename to algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/maze/solver/BFSMazeSolver.java
index 08972251b8..0e3101925c 100644
--- a/algorithms/src/main/java/com/baeldung/algorithms/maze/solver/BFSMazeSolver.java
+++ b/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/maze/solver/BFSMazeSolver.java
@@ -1,52 +1,52 @@
-package com.baeldung.algorithms.maze.solver;
-
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.LinkedList;
-import java.util.List;
-
-public class BFSMazeSolver {
- private static final int[][] DIRECTIONS = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } };
-
- public List solve(Maze maze) {
- LinkedList nextToVisit = new LinkedList<>();
- Coordinate start = maze.getEntry();
- nextToVisit.add(start);
-
- while (!nextToVisit.isEmpty()) {
- Coordinate cur = nextToVisit.remove();
-
- if (!maze.isValidLocation(cur.getX(), cur.getY()) || maze.isExplored(cur.getX(), cur.getY())) {
- continue;
- }
-
- if (maze.isWall(cur.getX(), cur.getY())) {
- maze.setVisited(cur.getX(), cur.getY(), true);
- continue;
- }
-
- if (maze.isExit(cur.getX(), cur.getY())) {
- return backtrackPath(cur);
- }
-
- for (int[] direction : DIRECTIONS) {
- Coordinate coordinate = new Coordinate(cur.getX() + direction[0], cur.getY() + direction[1], cur);
- nextToVisit.add(coordinate);
- maze.setVisited(cur.getX(), cur.getY(), true);
- }
- }
- return Collections.emptyList();
- }
-
- private List backtrackPath(Coordinate cur) {
- List path = new ArrayList<>();
- Coordinate iter = cur;
-
- while (iter != null) {
- path.add(iter);
- iter = iter.parent;
- }
-
- return path;
- }
-}
+package com.baeldung.algorithms.maze.solver;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.LinkedList;
+import java.util.List;
+
+public class BFSMazeSolver {
+ private static final int[][] DIRECTIONS = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } };
+
+ public List solve(Maze maze) {
+ LinkedList nextToVisit = new LinkedList<>();
+ Coordinate start = maze.getEntry();
+ nextToVisit.add(start);
+
+ while (!nextToVisit.isEmpty()) {
+ Coordinate cur = nextToVisit.remove();
+
+ if (!maze.isValidLocation(cur.getX(), cur.getY()) || maze.isExplored(cur.getX(), cur.getY())) {
+ continue;
+ }
+
+ if (maze.isWall(cur.getX(), cur.getY())) {
+ maze.setVisited(cur.getX(), cur.getY(), true);
+ continue;
+ }
+
+ if (maze.isExit(cur.getX(), cur.getY())) {
+ return backtrackPath(cur);
+ }
+
+ for (int[] direction : DIRECTIONS) {
+ Coordinate coordinate = new Coordinate(cur.getX() + direction[0], cur.getY() + direction[1], cur);
+ nextToVisit.add(coordinate);
+ maze.setVisited(cur.getX(), cur.getY(), true);
+ }
+ }
+ return Collections.emptyList();
+ }
+
+ private List backtrackPath(Coordinate cur) {
+ List path = new ArrayList<>();
+ Coordinate iter = cur;
+
+ while (iter != null) {
+ path.add(iter);
+ iter = iter.parent;
+ }
+
+ return path;
+ }
+}
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/maze/solver/Coordinate.java b/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/maze/solver/Coordinate.java
similarity index 94%
rename from algorithms/src/main/java/com/baeldung/algorithms/maze/solver/Coordinate.java
rename to algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/maze/solver/Coordinate.java
index 09b2ced5e6..8202c89076 100644
--- a/algorithms/src/main/java/com/baeldung/algorithms/maze/solver/Coordinate.java
+++ b/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/maze/solver/Coordinate.java
@@ -1,31 +1,31 @@
-package com.baeldung.algorithms.maze.solver;
-
-public class Coordinate {
- int x;
- int y;
- Coordinate parent;
-
- public Coordinate(int x, int y) {
- this.x = x;
- this.y = y;
- this.parent = null;
- }
-
- public Coordinate(int x, int y, Coordinate parent) {
- this.x = x;
- this.y = y;
- this.parent = parent;
- }
-
- int getX() {
- return x;
- }
-
- int getY() {
- return y;
- }
-
- Coordinate getParent() {
- return parent;
- }
-}
+package com.baeldung.algorithms.maze.solver;
+
+public class Coordinate {
+ int x;
+ int y;
+ Coordinate parent;
+
+ public Coordinate(int x, int y) {
+ this.x = x;
+ this.y = y;
+ this.parent = null;
+ }
+
+ public Coordinate(int x, int y, Coordinate parent) {
+ this.x = x;
+ this.y = y;
+ this.parent = parent;
+ }
+
+ int getX() {
+ return x;
+ }
+
+ int getY() {
+ return y;
+ }
+
+ Coordinate getParent() {
+ return parent;
+ }
+}
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/maze/solver/DFSMazeSolver.java b/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/maze/solver/DFSMazeSolver.java
similarity index 96%
rename from algorithms/src/main/java/com/baeldung/algorithms/maze/solver/DFSMazeSolver.java
rename to algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/maze/solver/DFSMazeSolver.java
index f9640066b9..ee821631db 100644
--- a/algorithms/src/main/java/com/baeldung/algorithms/maze/solver/DFSMazeSolver.java
+++ b/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/maze/solver/DFSMazeSolver.java
@@ -1,48 +1,48 @@
-package com.baeldung.algorithms.maze.solver;
-
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
-
-public class DFSMazeSolver {
- private static final int[][] DIRECTIONS = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } };
-
- public List solve(Maze maze) {
- List path = new ArrayList<>();
- if (explore(maze, maze.getEntry()
- .getX(),
- maze.getEntry()
- .getY(),
- path)) {
- return path;
- }
- return Collections.emptyList();
- }
-
- private boolean explore(Maze maze, int row, int col, List path) {
- if (!maze.isValidLocation(row, col) || maze.isWall(row, col) || maze.isExplored(row, col)) {
- return false;
- }
-
- path.add(new Coordinate(row, col));
- maze.setVisited(row, col, true);
-
- if (maze.isExit(row, col)) {
- return true;
- }
-
- for (int[] direction : DIRECTIONS) {
- Coordinate coordinate = getNextCoordinate(row, col, direction[0], direction[1]);
- if (explore(maze, coordinate.getX(), coordinate.getY(), path)) {
- return true;
- }
- }
-
- path.remove(path.size() - 1);
- return false;
- }
-
- private Coordinate getNextCoordinate(int row, int col, int i, int j) {
- return new Coordinate(row + i, col + j);
- }
-}
+package com.baeldung.algorithms.maze.solver;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+public class DFSMazeSolver {
+ private static final int[][] DIRECTIONS = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } };
+
+ public List solve(Maze maze) {
+ List path = new ArrayList<>();
+ if (explore(maze, maze.getEntry()
+ .getX(),
+ maze.getEntry()
+ .getY(),
+ path)) {
+ return path;
+ }
+ return Collections.emptyList();
+ }
+
+ private boolean explore(Maze maze, int row, int col, List path) {
+ if (!maze.isValidLocation(row, col) || maze.isWall(row, col) || maze.isExplored(row, col)) {
+ return false;
+ }
+
+ path.add(new Coordinate(row, col));
+ maze.setVisited(row, col, true);
+
+ if (maze.isExit(row, col)) {
+ return true;
+ }
+
+ for (int[] direction : DIRECTIONS) {
+ Coordinate coordinate = getNextCoordinate(row, col, direction[0], direction[1]);
+ if (explore(maze, coordinate.getX(), coordinate.getY(), path)) {
+ return true;
+ }
+ }
+
+ path.remove(path.size() - 1);
+ return false;
+ }
+
+ private Coordinate getNextCoordinate(int row, int col, int i, int j) {
+ return new Coordinate(row + i, col + j);
+ }
+}
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/maze/solver/Maze.java b/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/maze/solver/Maze.java
similarity index 96%
rename from algorithms/src/main/java/com/baeldung/algorithms/maze/solver/Maze.java
rename to algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/maze/solver/Maze.java
index 8aaa44d9b1..d0a0ed65d9 100644
--- a/algorithms/src/main/java/com/baeldung/algorithms/maze/solver/Maze.java
+++ b/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/maze/solver/Maze.java
@@ -1,141 +1,141 @@
-package com.baeldung.algorithms.maze.solver;
-
-import java.io.File;
-import java.io.FileNotFoundException;
-import java.util.Arrays;
-import java.util.List;
-import java.util.Scanner;
-
-public class Maze {
- private static final int ROAD = 0;
- private static final int WALL = 1;
- private static final int START = 2;
- private static final int EXIT = 3;
- private static final int PATH = 4;
-
- private int[][] maze;
- private boolean[][] visited;
- private Coordinate start;
- private Coordinate end;
-
- public Maze(File maze) throws FileNotFoundException {
- String fileText = "";
- try (Scanner input = new Scanner(maze)) {
- while (input.hasNextLine()) {
- fileText += input.nextLine() + "\n";
- }
- }
- initializeMaze(fileText);
- }
-
- private void initializeMaze(String text) {
- if (text == null || (text = text.trim()).length() == 0) {
- throw new IllegalArgumentException("empty lines data");
- }
-
- String[] lines = text.split("[\r]?\n");
- maze = new int[lines.length][lines[0].length()];
- visited = new boolean[lines.length][lines[0].length()];
-
- for (int row = 0; row < getHeight(); row++) {
- if (lines[row].length() != getWidth()) {
- throw new IllegalArgumentException("line " + (row + 1) + " wrong length (was " + lines[row].length() + " but should be " + getWidth() + ")");
- }
-
- for (int col = 0; col < getWidth(); col++) {
- if (lines[row].charAt(col) == '#')
- maze[row][col] = WALL;
- else if (lines[row].charAt(col) == 'S') {
- maze[row][col] = START;
- start = new Coordinate(row, col);
- } else if (lines[row].charAt(col) == 'E') {
- maze[row][col] = EXIT;
- end = new Coordinate(row, col);
- } else
- maze[row][col] = ROAD;
- }
- }
- }
-
- public int getHeight() {
- return maze.length;
- }
-
- public int getWidth() {
- return maze[0].length;
- }
-
- public Coordinate getEntry() {
- return start;
- }
-
- public Coordinate getExit() {
- return end;
- }
-
- public boolean isExit(int x, int y) {
- return x == end.getX() && y == end.getY();
- }
-
- public boolean isStart(int x, int y) {
- return x == start.getX() && y == start.getY();
- }
-
- public boolean isExplored(int row, int col) {
- return visited[row][col];
- }
-
- public boolean isWall(int row, int col) {
- return maze[row][col] == WALL;
- }
-
- public void setVisited(int row, int col, boolean value) {
- visited[row][col] = value;
- }
-
- public boolean isValidLocation(int row, int col) {
- if (row < 0 || row >= getHeight() || col < 0 || col >= getWidth()) {
- return false;
- }
- return true;
- }
-
- public void printPath(List path) {
- int[][] tempMaze = Arrays.stream(maze)
- .map(int[]::clone)
- .toArray(int[][]::new);
- for (Coordinate coordinate : path) {
- if (isStart(coordinate.getX(), coordinate.getY()) || isExit(coordinate.getX(), coordinate.getY())) {
- continue;
- }
- tempMaze[coordinate.getX()][coordinate.getY()] = PATH;
- }
- System.out.println(toString(tempMaze));
- }
-
- public String toString(int[][] maze) {
- StringBuilder result = new StringBuilder(getWidth() * (getHeight() + 1));
- for (int row = 0; row < getHeight(); row++) {
- for (int col = 0; col < getWidth(); col++) {
- if (maze[row][col] == ROAD) {
- result.append(' ');
- } else if (maze[row][col] == WALL) {
- result.append('#');
- } else if (maze[row][col] == START) {
- result.append('S');
- } else if (maze[row][col] == EXIT) {
- result.append('E');
- } else {
- result.append('.');
- }
- }
- result.append('\n');
- }
- return result.toString();
- }
-
- public void reset() {
- for (int i = 0; i < visited.length; i++)
- Arrays.fill(visited[i], false);
- }
-}
+package com.baeldung.algorithms.maze.solver;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Scanner;
+
+public class Maze {
+ private static final int ROAD = 0;
+ private static final int WALL = 1;
+ private static final int START = 2;
+ private static final int EXIT = 3;
+ private static final int PATH = 4;
+
+ private int[][] maze;
+ private boolean[][] visited;
+ private Coordinate start;
+ private Coordinate end;
+
+ public Maze(File maze) throws FileNotFoundException {
+ String fileText = "";
+ try (Scanner input = new Scanner(maze)) {
+ while (input.hasNextLine()) {
+ fileText += input.nextLine() + "\n";
+ }
+ }
+ initializeMaze(fileText);
+ }
+
+ private void initializeMaze(String text) {
+ if (text == null || (text = text.trim()).length() == 0) {
+ throw new IllegalArgumentException("empty lines data");
+ }
+
+ String[] lines = text.split("[\r]?\n");
+ maze = new int[lines.length][lines[0].length()];
+ visited = new boolean[lines.length][lines[0].length()];
+
+ for (int row = 0; row < getHeight(); row++) {
+ if (lines[row].length() != getWidth()) {
+ throw new IllegalArgumentException("line " + (row + 1) + " wrong length (was " + lines[row].length() + " but should be " + getWidth() + ")");
+ }
+
+ for (int col = 0; col < getWidth(); col++) {
+ if (lines[row].charAt(col) == '#')
+ maze[row][col] = WALL;
+ else if (lines[row].charAt(col) == 'S') {
+ maze[row][col] = START;
+ start = new Coordinate(row, col);
+ } else if (lines[row].charAt(col) == 'E') {
+ maze[row][col] = EXIT;
+ end = new Coordinate(row, col);
+ } else
+ maze[row][col] = ROAD;
+ }
+ }
+ }
+
+ public int getHeight() {
+ return maze.length;
+ }
+
+ public int getWidth() {
+ return maze[0].length;
+ }
+
+ public Coordinate getEntry() {
+ return start;
+ }
+
+ public Coordinate getExit() {
+ return end;
+ }
+
+ public boolean isExit(int x, int y) {
+ return x == end.getX() && y == end.getY();
+ }
+
+ public boolean isStart(int x, int y) {
+ return x == start.getX() && y == start.getY();
+ }
+
+ public boolean isExplored(int row, int col) {
+ return visited[row][col];
+ }
+
+ public boolean isWall(int row, int col) {
+ return maze[row][col] == WALL;
+ }
+
+ public void setVisited(int row, int col, boolean value) {
+ visited[row][col] = value;
+ }
+
+ public boolean isValidLocation(int row, int col) {
+ if (row < 0 || row >= getHeight() || col < 0 || col >= getWidth()) {
+ return false;
+ }
+ return true;
+ }
+
+ public void printPath(List path) {
+ int[][] tempMaze = Arrays.stream(maze)
+ .map(int[]::clone)
+ .toArray(int[][]::new);
+ for (Coordinate coordinate : path) {
+ if (isStart(coordinate.getX(), coordinate.getY()) || isExit(coordinate.getX(), coordinate.getY())) {
+ continue;
+ }
+ tempMaze[coordinate.getX()][coordinate.getY()] = PATH;
+ }
+ System.out.println(toString(tempMaze));
+ }
+
+ public String toString(int[][] maze) {
+ StringBuilder result = new StringBuilder(getWidth() * (getHeight() + 1));
+ for (int row = 0; row < getHeight(); row++) {
+ for (int col = 0; col < getWidth(); col++) {
+ if (maze[row][col] == ROAD) {
+ result.append(' ');
+ } else if (maze[row][col] == WALL) {
+ result.append('#');
+ } else if (maze[row][col] == START) {
+ result.append('S');
+ } else if (maze[row][col] == EXIT) {
+ result.append('E');
+ } else {
+ result.append('.');
+ }
+ }
+ result.append('\n');
+ }
+ return result.toString();
+ }
+
+ public void reset() {
+ for (int i = 0; i < visited.length; i++)
+ Arrays.fill(visited[i], false);
+ }
+}
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/maze/solver/MazeDriver.java b/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/maze/solver/MazeDriver.java
similarity index 96%
rename from algorithms/src/main/java/com/baeldung/algorithms/maze/solver/MazeDriver.java
rename to algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/maze/solver/MazeDriver.java
index 60263deba3..a47c3c8581 100644
--- a/algorithms/src/main/java/com/baeldung/algorithms/maze/solver/MazeDriver.java
+++ b/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/maze/solver/MazeDriver.java
@@ -1,34 +1,34 @@
-package com.baeldung.algorithms.maze.solver;
-
-import java.io.File;
-import java.util.List;
-
-public class MazeDriver {
- public static void main(String[] args) throws Exception {
- File maze1 = new File("src/main/resources/maze/maze1.txt");
- File maze2 = new File("src/main/resources/maze/maze2.txt");
-
- execute(maze1);
- execute(maze2);
- }
-
- private static void execute(File file) throws Exception {
- Maze maze = new Maze(file);
- dfs(maze);
- bfs(maze);
- }
-
- private static void bfs(Maze maze) {
- BFSMazeSolver bfs = new BFSMazeSolver();
- List path = bfs.solve(maze);
- maze.printPath(path);
- maze.reset();
- }
-
- private static void dfs(Maze maze) {
- DFSMazeSolver dfs = new DFSMazeSolver();
- List path = dfs.solve(maze);
- maze.printPath(path);
- maze.reset();
- }
-}
+package com.baeldung.algorithms.maze.solver;
+
+import java.io.File;
+import java.util.List;
+
+public class MazeDriver {
+ public static void main(String[] args) throws Exception {
+ File maze1 = new File("src/main/resources/maze/maze1.txt");
+ File maze2 = new File("src/main/resources/maze/maze2.txt");
+
+ execute(maze1);
+ execute(maze2);
+ }
+
+ private static void execute(File file) throws Exception {
+ Maze maze = new Maze(file);
+ dfs(maze);
+ bfs(maze);
+ }
+
+ private static void bfs(Maze maze) {
+ BFSMazeSolver bfs = new BFSMazeSolver();
+ List path = bfs.solve(maze);
+ maze.printPath(path);
+ maze.reset();
+ }
+
+ private static void dfs(Maze maze) {
+ DFSMazeSolver dfs = new DFSMazeSolver();
+ List path = dfs.solve(maze);
+ maze.printPath(path);
+ maze.reset();
+ }
+}
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/numberwordconverter/NumberWordConverter.java b/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/numberwordconverter/NumberWordConverter.java
similarity index 100%
rename from algorithms/src/main/java/com/baeldung/algorithms/numberwordconverter/NumberWordConverter.java
rename to algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/numberwordconverter/NumberWordConverter.java
diff --git a/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/relativelyprime/RelativelyPrime.java b/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/relativelyprime/RelativelyPrime.java
new file mode 100644
index 0000000000..fbea87be30
--- /dev/null
+++ b/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/relativelyprime/RelativelyPrime.java
@@ -0,0 +1,45 @@
+package com.baeldung.algorithms.relativelyprime;
+
+import java.math.BigInteger;
+
+class RelativelyPrime {
+
+ static boolean iterativeRelativelyPrime(int a, int b) {
+ return iterativeGCD(a, b) == 1;
+ }
+
+ static boolean recursiveRelativelyPrime(int a, int b) {
+ return recursiveGCD(a, b) == 1;
+ }
+
+ static boolean bigIntegerRelativelyPrime(int a, int b) {
+ return BigInteger.valueOf(a).gcd(BigInteger.valueOf(b)).equals(BigInteger.ONE);
+ }
+
+ private static int iterativeGCD(int a, int b) {
+ int tmp;
+ while (b != 0) {
+ if (a < b) {
+ tmp = a;
+ a = b;
+ b = tmp;
+ }
+ tmp = b;
+ b = a % b;
+ a = tmp;
+ }
+ return a;
+ }
+
+ private static int recursiveGCD(int a, int b) {
+ if (b == 0) {
+ return a;
+ }
+ if (a < b) {
+ return recursiveGCD(b, a);
+ }
+ return recursiveGCD(b, a % b);
+ }
+
+
+}
diff --git a/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/reversingtree/TreeNode.java b/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/reversingtree/TreeNode.java
new file mode 100644
index 0000000000..a6ec2277e5
--- /dev/null
+++ b/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/reversingtree/TreeNode.java
@@ -0,0 +1,42 @@
+package com.baeldung.algorithms.reversingtree;
+
+public class TreeNode {
+
+ private int value;
+ private TreeNode rightChild;
+ private TreeNode leftChild;
+
+ public int getValue() {
+ return value;
+ }
+
+ public void setValue(int value) {
+ this.value = value;
+ }
+
+ public TreeNode getRightChild() {
+ return rightChild;
+ }
+
+ public void setRightChild(TreeNode rightChild) {
+ this.rightChild = rightChild;
+ }
+
+ public TreeNode getLeftChild() {
+ return leftChild;
+ }
+
+ public void setLeftChild(TreeNode leftChild) {
+ this.leftChild = leftChild;
+ }
+
+ public TreeNode(int value, TreeNode leftChild, TreeNode rightChild) {
+ this.value = value;
+ this.rightChild = rightChild;
+ this.leftChild = leftChild;
+ }
+
+ public TreeNode(int value) {
+ this.value = value;
+ }
+}
diff --git a/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/reversingtree/TreeReverser.java b/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/reversingtree/TreeReverser.java
new file mode 100644
index 0000000000..162119d390
--- /dev/null
+++ b/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/reversingtree/TreeReverser.java
@@ -0,0 +1,53 @@
+package com.baeldung.algorithms.reversingtree;
+
+import java.util.LinkedList;
+
+public class TreeReverser {
+
+ public void reverseRecursive(TreeNode treeNode) {
+ if (treeNode == null) {
+ return;
+ }
+
+ TreeNode temp = treeNode.getLeftChild();
+ treeNode.setLeftChild(treeNode.getRightChild());
+ treeNode.setRightChild(temp);
+
+ reverseRecursive(treeNode.getLeftChild());
+ reverseRecursive(treeNode.getRightChild());
+ }
+
+ public void reverseIterative(TreeNode treeNode) {
+ LinkedList queue = new LinkedList();
+
+ if (treeNode != null) {
+ queue.add(treeNode);
+ }
+
+ while (!queue.isEmpty()) {
+
+ TreeNode node = queue.poll();
+ if (node.getLeftChild() != null)
+ queue.add(node.getLeftChild());
+ if (node.getRightChild() != null)
+ queue.add(node.getRightChild());
+
+ TreeNode temp = node.getLeftChild();
+ node.setLeftChild(node.getRightChild());
+ node.setRightChild(temp);
+ }
+ }
+
+ public String toString(TreeNode root) {
+ if (root == null) {
+ return "";
+ }
+
+ StringBuffer buffer = new StringBuffer(String.valueOf(root.getValue())).append(" ");
+
+ buffer.append(toString(root.getLeftChild()));
+ buffer.append(toString(root.getRightChild()));
+
+ return buffer.toString();
+ }
+}
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/slope_one/InputData.java b/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/slope_one/InputData.java
similarity index 100%
rename from algorithms/src/main/java/com/baeldung/algorithms/slope_one/InputData.java
rename to algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/slope_one/InputData.java
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/slope_one/Item.java b/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/slope_one/Item.java
similarity index 100%
rename from algorithms/src/main/java/com/baeldung/algorithms/slope_one/Item.java
rename to algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/slope_one/Item.java
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/slope_one/SlopeOne.java b/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/slope_one/SlopeOne.java
similarity index 100%
rename from algorithms/src/main/java/com/baeldung/algorithms/slope_one/SlopeOne.java
rename to algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/slope_one/SlopeOne.java
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/slope_one/User.java b/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/slope_one/User.java
similarity index 100%
rename from algorithms/src/main/java/com/baeldung/algorithms/slope_one/User.java
rename to algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/slope_one/User.java
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/sudoku/BacktrackingAlgorithm.java b/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/sudoku/BacktrackingAlgorithm.java
similarity index 100%
rename from algorithms/src/main/java/com/baeldung/algorithms/sudoku/BacktrackingAlgorithm.java
rename to algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/sudoku/BacktrackingAlgorithm.java
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/sudoku/ColumnNode.java b/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/sudoku/ColumnNode.java
similarity index 100%
rename from algorithms/src/main/java/com/baeldung/algorithms/sudoku/ColumnNode.java
rename to algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/sudoku/ColumnNode.java
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingLinks.java b/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/sudoku/DancingLinks.java
similarity index 100%
rename from algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingLinks.java
rename to algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/sudoku/DancingLinks.java
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingLinksAlgorithm.java b/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/sudoku/DancingLinksAlgorithm.java
similarity index 100%
rename from algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingLinksAlgorithm.java
rename to algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/sudoku/DancingLinksAlgorithm.java
diff --git a/algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingNode.java b/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/sudoku/DancingNode.java
similarity index 100%
rename from algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingNode.java
rename to algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/sudoku/DancingNode.java
diff --git a/apache-cayenne/src/main/resources/logback.xml b/algorithms-miscellaneous-2/src/main/resources/logback.xml
similarity index 100%
rename from apache-cayenne/src/main/resources/logback.xml
rename to algorithms-miscellaneous-2/src/main/resources/logback.xml
diff --git a/algorithms/src/main/resources/maze/maze1.txt b/algorithms-miscellaneous-2/src/main/resources/maze/maze1.txt
similarity index 85%
rename from algorithms/src/main/resources/maze/maze1.txt
rename to algorithms-miscellaneous-2/src/main/resources/maze/maze1.txt
index 0a6309d25b..8b48c325d2 100644
--- a/algorithms/src/main/resources/maze/maze1.txt
+++ b/algorithms-miscellaneous-2/src/main/resources/maze/maze1.txt
@@ -1,12 +1,12 @@
-S ########
-# #
-# ### ## #
-# # # #
-# # # # #
-# ## #####
-# # #
-# # # # #
-##### ####
-# # E
-# # # #
+S ########
+# #
+# ### ## #
+# # # #
+# # # # #
+# ## #####
+# # #
+# # # # #
+##### ####
+# # E
+# # # #
##########
\ No newline at end of file
diff --git a/algorithms/src/main/resources/maze/maze2.txt b/algorithms-miscellaneous-2/src/main/resources/maze/maze2.txt
similarity index 92%
rename from algorithms/src/main/resources/maze/maze2.txt
rename to algorithms-miscellaneous-2/src/main/resources/maze/maze2.txt
index 22e6d0382a..df5b6bc66b 100644
--- a/algorithms/src/main/resources/maze/maze2.txt
+++ b/algorithms-miscellaneous-2/src/main/resources/maze/maze2.txt
@@ -1,22 +1,22 @@
-S ##########################
-# # # #
-# # #### ############### #
-# # # # # #
-# # #### # # ###############
-# # # # # # #
-# # # #### ### ########### #
-# # # # # #
-# ################## #
-######### # # # # #
-# # #### # ####### # #
-# # ### ### # # # # #
-# # ## # ##### # #
-##### ####### # # # # #
-# # ## ## #### # #
-# ##### ####### # #
-# # ############
-####### ######### # #
-# # ######## #
-# ####### ###### ## # E
-# # # ## #
+S ##########################
+# # # #
+# # #### ############### #
+# # # # # #
+# # #### # # ###############
+# # # # # # #
+# # # #### ### ########### #
+# # # # # #
+# ################## #
+######### # # # # #
+# # #### # ####### # #
+# # ### ### # # # # #
+# # ## # ##### # #
+##### ####### # # # # #
+# # ## ## #### # #
+# ##### ####### # #
+# # ############
+####### ######### # #
+# # ######## #
+# ####### ###### ## # E
+# # # ## #
############################
\ No newline at end of file
diff --git a/algorithms/src/test/java/com/baeldung/algorithms/DijkstraAlgorithmLongRunningUnitTest.java b/algorithms-miscellaneous-2/src/test/java/com/baeldung/algorithms/DijkstraAlgorithmLongRunningUnitTest.java
similarity index 100%
rename from algorithms/src/test/java/com/baeldung/algorithms/DijkstraAlgorithmLongRunningUnitTest.java
rename to algorithms-miscellaneous-2/src/test/java/com/baeldung/algorithms/DijkstraAlgorithmLongRunningUnitTest.java
diff --git a/algorithms-miscellaneous-2/src/test/java/com/baeldung/algorithms/conversion/ByteArrayConverterUnitTest.java b/algorithms-miscellaneous-2/src/test/java/com/baeldung/algorithms/conversion/ByteArrayConverterUnitTest.java
new file mode 100644
index 0000000000..be61802705
--- /dev/null
+++ b/algorithms-miscellaneous-2/src/test/java/com/baeldung/algorithms/conversion/ByteArrayConverterUnitTest.java
@@ -0,0 +1,127 @@
+package com.baeldung.algorithms.conversion;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThat;
+
+import org.apache.commons.codec.DecoderException;
+import org.hamcrest.text.IsEqualIgnoringCase;
+import org.junit.Before;
+import org.junit.Test;
+
+import com.baeldung.algorithms.conversion.HexStringConverter;
+
+public class ByteArrayConverterUnitTest {
+
+ private HexStringConverter hexStringConverter;
+
+ @Before
+ public void setup() {
+ hexStringConverter = new HexStringConverter();
+ }
+
+ @Test
+ public void shouldEncodeByteArrayToHexStringUsingBigIntegerToString() {
+ byte[] bytes = getSampleBytes();
+ String hexString = getSampleHexString();
+ if(hexString.charAt(0) == '0') {
+ hexString = hexString.substring(1);
+ }
+ String output = hexStringConverter.encodeUsingBigIntegerToString(bytes);
+ assertThat(output, IsEqualIgnoringCase.equalToIgnoringCase(hexString));
+ }
+
+ @Test
+ public void shouldEncodeByteArrayToHexStringUsingBigIntegerStringFormat() {
+ byte[] bytes = getSampleBytes();
+ String hexString = getSampleHexString();
+ String output = hexStringConverter.encodeUsingBigIntegerStringFormat(bytes);
+ assertThat(output, IsEqualIgnoringCase.equalToIgnoringCase(hexString));
+ }
+
+ @Test
+ public void shouldDecodeHexStringToByteArrayUsingBigInteger() {
+ byte[] bytes = getSampleBytes();
+ String hexString = getSampleHexString();
+ byte[] output = hexStringConverter.decodeUsingBigInteger(hexString);
+ assertArrayEquals(bytes, output);
+ }
+
+ @Test
+ public void shouldEncodeByteArrayToHexStringUsingCharacterConversion() {
+ byte[] bytes = getSampleBytes();
+ String hexString = getSampleHexString();
+ String output = hexStringConverter.encodeHexString(bytes);
+ assertThat(output, IsEqualIgnoringCase.equalToIgnoringCase(hexString));
+ }
+
+ @Test
+ public void shouldDecodeHexStringToByteArrayUsingCharacterConversion() {
+ byte[] bytes = getSampleBytes();
+ String hexString = getSampleHexString();
+ byte[] output = hexStringConverter.decodeHexString(hexString);
+ assertArrayEquals(bytes, output);
+ }
+
+ @Test(expected=IllegalArgumentException.class)
+ public void shouldDecodeHexToByteWithInvalidHexCharacter() {
+ hexStringConverter.hexToByte("fg");
+ }
+
+ @Test
+ public void shouldEncodeByteArrayToHexStringDataTypeConverter() {
+ byte[] bytes = getSampleBytes();
+ String hexString = getSampleHexString();
+ String output = hexStringConverter.encodeUsingDataTypeConverter(bytes);
+ assertThat(output, IsEqualIgnoringCase.equalToIgnoringCase(hexString));
+ }
+
+ @Test
+ public void shouldDecodeHexStringToByteArrayUsingDataTypeConverter() {
+ byte[] bytes = getSampleBytes();
+ String hexString = getSampleHexString();
+ byte[] output = hexStringConverter.decodeUsingDataTypeConverter(hexString);
+ assertArrayEquals(bytes, output);
+ }
+
+ @Test
+ public void shouldEncodeByteArrayToHexStringUsingGuava() {
+ byte[] bytes = getSampleBytes();
+ String hexString = getSampleHexString();
+ String output = hexStringConverter.encodeUsingGuava(bytes);
+ assertThat(output, IsEqualIgnoringCase.equalToIgnoringCase(hexString));
+ }
+
+ @Test
+ public void shouldDecodeHexStringToByteArrayUsingGuava() {
+ byte[] bytes = getSampleBytes();
+ String hexString = getSampleHexString();
+ byte[] output = hexStringConverter.decodeUsingGuava(hexString);
+ assertArrayEquals(bytes, output);
+ }
+
+ @Test
+ public void shouldEncodeByteArrayToHexStringUsingApacheCommons() throws DecoderException {
+ byte[] bytes = getSampleBytes();
+ String hexString = getSampleHexString();
+ String output = hexStringConverter.encodeUsingApacheCommons(bytes);
+ assertThat(output, IsEqualIgnoringCase.equalToIgnoringCase(hexString));
+ }
+
+ @Test
+ public void shouldDecodeHexStringToByteArrayUsingApacheCommons() throws DecoderException {
+ byte[] bytes = getSampleBytes();
+ String hexString = getSampleHexString();
+ byte[] output = hexStringConverter.decodeUsingApacheCommons(hexString);
+ assertArrayEquals(bytes, output);
+ }
+
+ private String getSampleHexString() {
+ return "0af50c0e2d10";
+ }
+
+ private byte[] getSampleBytes() {
+ return new byte[] { 10, -11, 12, 14, 45, 16 };
+ }
+
+}
diff --git a/algorithms/src/test/java/com/baeldung/algorithms/editdistance/EditDistanceDataProvider.java b/algorithms-miscellaneous-2/src/test/java/com/baeldung/algorithms/editdistance/EditDistanceDataProvider.java
similarity index 96%
rename from algorithms/src/test/java/com/baeldung/algorithms/editdistance/EditDistanceDataProvider.java
rename to algorithms-miscellaneous-2/src/test/java/com/baeldung/algorithms/editdistance/EditDistanceDataProvider.java
index 89bd871616..d11da61191 100644
--- a/algorithms/src/test/java/com/baeldung/algorithms/editdistance/EditDistanceDataProvider.java
+++ b/algorithms-miscellaneous-2/src/test/java/com/baeldung/algorithms/editdistance/EditDistanceDataProvider.java
@@ -1,21 +1,21 @@
-package com.baeldung.algorithms.editdistance;
-
-import org.junit.runners.Parameterized.Parameters;
-
-import java.util.Arrays;
-import java.util.Collection;
-
-public class EditDistanceDataProvider {
-
- @Parameters
- public static Collection