473,509 Members | 2,575 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Generic generics help

I'm trying to write a data class using generics. This class is a Sparse
Matrix meaning that it can have many dimensions but not many entries (e.g. a
100 by 100 matrix, but there might only be 10 entries in the matrix).

I intend to use LinkedLists or Trees to manage the row and column keys and
probably a hash Map to manage the cells - but that's not important right
now.

My first step is that in my add method, I need to ensure that my row and
column keys are in my list. I would like to use one common routine for this.
The problem is I can't figure out the right generics syntax.

Here is what I started with:

public class SparseMatrix<R, C, E{

private TreeSet<RrowHeaders = new TreeSet<R();
private TreeSet<CcolHeaders = new TreeSet<C();

public void add (R rowKey, C colKey, E element) {

ensureExists (rowHeaders, rowKey);
ensureExists (colHeaders, colKey);
}

private void ensureExists (TreeSet<? extends ObjecttreeSet, Object
key) {
Iterator<? extends ObjectkeyIt = treeSet.iterator();
Object k = null;
while (keyIt.hasNext()) {
k = keyIt.next();
if (k.equals(key)) {
break;
}
k = null;
}
if (k == null) {
treeSet.add (key);
}
}
}
The problem is that the compiler is complaining about the treeSet.add (key)
near the bottom. The error is:
symbol : method add(java.lang.Object)
location: class java.util.TreeSet<capture#346 of ? extends java.lang.Object>
treeSet.add (key);

