11

I tried to write generic function that remove the duplicate elements from array.

public static <E extends Comparable<E>> ArrayList<E> removeDuplicate(E[] arr) {
    //do quicksort
    Arrays.sort(arr);
    ArrayList<E> list = new ArrayList<E>();
    int i;
    for(i=0; i<arr.length-1; i++) {
        if(arr[i].compareTo(arr[i+1]) != 0) { //if not duplicate, add to the list
            list.add(arr[i]);
        }
    }
    list.add(arr[i]); //add last element
    return list;
}

As you can see you can't pass primitive type like int[] array since I am comparing elements by compareTo() method that defined in Comparable interface.

I noticed the first line (method declaration):

public static <E extends Comparable<E>> ArrayList<E> removeDuplicate(E[] arr) {

How come it says "extends Comparable" ?

Comparable is an interface so why is it not "implement Comparable"? This is first time I wrote generic function so I'm bit confused about such detail. (any wondering would prevent me from understanding..)

EDIT: Found this article related to this topic.

http://www.tutorialspoint.com/java/java_generics.htm

Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
Meow
  • 18,371
  • 52
  • 136
  • 180

3 Answers3

10

This is just the convention chosen for generics. When using bounded type parameters you use extends (even though it might mean implements in some cases) or super.

You can even do something like <E extends Comparable<E> & Cloneable> to define that the object that would replace the type parameter should implement both those interfaces.

Andrei Fierbinteanu
  • 7,656
  • 3
  • 31
  • 45
5

If You want to use the thing that implements You just write is as generic parameter

class Bar extends  Foo<String> { /* Code */}

The wildcard that You are talking about are three

  1. "? extends Type": Denotes a family of subtypes of type Type. This is the most useful wildcard
  2. "? super Type": Denotes a family of supertypes of type Type
  3. "?": Denotes the set of all types or any

You method should look like

public static <T extends Comparable<? super T>> Collection<T> sort(T[] list) {

        Collection<T> list = new ArrayList<T>();

         //do quicksort
        Arrays.sort(arr);

        Collection<T> list = new ArrayList<T>();
        int i;
        for(i=0; i<arr.length-1; i++) {
            if(arr[i].compareTo(arr[i+1]) != 0) { //if not duplicate, add to the list
                list.add(arr[i]);
            }
        }
        list.add(arr[i]); //add last element
//btw how do You know that last is not duplicate 
        return list;

}

For detail please visit this page

ByteWelder
  • 5,464
  • 1
  • 38
  • 45
  • Thanks for the tip. Answer to your question, "how do I know the last is not duplicate": If the sorted array consists A,B,B 1. I put A to arraylist. 2 .Skip second B since index[1] and index[1+1] is same. 3 .Get out of loop. 4. Add the last index (which is last B) – Meow Jul 24 '10 at 20:49
1

For one thing, E might be an interface.

Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305
  • 7
    Of course it's a sentence. It has a subject, verb, and object. It is also a correct answer. What's your problem? – user207421 Jul 25 '10 at 01:11