I'm porting Java7 code to Java8, and I came up with a following problem. In my codebase I have two methods:
public static <T> ImmutableSet<T> append(Set<T> set, T elem) {
return ImmutableSet.<T>builder().addAll(set).add(elem).build();
}
public static <T> ImmutableSet<T> append(Set<T> set, Set<T> elemSet) {
ImmutableSet.Builder<T> newSet = ImmutableSet.builder();
return newSet.addAll(set).addAll(elemSet).build();
Compiler returns error about ambiguous match for method append in following test:
@Test(expected = NullPointerException.class)
public void shouldAppendThrowNullPointerForNullSecondSet() {
ImmutableSet<Integer> obj = null;
CollectionUtils.append(ImmutableSet.of(1), obj);
}
Compiler error:
reference to append is ambiguous both method append(java.util.Set,T) in CollectionUtils and method append(java.util.Set,java.util.Set) in CollectionUtils match
How to rewrite these functions to work with type inference from introduced with Java8?