简体   繁体   中英

Using LinkedList in java, when needing both List and DeQueue methods

If I want to instantiate a LinkedList and require access to the methods in both List and Dequeue interfaces, and do not want to type to the concrete implementation, and do not want to cast between interfaces, is there a way?

ie:

LinkedList ll = new LinkedList(); // don't want to do this...


List ll = new LinkedList();
ll.peekFirst(); // can't access peekFirst method
((DeQueue) ll).peekFirst(); // Kinda ugly
public interface Foo<T> extends List<T>, Deque<T>{}

public class Bar<T> extends LinkedList<T> implements Foo<T>{}

Foo ll = new Bar();

Don't have eclipse near me, this should compile though.

I think in Java you have no choice but to use the concrete type, or store two references to the same object, one typed as List, and the other as Dequeue.

List<T>       list;
Dequeue<T>    queue;

/** Construct a new instance of SomeClass */
private SomeClass() {
    LinkedList<T> tmp=new LinkedList<T>();
    list=tmp;
    queue=tmp;
    }

Not saying I especially like that, though.

I don't see how you can do that if you are declaring ll as a java.util.List. Should be able to create your own class but then you'll be tied to that and not to the stock classes/interfaces...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM