• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Collections

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks!

K
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!)
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ;-)
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.

K
 
reply
    Bookmark Topic Watch Topic
  • New Topic