### Thread: How do you tell a Collection from a List?

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

Join Date
Jul 2005
Location
Chicago, Illinois, U.S.A.
Posts
179
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. 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?
3. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Jul 2005
Location
Chicago, Illinois, U.S.A.
Posts
179
Rep Power
0
Hmm I would say both.
4. 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.
5. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Jul 2005
Location
Chicago, Illinois, U.S.A.
Posts
179
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?
6. 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?
7. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Jul 2005
Location
Chicago, Illinois, U.S.A.
Posts
179
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.
8. 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.
9. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Jul 2005
Location
Chicago, Illinois, U.S.A.
Posts
179
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.

10. 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.
11. 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.
12. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Jul 2005
Location
Chicago, Illinois, U.S.A.
Posts
179
Rep Power
0
Originally Posted by requinix
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.
My question isn't what is inheritance.

It was, what are some of the descriptive differences between

public interface List<E> extends Collection<E>

and

public interface Collections<E> extends List<E>

Assuming the 2 aren't the same.
13. In the first one, the List<E> interface has all the public behavior of the Collection<E> interface. Any method you can call on a Collection<E> you can also call on a List<E>. This is what Java uses.

In the second one, the Collection<E> interface has all the public behavior of the List<E> interface. Any method you can call on a List<E> you can also call on a Collection<E>. This is not what Java uses.