-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhancement: If there are a large number of elements, using replaceAl…
…l may cause performance issues.
- Loading branch information
Showing
2 changed files
with
66 additions
and
26 deletions.
There are no files selected for viewing
This file contains 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.dlsc.gemsfx.util; | ||
|
||
import java.util.List; | ||
import java.util.ListIterator; | ||
import java.util.function.Predicate; | ||
|
||
public class ListUtils { | ||
|
||
/** | ||
* Update matching elements in the list with the provided new value. | ||
* | ||
* @param list the list to operate on | ||
* @param matchPredicate the predicate to match elements | ||
* @param newValue the new value to replace matching elements | ||
* @param breakAfterFirstMatch whether to break after the first match | ||
* @param <T> the type of elements in the list | ||
* @return the number of elements updated | ||
*/ | ||
public static <T> int updateMatching(List<T> list, Predicate<T> matchPredicate, T newValue, boolean breakAfterFirstMatch) { | ||
int updateCount = 0; | ||
ListIterator<T> iterator = list.listIterator(); | ||
while (iterator.hasNext()) { | ||
T currentElement = iterator.next(); | ||
if (matchPredicate.test(currentElement)) { | ||
iterator.set(newValue); | ||
updateCount++; | ||
if (breakAfterFirstMatch) { | ||
break; | ||
} | ||
} | ||
} | ||
return updateCount; | ||
} | ||
|
||
/** | ||
* Update the first matching element in the list with the provided new value. | ||
* | ||
* @param list the list to operate on | ||
* @param matchPredicate the predicate to match elements | ||
* @param newValue the new value to replace matching elements | ||
* @param <T> the type of elements in the list | ||
* @return the number of elements updated | ||
*/ | ||
public static <T> int updateFirstMatching(List<T> list, Predicate<T> matchPredicate, T newValue) { | ||
return updateMatching(list, matchPredicate, newValue, true); | ||
} | ||
|
||
/** | ||
* Update all matching elements in the list with the provided new value. | ||
* | ||
* @param list the list to operate on | ||
* @param matchPredicate the predicate to match elements | ||
* @param newValue the new value to replace matching elements | ||
* @param <T> the type of elements in the list | ||
* @return the number of elements updated | ||
*/ | ||
public static <T> int updateAllMatching(List<T> list, Predicate<T> matchPredicate, T newValue) { | ||
return updateMatching(list, matchPredicate, newValue, false); | ||
} | ||
} | ||
|