Ensuring that the "Object o" parameter has the same generic type when implementing java interfaces

I am trying to create a hybrid list that uses binary search trees for various functions. The class is trying to implement List< E >. To index these trees, use a comparison to decide whether to place data in the left or right child of the parent. This is done using the variable Comparatorspecified by the constructor:

private Comparator<E> comparator; // method for ordering the tree

This ensures that the type Eis somehow comparable. However, the problem is how the List interface declares its abstract functions. For instance:

public boolean contains(Object o);

I usually assume that the parameter is comparable and then uses the recursive search method in the tree, but since the parameter is Object, I cannot make this assumption.

I tried to make sure it has a type Eby doing the following:

if (o instanceof E) {}

but it does not work and gives me this suggestion:

Cannot perform instanceof check against type parameter E. 
Use its erasure Object instead since further generic type information will be erased at runtime

Any ideas on how to solve this?

Edit:

This creates an instance of the class:

TreeCollection<Integer> c = new TreeCollection<>(
            new Comparator<Integer>() {

                @Override
                public int compare(Integer o1, Integer o2) {
                    return o1.compareTo(o2);
                }

            });
+4
source share
3 answers

. - Comparator, .contains(), .equals(), ( ), . TreeSet.

- O (log n), ; , . , , . , E , . E, , , - ClassCastException, . TreeSet - TreeSet.contains() , ClassCastException, .

.contains() , Collection.contains(), , - .contains(), , , ; , -, , , .

+4

Java, "Heterogeneous Container".

, , , Heterogenic Container - , .

, .

public class MyListTreeSet<E> extends AbstractList<E>
{

   private Class<E> type;
   private TreeSet<E> set;

   public MyListTreeSet( Comparator<? super E> comp, Class<E> type )
   {
      this.type = type;
      set = new TreeSet<>( comp );
   }

, contains(), , .

   @Override
   public boolean contains( Object o )
   {
      if( !type.isInstance( o ) ) return false;
      E e = type.cast( o );
      // e is now the same type as your set and therefore 
      // implements Comparable
      return set.contains( e );  // quickie example
   }

, .

EDIT:

, , (). , , , . TreeCollection - , , TreeCollection.

TreeCollection<Integer> c = new NewTreeCollection<>(
        new Comparator<Integer>() {

            @Override
            public int compare(Integer o1, Integer o2) {
                return o1.compareTo(o2);
            }

        }, Integer.class );

NewTreeCollection , I, , TreeCollection TreeSet, .

public class NewTreeCollection<E> extends TreeCollection<E>
{

   private Class<E> type;
   private TreeSet<E> set;

   public NewTreeCollection( Comparator<? super E> comp, Class<E> type )
   {
      this.type = type;
      set = new TreeSet<>( comp );
   }

   @Override
   public boolean contains( Object o )
   {
      if( !type.isInstance( o ) ) return false;
      E e = type.cast( o );
      // e is now the same type as your set and therefore 
      // implements Comparable
      return set.contains( e );  // quickie example
   }
}
+2

, /.

.

List<Integer> list = new ArrayList<>(); 

list , ArrayList, .

class MyList extends ArrayList<Integer> {}
List<Integer> list1 = new MyList();

MyList - ArrayList. , , , .

Comparator<Integer> cmp = new Comparator<Integer>() {...};

cmp - , Comparator. , , .

Comparator<Integer> cmp = new Comparator<Integer>() {
    @Override
    public int compare(Integer x, Integer y) {
        return x.compareTo(y);
    }
};
Type genericType = cmp.getClass().getGenericInterfaces()[0];
// use getGenericSuperclass() if subclassing a class rather than implementing an 
// interface
if (genericType instanceof Class) {
    throw new RuntimeException("Missing type parameter.");
}
Type type = ((ParameterizedType) genericType).getActualTypeArguments()[0];  

Class<?> cls = (Class<?>) type;
boolean is_true = cls.isInstance(1);
boolean is_false = cls.isInstance(1.);
0

All Articles