• Post Reply Bookmark Topic Watch Topic
  • New Topic

Collections  RSS feed

 
Keith Stansbie
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, All. VERY new to all this so sorry for the basicness of the question.

Can someone please explain what a collection is in words of one syllable? Is it just somewhere to put related classes? Is there anywhere that I can see a bit of example code so that I can get my head around it?

K
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Keith,

Welcome to JavaRanch!

Yes, it's just a place to store a bunch of objects.

In real life, you have different kinds of containers for storing things in different ways: a box, a filing cabinet, a block of lucite. These different containers have different advantages and disadvantages: you can throw things quickly into a box, but then they're hard to find. You can find things quickly in a filing cabinet, but putting them away takes some time. And things inside a block of lucite are visible, and protected from the elements, but can't breathe, so you wouldn't want to put a puppy in there.

Java is the same way. There are collections which make it easy to store things, but harder to find them (Lists), collections that make it easy to find things, but harder to store them (Maps), and collections that silently throw away duplicates (Sets). The code you write to use each kind is quite similar, but the performance and capabilities are different for each kind.

This section of Sun's online tutorial is about Collections.
[ April 25, 2006: Message edited by: Ernest Friedman-Hill ]
 
Keith Stansbie
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!

K
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That was probably one of the best metaphors I've read about why my dog hates it when I encase him in lucite....

(and a nice job of explaining Collections too!)
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I liked the Smalltalk names ... bag, ordered bag, dictionary, etc. Put puppies in a bag before throw BagInRiver exception.
 
Keith Stansbie
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To continue...

I have read up and am still a bit confuddled.

Some types of collections "do not allow duplicates" eg Hashset(? hope this is correct).

What does that mean?

EG Lets say I set up a collection called Vehicles with classes Cars, Bikes, etc. If no duplicates are allowed, does that mean I can only have one type of car (one instance of the class Car)? Or only one car called Ford - one instance of the Car class with the characteristics of a red ford?

The more I think, the more I feel the need to drink ;-)
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Keith Stansbie:

I have read up and am still a bit confuddled.

Some types of collections "do not allow duplicates" eg Hashset(? hope this is correct).


Sets do not have duplicates. Are you familiar with the concept of sets from mathemetics? It's no more strange or befuddling than that. What does it mean to say that x and y are equals? It means that x.equals(y) returns true. Again, that shouldn't seem weird: equals means equals. What difference does this make? Well, consider a typical Collection method like:

boolean add(Object)

The API states that it:

[I]Ensures that this collection contains the specified element. Returns true if this collection changed as a result of the call. (Returns false if this collection does not permit duplicates and already contains the specified element.)[I]

In other words if s is a Set and x.equals(y) then b2 is false and size1 == size2:

boolean b1 = s.add(x);
int size1 = s.size();
boolean b2 = s.add(y);
int size2 = s.size();
 
Keith Stansbie
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am very sorry and hope that i am not winding folk up...

So that means that I cannot put something into a set which is already there. (I recall sets dimly from maths and although I went on to a reasonably numerate degree, never found a practical application of them - until now, obviously...).

But if I have a class in a set eg a class Potato in a set of Vegetables, I can create as many Different instances of Potato (vivaldi, king eddie, jersey, etc) as i want?

I think, and indeed hope, that the answer to that question is YES.

Thankyou for your patience.

K
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, there can be any number of objects of the same class in a set. But each object can only be added a single time.
 
Keith Stansbie
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.

K
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!