0

This is the code I have right now

public class removerDuplicados<T> {

    public removerDuplicados() {
    }

    public T[] removerDups(T [] arr) {
        T[] res = Arrays.stream(arr).distinct().toArray();
        return res;
    }
}

But this line

T[] res = Arrays.stream(arr).distinct().toArray();

Gives me incompatible types error

2
  • try using T[] res = (T[]) (Arrays.stream(arr).distinct().toArray()) ; Commented Aug 4, 2021 at 22:32
  • @MattDog_222Overwatch Doesn't work. Arrays aren't generic. Commented Aug 4, 2021 at 22:34

3 Answers 3

2

Interestingly enough, you can't do this particular trick with collections, where types are erased, but you can with arrays:

// this method can be static
@SuppressWarnings("unsafe") // safe because we're getting the type from the array object
public static <T> T[] removeDups(T[] arr) {
  // https://stackoverflow.com/questions/212805/in-java-how-do-i-dynamically-determine-the-type-of-an-array
  Class<T> componentType = (Class<T>) arr.getClass().getComponentType();

  return Arrays.stream(arr)
    .distinct()
    .toArray(size -> (T[]) Array.newInstance(componentType, size));
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try this. The copy is redundant, but it can be compiled without reflections, casting or warnings.

public static <T> T[] removerDups(T [] arr) {
    return Arrays.stream(arr)
        .distinct()
        .toArray(n -> Arrays.copyOf(arr, n));
}

public static void main(String[] args) {
    String[] array = {"a", "b", "a"};
    String[] uniq = removerDups(array);
    System.out.println(Arrays.toString(uniq));
}

output:

[a, b]

Comments

-1

This will work. Just call removerDuplicados.removerDups( ARRAY_NAME )

import java.util.Arrays;

public class removerDuplicados<T>
{
    @SuppressWarnings("unchecked")
    public static <T> T[] removerDups(T [] arr)
    {
        return ( (T[]) (Arrays.stream(arr).distinct().toArray()) ) ;
    }
}

5 Comments

This won't work because Object[] is not a T[].
@chrylis-cautiouslyoptimistic- Don't downvote my comment before trying it. It literally compiles. Copy paste it and run this in a main class public static void main(String[] args) { Integer[] ints = { 1, 2, 3, 3, 3, 4, 5, 6, 6, 7, 8, 1 } ; System.out.println(Arrays.toString(removerDuplicados.removerDups(ints) )); }
Now try ints = removeDups(ints);. You'll get a ClassCastException. Your example works because T is Object.
@chrylis-cautiouslyoptimistic- Yeah but even your answer wont work because it doesn't have a return type and you would have to individually cast each thing to type T I think
Missing return type fixed. You don't need to cast each stream member; that's handled as part of the type-safe version of toArray.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.