diff --git a/algorithms/src/main/java/com/baeldung/algorithms/hillclimbing/HillClimbing.java b/algorithms/src/main/java/com/baeldung/algorithms/hillclimbing/HillClimbing.java index 3523ff07c0..69eaa0580b 100644 --- a/algorithms/src/main/java/com/baeldung/algorithms/hillclimbing/HillClimbing.java +++ b/algorithms/src/main/java/com/baeldung/algorithms/hillclimbing/HillClimbing.java @@ -22,7 +22,7 @@ public class HillClimbing { } } - public static void printEachStep(State state) { + private static void printEachStep(State state) { List> stackList = state.getState(); System.out.println("----------------"); stackList.forEach(stack -> { @@ -33,8 +33,8 @@ public class HillClimbing { }); } - public Stack getStackWithValues(String[] blocks) { - Stack stack = new Stack(); + private Stack getStackWithValues(String[] blocks) { + Stack stack = new Stack<>(); for (String block : blocks) stack.push(block); return stack; @@ -42,14 +42,9 @@ public class HillClimbing { /** * This method prepares path from init state to goal state - * - * @param initStateStack - * @param goalStateStack - * @return - * @throws Exception */ public List getRouteWithHillClimbing(Stack initStateStack, Stack goalStateStack) throws Exception { - List> initStateStackList = new ArrayList>(); + List> initStateStackList = new ArrayList<>(); initStateStackList.add(initStateStack); int initStateHeuristics = getHeuristicsValue(initStateStackList, goalStateStack); State initState = new State(initStateStackList, initStateHeuristics); @@ -71,27 +66,21 @@ public class HillClimbing { } } - if (noStateFound) - throw new Exception("No path found"); return resultPath; } /** * This method finds new state from current state based on goal and * heuristics - * - * @param currentState - * @param goalStateStack - * @return a next state */ public State findNextState(State currentState, Stack goalStateStack) { List> listOfStacks = currentState.getState(); int currentStateHeuristics = currentState.getHeuristics(); - Optional newState = listOfStacks.stream() + return listOfStacks.stream() .map(stack -> { - State tempState = null; - List> tempStackList = new ArrayList>(listOfStacks); + State tempState; + List> tempStackList = new ArrayList<>(listOfStacks); String block = stack.pop(); if (stack.size() == 0) tempStackList.remove(stack); @@ -104,24 +93,17 @@ public class HillClimbing { return tempState; }) .filter(Objects::nonNull) - .findFirst(); - - return newState.orElse(null); + .findFirst() + .orElse(null); } /** * Operation to be applied on a state in order to find new states. This * operation pushes an element into a new stack - * - * @param currentStackList - * @param block - * @param currentStateHeuristics - * @param goalStateStack - * @return a new state */ private State pushElementToNewStack(List> currentStackList, String block, int currentStateHeuristics, Stack goalStateStack) { State newState = null; - Stack newStack = new Stack(); + Stack newStack = new Stack<>(); newStack.push(block); currentStackList.add(newStack); @@ -138,13 +120,6 @@ public class HillClimbing { * Operation to be applied on a state in order to find new states. This * operation pushes an element into one of the other stacks to explore new * states - * - * @param stack - * @param currentStackList - * @param block - * @param currentStateHeuristics - * @param goalStateStack - * @return a new state */ private State pushElementToExistingStacks(Stack currentStack, List> currentStackList, String block, int currentStateHeuristics, Stack goalStateStack) { @@ -168,14 +143,10 @@ public class HillClimbing { /** * This method returns heuristics value for given state with respect to goal * state - * - * @param currentState - * @param goalStateStack - * @return */ public int getHeuristicsValue(List> currentState, Stack goalStateStack) { - Integer heuristicValue = 0; + Integer heuristicValue; heuristicValue = currentState.stream() .mapToInt(stack -> { int stackHeuristics = 0; diff --git a/algorithms/src/main/java/com/baeldung/algorithms/hillclimbing/State.java b/algorithms/src/main/java/com/baeldung/algorithms/hillclimbing/State.java index dc49c782fa..ad22aa27e7 100644 --- a/algorithms/src/main/java/com/baeldung/algorithms/hillclimbing/State.java +++ b/algorithms/src/main/java/com/baeldung/algorithms/hillclimbing/State.java @@ -5,26 +5,23 @@ import java.util.List; import java.util.Stack; public class State { - List> state; - int heuristics; - - public State() { - } + private List> state; + private int heuristics; public State(List> state) { this.state = state; } - public State(List> state, int heuristics) { + State(List> state, int heuristics) { this.state = state; this.heuristics = heuristics; } - public State(State state) { + State(State state) { if (state != null) { - this.state = new ArrayList>(); + this.state = new ArrayList<>(); for (Stack s : state.getState()) { - Stack s1 = new Stack(); + Stack s1; s1 = (Stack) s.clone(); this.state.add(s1); } @@ -36,10 +33,6 @@ public class State { return state; } - public void setState(List> state) { - this.state = state; - } - public int getHeuristics() { return heuristics; } diff --git a/algorithms/src/test/java/algorithms/HillClimbingAlgorithmTest.java b/algorithms/src/test/java/algorithms/HillClimbingAlgorithmTest.java index f040d3a4ca..6e5055da6e 100644 --- a/algorithms/src/test/java/algorithms/HillClimbingAlgorithmTest.java +++ b/algorithms/src/test/java/algorithms/HillClimbingAlgorithmTest.java @@ -14,17 +14,17 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; public class HillClimbingAlgorithmTest { - Stack initStack; - Stack goalStack; + private Stack initStack; + private Stack goalStack; @Before public void initStacks() { String blockArr[] = { "B", "C", "D", "A" }; String goalBlockArr[] = { "A", "B", "C", "D" }; - initStack = new Stack(); + initStack = new Stack<>(); for (String block : blockArr) initStack.push(block); - goalStack = new Stack(); + goalStack = new Stack<>(); for (String block : goalBlockArr) goalStack.push(block); } @@ -48,7 +48,7 @@ public class HillClimbingAlgorithmTest { @Test public void givenCurrentState_whenFindNextState_thenBetterHeuristics() { HillClimbing hillClimbing = new HillClimbing(); - List> initList = new ArrayList>(); + List> initList = new ArrayList<>(); initList.add(initStack); State currentState = new State(initList); currentState.setHeuristics(hillClimbing.getHeuristicsValue(initList, goalStack));