#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2005
    Location
    Chicago, Illinois, U.S.A.
    Posts
    161
    Rep Power
    0

    How do you tell a Collection from a List?


    I can tell an array from a stack.

    I can tell an array stack from a linked list stack.
    I can tell an array queue from a linked list queue.

    But how do you tell a Collection array stack or array queue from a List array stack or array queue, and how do you tell a
    Collection linked-list stack or linked list queue from a List linked-list stack or linked-list queue?

    Thanks.
  2. #2
  3. Lazy Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,436
    Rep Power
    9645
    A List is also a Collection, so they aren't that much different. But LinkedLists, Queues, and Stacks are all different structures.

    Are you asking about the concepts or about how to tell them apart programmatically?
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2005
    Location
    Chicago, Illinois, U.S.A.
    Posts
    161
    Rep Power
    0
    Hmm I would say both.
  6. #4
  7. Lazy Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,436
    Rep Power
    9645
    What they are is fairly well explained already.

    Programmatically you can use instanceof, but for the most part you shouldn't have to care about what precise type something is - that's the whole point of polymorphism.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2005
    Location
    Chicago, Illinois, U.S.A.
    Posts
    161
    Rep Power
    0
    Originally Posted by LostShadow
    But how do you tell a Collection array stack or array queue from a List array stack or array queue, and how do you tell a
    Collection linked-list stack or linked list queue from a List linked-list stack or linked-list queue?

    Thanks.
    I meant.

    How do you tell a.

    -Linked-list stack that is Collection ADT, from linked-list stack that is List ADT.
    -Array stack that is Collection ADT, from an array stack that is List ADT.

    -Linked-list queue that is Collection ADT, from a linked-list queue that is List ADT.
    -Array queue that is Collection ADT, from an array queue that is List ADT.

    Can something be both a List and Collection at the same time?
  10. #6
  11. Lazy Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,436
    Rep Power
    9645
    Originally Posted by LostShadow
    How do you tell a.

    -Linked-list stack that is Collection ADT, from linked-list stack that is List ADT.
    -Array stack that is Collection ADT, from an array stack that is List ADT.

    -Linked-list queue that is Collection ADT, from a linked-list queue that is List ADT.
    -Array queue that is Collection ADT, from an array queue that is List ADT.
    Your terminology is ambiguous so either (a) I answered that or (b) your questions make no sense, likely because you don't know what you're talking about. I'm rather hoping it's the former.

    Originally Posted by LostShadow
    Can something be both a List and Collection at the same time?
    Look at List in the documentation. Do you see where it says
    Code:
    public interface List<E>
    extends Collection<E>
    Do you have any idea what that means?
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2005
    Location
    Chicago, Illinois, U.S.A.
    Posts
    161
    Rep Power
    0
    Coding-wise, I would have something like this.

    Array stack

    m_array[m_index] = v;
    m_index++;

    Linked list stack

    LNode newNode = new LNode(v);
    newNode.setLink(m_top);
    m_top = newNode;

    Array queue

    m_rear = (m_rear + 1) % m_array.length;
    m_array[m_rear] = v;
    m_numElements++;

    Linked queue
    Code:
             LNode newNode = new LNode(v);
            
            if (m_front == null)
                m_front = m_rear = newNode;
            else
            {
                m_rear.setLink(newNode);
                m_rear = newNode;
            }
            m_numElements++;
    For push() and enqueue() methods.

    Originally Posted by requinix
    Look at List in the documentation. Do you see where it says
    Code:
    public interface List<E>
    extends Collection<E>
    Do you have any idea what that means?
    Okay, would be the difference between these 2 interfaces:

    public interface List<E> extends Collection<E>

    and

    public interface Collection<E> extends List<E>
    ?

    Thanks.
  14. #8
  15. Lazy Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,436
    Rep Power
    9645
    You want to check whether code uses a particular design pattern? You just demonstrated that you know how to do that.

    Are we not talking about objects? List and Collection and Stack and whatnot? Those are classes/interfaces. You can use instanceof to test whether an object is an instance of a class or interface.

    I don't know what else to tell you.

    Originally Posted by LostShadow
    Okay, would be the difference between these 2 interfaces:

    public interface List<E> extends Collection<E>

    and

    public interface Collection<E> extends List<E>
    ?
    Learn about inheritance: you're using the most object-oriented language there is and inheritance is its most important concept, so not knowing about it is a problem you need to fix right now. You probably need to spend the rest of the day learning about the other core features of object-oriented programming too.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2005
    Location
    Chicago, Illinois, U.S.A.
    Posts
    161
    Rep Power
    0
    So you can have something defined as a list or collection, and the code still be the same, say, for pop() and enqueue() methods?

    Or is it more of how the program outputs the results, for example.

    Is a list a "sorted collection?" And so a collection is an unsorted list? If you sorted a collection, then it is the same as a list?

    If the program sorts, it's a list, otherwise, it's a collection?

    Is it that lists and collections have a bunch of properties, and if you take half of the properties from lists and half of properties from collection, you have a mixed-bag?

    Are we not talking about objects?
    Well, if the interface defined something as a list or collection, can you have a collection object or class in a list interface, or a list class or object, in a collection interface?

    Meh. I give up.
  18. #10
  19. Lazy Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,436
    Rep Power
    9645


    Originally Posted by LostShadow
    So you can have something defined as a list or collection, and the code still be the same, say, for pop() and enqueue() methods?
    If you have a Collection then the object, whatever concrete class it may be, will have those methods too. That is the fundamental purpose of an interface, and a fundamental concept of OOP.

    Please. Go find yourself a bunch of tutorials or articles or videos or whatever about object-oriented programming in Java. There is a lot here you don't know and you really, really need to know them if you're going to work with Java.

    Originally Posted by LostShadow
    Is a list a "sorted collection?"
    No. A List stores things linearly, meaning one after another. Whether it's sorted or not depends on the implementation.

    Originally Posted by LostShadow
    And so a collection is an unsorted list?
    No. A Collection is a vague concept of having a bunch of things. A List is a specific type of Collection (as noted above). Whether a Collection is sorted or not depends on the implementation.

    Originally Posted by LostShadow
    If you sorted a collection, then it is the same as a list?
    No.

    Originally Posted by LostShadow
    If the program sorts, it's a list, otherwise, it's a collection?
    No.

    Originally Posted by LostShadow
    Is it that lists and collections have a bunch of properties, and if you take half of the properties from lists and half of properties from collection, you have a mixed-bag?
    I don't even know what you're thinking.

    Originally Posted by LostShadow
    Well, if the interface defined something as a list or collection, can you have a collection object or class in a list interface, or a list class or object, in a collection interface?
    That one is even more confusing.

    Originally Posted by LostShadow
    Meh. I give up.
    Me too.
  20. #11
  21. Wiser? Not exactly.
    Devshed God 2nd Plane (6000 - 6499 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    6,112
    Rep Power
    4103
    The interfaces java.util.Collection and java.util.List represent concepts/ideas not actual things. That might be where you're getting confused.


    java.util.Collection is the concept that you have many items that are all grouped together in some way.

    java.util.List extends that idea with the additional requirement those things are represented though simple sequential indexing.

    java.util.ArrayList and java.util.LinkedList are specific ways one might implement the concept of a list, one using a simple array and the other using a linked list. These are concrete things that exist, not just concepts.

    If you're writing code that generates a list you need to choose a specific implementation to use.
    Code:
        private static java.util.LinkedList<String> createShoppingList() {
            java.util.LinkedList<String> l = new java.util.LinkedList<String>();
            l.add("Dr. Pepper");
            l.add("Pizza");
            l.add("Wings");
            l.add("Chips");
    
            return l;
        }
    If you're writing code that just consumes a list (ie, iterates over it and does something to each item) then you shouldn't care what specific implementation the list uses, just that it meets the concept of being a java.util.List.
    Code:
        private static void printShoppingList(java.util.List<String> shoppingList) {
            for (String item : shoppingList) {
                System.out.println(item);
            }
        }
    I think you need to just take some time to read up on Inheritance and Polymorphism as well as what an interface is so you have a better idea of what's going on.
    Recycle your old CD's



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud

IMN logo majestic logo threadwatch logo seochat tools logo