aspose file tools*
The moose likes Beginning Java and the fly likes Generics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Generics " Watch "Generics " New topic
Author

Generics

Nathan Doe
Greenhorn

Joined: Oct 17, 2009
Posts: 24
I am relatively new to the concept of generics. I am working on a project that simulates a registrars office at a school. It has a Clerk, Student and RegistrarSimulator class. In the Clerk class it is supposed to have a reference to a Queue in the constructor and that is then supposed to be stored in a ConcurrentLinkedQueue. In the API the ConcurrentLinkedQueue constructor is:

public ConcurrentLinkedQueue(Collection<? extends E> c)

What exactly does that mean and how could I implement it in my program?


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Isn't the API documentation clear on that point? Or do you mean the bit about extends E? In which case start with the Java™ Tutorials: use ctrl-F for generics and look for wildcards. It means the Collection contains the same type "E" or one of its sub-classes.
Nathan Doe
Greenhorn

Joined: Oct 17, 2009
Posts: 24
So for my project I would do

ConcurrentLinkedQueue <Collection<? Extends Queue> c>

or is that still incorrect?
colton peterson
Ranch Hand

Joined: Nov 18, 2007
Posts: 97

First look at the name of the class

ConcurrentLinkedQueue<E>

whenever you would use the classes name you substitute E with the class of your choice For example, if I wanted to make a queue of Fruit objects it would go:

ConcurrentLinkedQueue<Fruit> fruits;

so now it holds fruit objects to initialise it, I would go:

fruits = new ConcurrentLinkedQueue<Fruit>(); //using the no-arg constructor

but you wanted to use the other constructor which is

ConcurrentLinkedQueue (Collection<? Extends E> c);

so the what this constructor takes is a Collection whose objects extend E so in my example I could give it an arraylist of fruit, or a hashset of pears(provided that Pear extends Fruit) or any collection that has Fruit as a superclass. So:

pears = new ArrayList<Pear>(); //Pear extends Fruit
pears.add(new Pear());
fruits = new ConcurrentLinkedQueue<Fruit>(pears); //because pears is a Collection of Pear objects which would extend E(which stands for Fruit in this case)


www.mormon.org
Nathan Doe
Greenhorn

Joined: Oct 17, 2009
Posts: 24
Thanks that helped a lot. If i am just using the ConcurrentLinkedQueue for a queue representing a line at a school registrar office, would the first constructor be better do you think or the second?

colton peterson
Ranch Hand

Joined: Nov 18, 2007
Posts: 97

well the second one would only be used if you already have a list of people in line, which if you ask me wouldn't make sense. The first one is usually the one you want, but if you had any code I could say for sure
Nathan Doe
Greenhorn

Joined: Oct 17, 2009
Posts: 24
I dont have any code yet... but at first there will be no one in line and then after a certain amount of time the line will start to accumulate. So i will go with the first constructor.

Thanks again.
colton peterson
Ranch Hand

Joined: Nov 18, 2007
Posts: 97

cool, I have a question though, will your program be mulit threaded?
Nathan Doe
Greenhorn

Joined: Oct 17, 2009
Posts: 24
Yeah, that is actually the purpose of the assignment I am working on - to work with threads.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Generics