java 8 merge all elements of ListB into ListA if not present : <p>I need to merge all elements of a <strong>listB</strong> into another list <strong>listA</strong>. </p>

<p>If an element is already present (based on a custom equality-check) in <strong>listA</strong> I don't want to add it.</p>

<p>I don't want to use Set, and I don't want to override equals() and hashCode(). </p>

<p>Reasons are, I don't want to prevent duplicates in listA per se, I only want to not merge from listB if there are already elements in listA which I consider being equal.</p>

<p>I don't want to override equals() and hashCode() since that would mean I need to make sure, my implementation of equals() for the elements hold in every circumstance. It might however be, that elements from listB are not fully initialized, i.e. they might miss an object id, where that might be present in elements of listA.</p>

<p>My current approach involves an interface and a Utility-Function:</p>

<pre class="lang-java prettyprint-override"><code>public interface HasEqualityFunction&lt;T&gt; {

    public boolean hasEqualData(T other);
}

public class AppleVariety implements HasEqualityFunction&lt;AppleVariety&gt; {
    private String manufacturerName;
    private String varietyName;

    @Override
    public boolean hasEqualData(AppleVariety other) {
        return (this.manufacturerName.equals(other.getManufacturerName())
            &amp;&amp; this.varietyName.equals(other.getVarietyName()));
    }

    // ... getter-Methods here
}


public class CollectionUtils {
    public static &lt;T extends HasEqualityFunction&gt; void merge(
        List&lt;T&gt; listA,
        List&lt;T&gt; listB) {
        if (listB.isEmpty()) {
            return;
        }
        Predicate&lt;T&gt; exists
            = (T x) -&gt; {
                return listA.stream().noneMatch(
                        x::hasEqualData);
            };
        listA.addAll(listB.stream()
            .filter(exists)
            .collect(Collectors.toList())
        );
    }
}
</code></pre>

<p>And then I'd use it like this:</p>

<pre class="lang-java prettyprint-override"><code>...
List&lt;AppleVariety&gt; appleVarietiesFromOnePlace = ... init here with some elements
List&lt;AppleVariety&gt; appleVarietiesFromAnotherPlace = ... init here with some elements
CollectionUtils.merge(appleVarietiesFromOnePlace, appleVarietiesFromAnotherPlace);
...
</code></pre>

<p>to get my new list in listA with all elements merged from B.</p>

<p>Is this a good approach? Is there a better/easier way to accomplish the same?</p>
