Skip to content

Commit c9cd941

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents a08733d + 4552b80 commit c9cd941

48 files changed

Lines changed: 1820 additions & 498 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎algorithms/src/main/java/com/baeldung/algorithms/hillclimbing/HillClimbing.java‎

Lines changed: 62 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import java.util.Objects;
66
import java.util.Optional;
77
import java.util.Stack;
8-
import java.util.stream.Collectors;
98

109
public class HillClimbing {
1110
public static void main(String[] args) {
@@ -22,7 +21,7 @@ public static void main(String[] args) {
2221
}
2322
}
2423

25-
public static void printEachStep(State state) {
24+
private static void printEachStep(State state) {
2625
List<Stack<String>> stackList = state.getState();
2726
System.out.println("----------------");
2827
stackList.forEach(stack -> {
@@ -33,23 +32,18 @@ public static void printEachStep(State state) {
3332
});
3433
}
3534

36-
public Stack<String> getStackWithValues(String[] blocks) {
37-
Stack<String> stack = new Stack<String>();
35+
private Stack<String> getStackWithValues(String[] blocks) {
36+
Stack<String> stack = new Stack<>();
3837
for (String block : blocks)
3938
stack.push(block);
4039
return stack;
4140
}
4241

4342
/**
4443
* This method prepares path from init state to goal state
45-
*
46-
* @param initStateStack
47-
* @param goalStateStack
48-
* @return
49-
* @throws Exception
5044
*/
5145
public List<State> getRouteWithHillClimbing(Stack<String> initStateStack, Stack<String> goalStateStack) throws Exception {
52-
List<Stack<String>> initStateStackList = new ArrayList<Stack<String>>();
46+
List<Stack<String>> initStateStackList = new ArrayList<>();
5347
initStateStackList.add(initStateStack);
5448
int initStateHeuristics = getHeuristicsValue(initStateStackList, goalStateStack);
5549
State initState = new State(initStateStackList, initStateHeuristics);
@@ -71,57 +65,51 @@ public List<State> getRouteWithHillClimbing(Stack<String> initStateStack, Stack<
7165
}
7266
}
7367

74-
if (noStateFound)
75-
throw new Exception("No path found");
7668
return resultPath;
7769
}
7870

7971
/**
8072
* This method finds new state from current state based on goal and
8173
* heuristics
82-
*
83-
* @param currentState
84-
* @param goalStateStack
85-
* @return a next state
8674
*/
8775
public State findNextState(State currentState, Stack<String> goalStateStack) {
8876
List<Stack<String>> listOfStacks = currentState.getState();
8977
int currentStateHeuristics = currentState.getHeuristics();
9078

91-
Optional<State> newState = listOfStacks.stream()
79+
return listOfStacks.stream()
9280
.map(stack -> {
93-
State tempState = null;
94-
List<Stack<String>> tempStackList = new ArrayList<Stack<String>>(listOfStacks);
95-
String block = stack.pop();
96-
if (stack.size() == 0)
97-
tempStackList.remove(stack);
98-
tempState = pushElementToNewStack(tempStackList, block, currentStateHeuristics, goalStateStack);
99-
if (tempState == null) {
100-
tempState = pushElementToExistingStacks(stack, tempStackList, block, currentStateHeuristics, goalStateStack);
101-
}
102-
if (tempState == null)
103-
stack.push(block);
104-
return tempState;
81+
return applyOperationsOnState(listOfStacks, stack, currentStateHeuristics, goalStateStack);
10582
})
10683
.filter(Objects::nonNull)
107-
.findFirst();
108-
109-
return newState.orElse(null);
84+
.findFirst()
85+
.orElse(null);
11086
}
11187

88+
/**
89+
* This method applies operations on the current state to get a new state
90+
*/
91+
public State applyOperationsOnState(List<Stack<String>> listOfStacks, Stack<String> stack, int currentStateHeuristics, Stack<String> goalStateStack) {
92+
State tempState;
93+
List<Stack<String>> tempStackList = new ArrayList<>(listOfStacks);
94+
String block = stack.pop();
95+
if (stack.size() == 0)
96+
tempStackList.remove(stack);
97+
tempState = pushElementToNewStack(tempStackList, block, currentStateHeuristics, goalStateStack);
98+
if (tempState == null) {
99+
tempState = pushElementToExistingStacks(stack, tempStackList, block, currentStateHeuristics, goalStateStack);
100+
}
101+
if (tempState == null)
102+
stack.push(block);
103+
return tempState;
104+
}
105+
112106
/**
113107
* Operation to be applied on a state in order to find new states. This
114108
* operation pushes an element into a new stack
115-
*
116-
* @param currentStackList
117-
* @param block
118-
* @param currentStateHeuristics
119-
* @param goalStateStack
120-
* @return a new state
121109
*/
122110
private State pushElementToNewStack(List<Stack<String>> currentStackList, String block, int currentStateHeuristics, Stack<String> goalStateStack) {
123111
State newState = null;
124-
Stack<String> newStack = new Stack<String>();
112+
Stack<String> newStack = new Stack<>();
125113
newStack.push(block);
126114

127115
currentStackList.add(newStack);
@@ -138,62 +126,64 @@ private State pushElementToNewStack(List<Stack<String>> currentStackList, String
138126
* Operation to be applied on a state in order to find new states. This
139127
* operation pushes an element into one of the other stacks to explore new
140128
* states
141-
*
142-
* @param stack
143-
* @param currentStackList
144-
* @param block
145-
* @param currentStateHeuristics
146-
* @param goalStateStack
147-
* @return a new state
148129
*/
149130
private State pushElementToExistingStacks(Stack currentStack, List<Stack<String>> currentStackList, String block, int currentStateHeuristics, Stack<String> goalStateStack) {
150131

151132
Optional<State> newState = currentStackList.stream()
152133
.filter(stack -> stack != currentStack)
153134
.map(stack -> {
154-
stack.push(block);
155-
int newStateHeuristics = getHeuristicsValue(currentStackList, goalStateStack);
156-
if (newStateHeuristics > currentStateHeuristics) {
157-
return new State(currentStackList, newStateHeuristics);
158-
}
159-
stack.pop();
160-
return null;
135+
return pushElementToStack(stack, block, currentStackList, currentStateHeuristics, goalStateStack);
161136
})
162137
.filter(Objects::nonNull)
163138
.findFirst();
164139

165140
return newState.orElse(null);
166141
}
142+
143+
/**
144+
* This method pushes a block to the stack and returns new state if its closer to goal
145+
*/
146+
private State pushElementToStack(Stack stack, String block, List<Stack<String>> currentStackList, int currentStateHeuristics, Stack<String> goalStateStack) {
147+
stack.push(block);
148+
int newStateHeuristics = getHeuristicsValue(currentStackList, goalStateStack);
149+
if (newStateHeuristics > currentStateHeuristics) {
150+
return new State(currentStackList, newStateHeuristics);
151+
}
152+
stack.pop();
153+
return null;
154+
}
167155

168156
/**
169157
* This method returns heuristics value for given state with respect to goal
170158
* state
171-
*
172-
* @param currentState
173-
* @param goalStateStack
174-
* @return
175159
*/
176160
public int getHeuristicsValue(List<Stack<String>> currentState, Stack<String> goalStateStack) {
177-
178-
Integer heuristicValue = 0;
161+
Integer heuristicValue;
179162
heuristicValue = currentState.stream()
180163
.mapToInt(stack -> {
181-
int stackHeuristics = 0;
182-
boolean isPositioneCorrect = true;
183-
int goalStartIndex = 0;
184-
for (String currentBlock : stack) {
185-
if (isPositioneCorrect && currentBlock.equals(goalStateStack.get(goalStartIndex))) {
186-
stackHeuristics += goalStartIndex;
187-
} else {
188-
stackHeuristics -= goalStartIndex;
189-
isPositioneCorrect = false;
190-
}
191-
goalStartIndex++;
192-
}
193-
return stackHeuristics;
164+
return getHeuristicsValueForStack(stack, currentState, goalStateStack);
194165
})
195166
.sum();
196167
return heuristicValue;
197168
}
169+
170+
/**
171+
* This method returns heuristics value for a particular stack
172+
*/
173+
public int getHeuristicsValueForStack(Stack<String> stack, List<Stack<String>> currentState, Stack<String> goalStateStack) {
174+
int stackHeuristics = 0;
175+
boolean isPositioneCorrect = true;
176+
int goalStartIndex = 0;
177+
for (String currentBlock : stack) {
178+
if (isPositioneCorrect && currentBlock.equals(goalStateStack.get(goalStartIndex))) {
179+
stackHeuristics += goalStartIndex;
180+
} else {
181+
stackHeuristics -= goalStartIndex;
182+
isPositioneCorrect = false;
183+
}
184+
goalStartIndex++;
185+
}
186+
return stackHeuristics;
187+
}
198188

199189
}

‎algorithms/src/main/java/com/baeldung/algorithms/hillclimbing/State.java‎

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,23 @@
55
import java.util.Stack;
66

77
public class State {
8-
List<Stack<String>> state;
9-
int heuristics;
10-
11-
public State() {
12-
}
8+
private List<Stack<String>> state;
9+
private int heuristics;
1310

1411
public State(List<Stack<String>> state) {
1512
this.state = state;
1613
}
1714

18-
public State(List<Stack<String>> state, int heuristics) {
15+
State(List<Stack<String>> state, int heuristics) {
1916
this.state = state;
2017
this.heuristics = heuristics;
2118
}
2219

23-
public State(State state) {
20+
State(State state) {
2421
if (state != null) {
25-
this.state = new ArrayList<Stack<String>>();
22+
this.state = new ArrayList<>();
2623
for (Stack s : state.getState()) {
27-
Stack s1 = new Stack();
24+
Stack s1;
2825
s1 = (Stack) s.clone();
2926
this.state.add(s1);
3027
}
@@ -36,10 +33,6 @@ public List<Stack<String>> getState() {
3633
return state;
3734
}
3835

39-
public void setState(List<Stack<String>> state) {
40-
this.state = state;
41-
}
42-
4336
public int getHeuristics() {
4437
return heuristics;
4538
}

‎algorithms/src/test/java/algorithms/HillClimbingAlgorithmTest.java‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@
1414
import static org.junit.Assert.assertTrue;
1515

1616
public class HillClimbingAlgorithmTest {
17-
Stack<String> initStack;
18-
Stack<String> goalStack;
17+
private Stack<String> initStack;
18+
private Stack<String> goalStack;
1919

2020
@Before
2121
public void initStacks() {
2222
String blockArr[] = { "B", "C", "D", "A" };
2323
String goalBlockArr[] = { "A", "B", "C", "D" };
24-
initStack = new Stack<String>();
24+
initStack = new Stack<>();
2525
for (String block : blockArr)
2626
initStack.push(block);
27-
goalStack = new Stack<String>();
27+
goalStack = new Stack<>();
2828
for (String block : goalBlockArr)
2929
goalStack.push(block);
3030
}
@@ -48,7 +48,7 @@ public void givenInitAndGoalState_whenGetPathWithHillClimbing_thenPathFound() {
4848
@Test
4949
public void givenCurrentState_whenFindNextState_thenBetterHeuristics() {
5050
HillClimbing hillClimbing = new HillClimbing();
51-
List<Stack<String>> initList = new ArrayList<Stack<String>>();
51+
List<Stack<String>> initList = new ArrayList<>();
5252
initList.add(initStack);
5353
State currentState = new State(initList);
5454
currentState.setHeuristics(hillClimbing.getHeuristicsValue(initList, goalStack));
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.baeldung.java9.time;
2+
3+
import java.time.LocalDate;
4+
import java.time.temporal.ChronoUnit;
5+
import java.util.ArrayList;
6+
import java.util.Calendar;
7+
import java.util.Date;
8+
import java.util.GregorianCalendar;
9+
import java.util.List;
10+
import java.util.stream.Collectors;
11+
import java.util.stream.IntStream;
12+
13+
public class TimeApi {
14+
15+
public static List<Date> getDatesBetweenUsingJava7(Date startDate, Date endDate) {
16+
List<Date> datesInRange = new ArrayList<Date>();
17+
Calendar calendar = new GregorianCalendar();
18+
calendar.setTime(startDate);
19+
20+
Calendar endCalendar = new GregorianCalendar();
21+
endCalendar.setTime(endDate);
22+
23+
while (calendar.before(endCalendar)) {
24+
Date result = calendar.getTime();
25+
datesInRange.add(result);
26+
calendar.add(Calendar.DATE, 1);
27+
}
28+
return datesInRange;
29+
}
30+
31+
public static List<LocalDate> getDatesBetweenUsingJava8(LocalDate startDate, LocalDate endDate) {
32+
long numOfDaysBetween = ChronoUnit.DAYS.between(startDate, endDate);
33+
return IntStream.iterate(0, i -> i + 1)
34+
.limit(numOfDaysBetween)
35+
.mapToObj(i -> startDate.plusDays(i))
36+
.collect(Collectors.toList());
37+
}
38+
39+
public static List<LocalDate> getDatesBetweenUsingJava9(LocalDate startDate, LocalDate endDate) {
40+
return startDate.datesUntil(endDate).collect(Collectors.toList());
41+
}
42+
43+
}

0 commit comments

Comments
 (0)