You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -200,8 +212,21 @@ public static boolean hasDuplicates() {
* @param <T> type of elements
* @return optional max value
*/
// todo: create a method and implement its logic manually without using util method from JDK
public static <T> Optional<T> findMax(Iterable<T> elements, Comparator<? super T> comparator) {
Iterator<T> iterator = elements.iterator();
T max = iterator.next();
while (iterator.hasNext()) {
T current = iterator.next();
if (comparator.compare(current, max) > 0) {
max = current;
}
}
return Optional.of(max);
}
/**
* findMostRecentlyCreatedEntity is a generic util method that accepts a collection of entities and returns the
* one that is the most recently created. If collection is empty,
Expand All
@@ -214,7 +239,11 @@ public static boolean hasDuplicates() {
* @param <T> entity type
* @return an entity from the given collection that has the max createdOn value
*/
// todo: create a method according to JavaDoc and implement it using previous method
public static <T extends BaseEntity> T findMostRecentlyCreatedEntity(Collection<T> entities) {
return findMax(entities, CREATED_ON_COMPARATOR)
.orElseThrow();
}
/**
* An util method that allows to swap two elements of any list. It changes the list so the element with the index
Expand All
@@ -228,7 +257,13 @@ public static boolean hasDuplicates() {
public static void swap(List<?> elements, int i, int j) {
Objects.checkIndex(i, elements.size());
Objects.checkIndex(j, elements.size());
throw new ExerciseNotCompletedException(); // todo: complete method implementation
swapHelper(elements, i, j);
}
private static <T> void swapHelper(List<T> elements, int i, int j) {
T temp = elements.get(i);
elements.set(i, elements.get(j));
elements.set(j, temp);
}
}
Expand Down
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.