question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

List contains elements using comparator

See original GitHub issue

Having 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:closed
  • Created a year ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
arturmkrcommented, May 17, 2022

@joel-costigliola , by the way here is nice solution:

    assertThat(tale_long_version)
        .usingRecursiveComparison()
        .ignoringCollectionOrder()
        .ignoringExpectedNullFields()
        .ignoringFields("characters")
        .isEqualTo(tale_short_version);

    String[] expectedCharacters =
        tale_short_version.characters().stream().map(TolkienCharacter::name).toArray(String[]::new);

    assertThat(
            filter(tale_long_version.characters())
                .with("name")
                .in((Object[]) expectedCharacters)
                .get())
        .usingRecursiveComparison()
        .ignoringCollectionOrder()
        .withComparatorForType(closeEnough, Double.class)
        .isEqualTo(tale_short_version.characters());
1reaction
arturmkrcommented, Apr 5, 2022

Thank you!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found