Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics

 
Nathan Doe
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50211
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So for my project I would do

ConcurrentLinkedQueue <Collection<? Extends Queue> c>

or is that still incorrect?
 
colton peterson
Ranch Hand
Posts: 97
Linux Python VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Nathan Doe
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 97
Linux Python VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 97
Linux Python VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cool, I have a question though, will your program be mulit threaded?
 
Nathan Doe
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, that is actually the purpose of the assignment I am working on - to work with threads.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic