List contains elements using comparator
See original GitHub issueHaving following code:
public class TolkienCharacter {
String name;
double height;
}
public class Tale {
private String title;
private List<TolkienCharacter> characters;
}
TolkienCharacter frodo = new TolkienCharacter("Frodo", 1.2);
TolkienCharacter sam = new TolkienCharacter("Sam", 1.3);
List<TolkienCharacter> short_list = Arrays.asList(frodo, sam);
TolkienCharacter frodo_new = new TolkienCharacter("Frodo", 1.3);
TolkienCharacter sam_new = new TolkienCharacter("Sam", 1.2);
TolkienCharacter pippin_new = new TolkienCharacter("Pippin", 1.1);
List<TolkienCharacter> full_list = Arrays.asList(sam_new, frodo_new, pippin_new);
Comparator<Double> closeEnough = (d1, d2) -> Math.abs(d1 - d2) <= 0.5 ? 0 : 1;
Tale tale_short_version = new Tale("Tale one", short_list);
Tale tale_long_version = new Tale("Tale one", full_list);
tale_long_version
is equal to tale_short_version
if:
- title is the same
- tale_long_version.characters contains all tale_short_version.characters by TolkienCharacter.name
TolkienCharacter objects are equal if name is the same and height is close enough (using comparator closeEnough).
I can compare TolkienCharacter objects by code:
assertThat(frodo).usingRecursiveComparison()
.withComparatorForType(closeEnough, Double.class)
.isEqualTo(sam);
But I have no idea, how to combine both - comparator and list contains elements.
Issue Analytics
- State:
- Created a year ago
- Comments:7 (3 by maintainers)
Top Results From Across the Web
List::contains with comparator - java - Stack Overflow
Is there any way (method, lambda, or elegant construct) to find an element in a list based on ...
Read more >List contains elements using comparator · Issue #2550 - GitHub
TolkienCharacter objects are equal if name is the same and height is close enough (using comparator closeEnough). I am looking for assertion, ...
Read more >Java List Contain containsDuplicates(List<T> list, Comparator ...
Java List Contain containsDuplicates(List list, Comparator comparator). Here you can find the source of containsDuplicates(List list, Comparator comparator).
Read more >How to Find an Element in a List with Java - Baeldung
This method returns the index of the first occurrence of the specified element in the given list, or -1 if the list doesn't...
Read more >Comparator Interface in Java with Examples - GeeksforGeeks
Method of Collections class for sorting List elements is used to sort the elements of List by the given comparator. public void sort(List...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@joel-costigliola , by the way here is nice solution:
Thank you!