Queue and Map interface

Discussion in 'Java' started by Philipp Kraus, Jun 13, 2014.

  1. Hello,

    I would like to create a class which implements a Map and a Queue with

    class myMapQueue<A,B> extends myAbstractClass implements Map<A,B>, Queue<B> {}

    The problem is the remove(Object) method, because the method is defined
    in the Queue and the Map
    interface in the Queue with boolean remove(Object) and in the Map with
    V remove(Object), so I get
    a "clash error". How can I solve this problem.

    Thanks a lot

    Phil
     
    Philipp Kraus, Jun 13, 2014
    #1
    1. Advertisements

  2. Philipp Kraus

    Eric Sosman Guest

    On 6/13/2014 4:52 PM, Philipp Kraus wrote:
    > Hello,
    >
    > I would like to create a class which implements a Map and a Queue with
    >
    > class myMapQueue<A,B> extends myAbstractClass implements Map<A,B>,
    > Queue<B> {}
    >
    > The problem is the remove(Object) method, because the method is defined
    > in the Queue and the Map
    > interface in the Queue with boolean remove(Object) and in the Map with V
    > remove(Object), so I get
    > a "clash error". How can I solve this problem.


    "Favor composition over inheritance." -- Bloch

    In short, I don't believe you can do what you want -- not in the
    way you want to do it. An alternative might be to implement Map<A,B>
    in an outer class, one that also contains a Queue<B> implementation as
    an inner class. The outer class would also have a getQueue() method
    to return an instance of the inner class -- whether that's a new
    instance created on the fly or a single instance that's created once
    per outer class is up to you.

    For even more "favoritism," you could have the outer class
    implement neither Map nor Queue, and delegate both those interfaces
    to inner classes.

    --
    Eric Sosman
    d
     
    Eric Sosman, Jun 13, 2014
    #2
    1. Advertisements

  3. Philipp Kraus

    markspace Guest

    On 6/13/2014 2:32 PM, Eric Sosman wrote:
    > For even more "favoritism," you could have the outer class
    > implement neither Map nor Queue, and delegate both those interfaces
    > to inner classes.
    >


    Or just have two methods to call on some generic object which return a
    "view" of that object as either a Map or a Queue.

    interface MyCollection {

    Map asMap();
    Queue asQueue();

    }
     
    markspace, Jun 14, 2014
    #3
  4. On 2014-06-13 21:32:20 +0000, Eric Sosman said:

    > On 6/13/2014 4:52 PM, Philipp Kraus wrote:
    >> Hello,
    >>
    >> I would like to create a class which implements a Map and a Queue with
    >>
    >> class myMapQueue<A,B> extends myAbstractClass implements Map<A,B>,
    >> Queue<B> {}
    >>
    >> The problem is the remove(Object) method, because the method is defined
    >> in the Queue and the Map
    >> interface in the Queue with boolean remove(Object) and in the Map with V
    >> remove(Object), so I get
    >> a "clash error". How can I solve this problem.

    >
    > "Favor composition over inheritance." -- Bloch
    >
    > In short, I don't believe you can do what you want -- not in the
    > way you want to do it. An alternative might be to implement Map<A,B>
    > in an outer class, one that also contains a Queue<B> implementation as
    > an inner class. The outer class would also have a getQueue() method
    > to return an instance of the inner class -- whether that's a new
    > instance created on the fly or a single instance that's created once
    > per outer class is up to you.
    >
    > For even more "favoritism," you could have the outer class
    > implement neither Map nor Queue, and delegate both those interfaces
    > to inner classes.


    Thanks you both for the idea, but it is not nice to do this with
    different inner structures, because
    I need on the class the interface of Map and Queue, because an object
    of myMapQueue should
    use with the Java Map or Queue structure. On the idea:

    class myMapQueue<K,V> {

    private myMap implements Map<K,V> {}
    private Queue implements Queue<V> {}

    public Map<K,V> getMap() {}
    public Queue<V> getQueue() {}

    }

    I need always the getMap or getQueue call before I can use the internal
    datastructure.

    Thanks a lot for the idea, but I need first to redfine my classes for a
    better code structure

    Phil
     
    Philipp Kraus, Jun 14, 2014
    #4
  5. Philipp Kraus

    Eric Sosman Guest

    On 6/14/2014 1:52 AM, Philipp Kraus wrote:
    > On 2014-06-13 21:32:20 +0000, Eric Sosman said:
    >
    >> On 6/13/2014 4:52 PM, Philipp Kraus wrote:
    >>> Hello,
    >>>
    >>> I would like to create a class which implements a Map and a Queue with
    >>>
    >>> class myMapQueue<A,B> extends myAbstractClass implements Map<A,B>,
    >>> Queue<B> {}
    >>>
    >>> The problem is the remove(Object) method, because the method is defined
    >>> in the Queue and the Map
    >>> interface in the Queue with boolean remove(Object) and in the Map with V
    >>> remove(Object), so I get
    >>> a "clash error". How can I solve this problem.

    >>
    >> "Favor composition over inheritance." -- Bloch
    >>
    >> In short, I don't believe you can do what you want -- not in the
    >> way you want to do it. An alternative might be to implement Map<A,B>
    >> in an outer class, one that also contains a Queue<B> implementation as
    >> an inner class. The outer class would also have a getQueue() method
    >> to return an instance of the inner class -- whether that's a new
    >> instance created on the fly or a single instance that's created once
    >> per outer class is up to you.
    >>
    >> For even more "favoritism," you could have the outer class
    >> implement neither Map nor Queue, and delegate both those interfaces
    >> to inner classes.

    >
    > Thanks you both for the idea, but it is not nice to do this with
    > different inner structures, because
    > I need on the class the interface of Map and Queue, because an object of
    > myMapQueue should
    > use with the Java Map or Queue structure. On the idea:
    >
    > class myMapQueue<K,V> {
    >
    > private myMap implements Map<K,V> {}
    > private Queue implements Queue<V> {}
    >
    > public Map<K,V> getMap() {}
    > public Queue<V> getQueue() {}
    >
    > }
    >
    > I need always the getMap or getQueue call before I can use the internal
    > datastructure.


    You've already discovered that Map and Queue are incompatible,
    hence no class can implement both of them. That is, there can be
    no single object that is simultaneously a Map and a Queue. That
    being so, you will need an intermediary of some kind to produce
    whichever view -- Queue or Map -- is needed for each use.

    > Thanks a lot for the idea, but I need first to redfine my classes for a
    > better code structure


    Perhaps if you described the larger problem you're trying to
    solve, somebody might have ideas about alternative approaches.

    (Idle anecdote: At a job interview I was once asked how to
    implement something rather similar, a blend of Map and PriorityQueue.
    I didn't come up with a real solution -- but I got the job anyhow.)

    --
    Eric Sosman
    d
     
    Eric Sosman, Jun 14, 2014
    #5
    1. Advertisements

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Similar Threads
  1. Paul L. Du Bois

    Queue.Queue-like class without the busy-wait

    Paul L. Du Bois, Mar 24, 2005, in forum: Python
    Replies:
    29
    Views:
    1,578
    Antoon Pardon
    Apr 4, 2005
  2. Russell Warren

    Is Queue.Queue.queue.clear() thread-safe?

    Russell Warren, Jun 22, 2006, in forum: Python
    Replies:
    4
    Views:
    1,032
    Russell Warren
    Jun 27, 2006
  3. Kceiw
    Replies:
    3
    Views:
    1,486
    Jim Langston
    Mar 14, 2006
  4. Gabriel Rossetti
    Replies:
    3
    Views:
    885
    Jerry Hill
    Apr 25, 2008
  5. redbaron
    Replies:
    3
    Views:
    851
    Paul Rubin
    Oct 21, 2008
  6. Frank Millman
    Replies:
    3
    Views:
    576
    Frank Millman
    Feb 9, 2010
  7. Kris
    Replies:
    0
    Views:
    809
  8. bintom
    Replies:
    6
    Views:
    6,824
    Öö Tiib
    Nov 3, 2012
Loading...