So far I've searched google for the error message and tried various (random)
combinations of ensureExists method signatures, but just can't get it to
work. The only thing I've found to work is if I duplicate the routine and
overload it as in:
private void ensureExists (TreeSet<RtreeSet, R key) {
....
private void ensureExists (TreeSet<CtreeSet, C key) {
....

Obviously I'd rather not duplicate the method - that seems silly.

Any help appreciated.

TIA

Aug 29 '08 #1
4 2054
wizard of oz wrote:
public class SparseMatrix<R, C, E{

private TreeSet<RrowHeaders = new TreeSet<R();
private TreeSet<CcolHeaders = new TreeSet<C();

public void add (R rowKey, C colKey, E element) {

ensureExists (rowHeaders, rowKey);
ensureExists (colHeaders, colKey);
}

private void ensureExists (TreeSet<? extends ObjecttreeSet, Object
The problem is that the compiler is complaining about the treeSet.add
Well, the problem is that "rowHeaders" and "colHeaders" contain types of
R and C respectively, and you're trying to add a type of Object.

If you want to ensure that some type R "rowKey" exists in rowHeaders, then

boolean exists = rowHeaders.contains( rowKey );

will do the whole thing for you. I think however you are confused as to
what a Set like TreeSet will actually do for you. You seem to be trying
to ensure that the /pair/ R, E exists, in which case you need a Map, not
a Set.

E tempElement;
if( (tempElement = rowMap.get( rowKey )) != null ) {
// the pair (rowKey, element) exist
}
You might even put rowKey and colKey together in one single object
though and just do look-ups on that. Faster and easier.
Aug 29 '08 #2
Thanks to Mark and Lucas for your replies.

You are both of course correct, in fact I simply add the new keys to the row
and column headers. Because they are sets, I don't get any dups.

However the question is still relevant. What if I want to have a single
method that can manipulate either one of the headers and accept a parameter
appropriate to the "type" of header (i.e. an R or C)?

The signature
private void ensureExists (TreeSet<? extends ObjecttreeSet, Object
key) {

Allows me to pass either type of header set (row or column), but I can't
pass the parameter, so my question is about how do I get something like
these to work:
private void someMethod (TreeSet<? extends ObjecttreeSet, <? extends
Objectkey) {
or
private void someMethod (TreeSet<? extends ObjecttreeSet, ? key) {
or
private void someMethod (TreeSet<XtreeSet, X key) {

None of the above worked for me.
Another variant is
private TreeSet<Xsubset (TreeSet<XtreeSet, X startKey, X endKey) {
Where X is either an R or C. and the subset returns a subset of values
between the start and end key values.

Surely this is possible!?!?!

"Mark Space" <ma*******@sbcglobal.netwrote in message
news:g9**********@registered.motzarella.org...
wizard of oz wrote:
>public class SparseMatrix<R, C, E{

private TreeSet<RrowHeaders = new TreeSet<R();
private TreeSet<CcolHeaders = new TreeSet<C();

public void add (R rowKey, C colKey, E element) {

ensureExists (rowHeaders, rowKey);
ensureExists (colHeaders, colKey);
}

private void ensureExists (TreeSet<? extends ObjecttreeSet, Object
>The problem is that the compiler is complaining about the treeSet.add

Well, the problem is that "rowHeaders" and "colHeaders" contain types of R
and C respectively, and you're trying to add a type of Object.

If you want to ensure that some type R "rowKey" exists in rowHeaders, then

boolean exists = rowHeaders.contains( rowKey );

will do the whole thing for you. I think however you are confused as to
what a Set like TreeSet will actually do for you. You seem to be trying
to ensure that the /pair/ R, E exists, in which case you need a Map, not a
Set.

E tempElement;
if( (tempElement = rowMap.get( rowKey )) != null ) {
// the pair (rowKey, element) exist
}
You might even put rowKey and colKey together in one single object though
and just do look-ups on that. Faster and easier.
Aug 29 '08 #3
wizard of oz wrote:
Surely this is possible!?!?!
No it's not.

In general you may have to abandon generics or pass around type tokens.
Check out _Effective Java_ by Joshua Bloch for more information on
ways to actually use generics.

However for your specific case this will do pretty well:

private <Tboolean ensureExists( Set<Ts, T key ) {
return s.contains( key );
}

Your program does have serious logic errors, you should use the design
Lucas gave you. In particular, ensureExists() won't tell you that the
/pair/ R, C exists, only that someone has stored some R and C in the
structure previously.

Aug 29 '08 #4
Does anyone think Java generics are misnamed?

Suppose I have the following:

LinkedList myList;
LinkedList <Foo yourList;

Isn't "myList" *more* generic, since it can hold
any type of Object, and can hold a mixture of
Objects (that is, the Objects in myList can have
different types)? Isn't "yourList" *less* generic,
since it can only hold Objects of type <Foo?
Sep 9 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
3290
by: Andreas Huber | last post by:
What follows is a discussion of my experience with .NET generics & the ..NET framework (as implemented in the Visual Studio 2005 Beta 1), which leads to questions as to why certain things are the...
3
2749
by: Tigger | last post by:
I have an object which could be compared to a DataTable/List which I am trying to genericify. I've spent about a day so far in refactoring and in the process gone through some hoops and hit some...
9
12777
by: mps | last post by:
I want to define a class that has a generic parameter that is itself a generic class. For example, if I have a generic IQueue<Tinterface, and class A wants to make use of a generic class that...
1
3416
by: interX | last post by:
Hi I'm new in VC++ and have a question to generics. I have a generic class, which contains an array of the generic type. This array I can pin and then I would like to get an unmanaged pointer to...
13
3794
by: rkausch | last post by:
Hello everyone, I'm writing because I'm frustrated with the implementation of C#'s generics, and need a workaround. I come from a Java background, and am currently writing a portion of an...
10
1912
by: Egghead | last post by:
Hi all, Can someone kindly enough point me to some situations that we shall or "must" use Generic Class? I can foresee the Generic Method is powerful, but I can not find a single situation that...
10
2691
by: phancey | last post by:
I'm quite new to generics. I have 2 generic classes: MyClass<Tand MyOtherClass<T>. MyClass<Thas 2 public Add methods Add(MyOtherClass<T>); Add(MyOtherClass<Wrapper<T>>); (Wrapper<Tis another...
6
4614
by: Venkatesh Bhupathi | last post by:
Hi All, I am trying to inherit the Generic IList<Tinterface to form the typed collection of objects of type T. Following is my sample code, Public Class Record { public string name;...
8
3137
by: MMAS | last post by:
Hey everyone -- Curious about some strange behaviour I'm seeing that seems to be related to my lack of understanding on how generics work in C#. Here's some simplified code (sorry for strange...
11
2529
by: Scott Stark | last post by:
Hello, The code below represents a singly-linked list that accepts any type of object. You can see I'm represting the Data variable a System.Object. How would I update this code to use...
0
7135
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7342
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7067
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7505
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5060
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3215
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3201
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1570
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
774
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.