• Post Reply Bookmark Topic Watch Topic
  • New Topic

Creating a Generic Class that works with int[] and double[]  RSS feed

 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got interested in another OP's thread and rather than hijack that thread or interfere with the OP's learning process, I thought I'd start my own thread.

The problem is to create a class that can be called like this:


I got this far at least:

If the class had been called like this:

...then my constructor would work. As it is, I would have to do something like this (I think):

But that seems to destroy the whole idea of a Generic class.

Then I get to the getMax() method. I want to return T but I can't instantiate a variable of that type. So I tried this:

This works fine for the int[] call, but gets this error with the double[] call:


So what are your thoughts on this? Where am I going wrong, or is there truly no elegant solution?
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can only cast an Integer to a T is the class is a Stats<Integer>. Remember the Integer and Double classes are immutable and final, so the only things you can cast them to are Number and Object.
 
Stephan van Hulst
Saloon Keeper
Posts: 7969
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The most elegant solution is to just accept that arrays and collections are not compatible, and to avoid accommodating the use of arrays. If a client really wants to use arrays, it's up to them to do the transformation.
 
Stephan van Hulst
Saloon Keeper
Posts: 7969
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Having said that, if you really want to provide a constructor that accepts an array, you can just do the transformation yourself, and store as a collection. Nowhere does it say you're obligated to store as a generic array. As a matter of fact, whenever you declare a field T[], slap yourself first and then think about if you really need it.
 
Stephan van Hulst
Saloon Keeper
Posts: 7969
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As for the getMax() method, you should make the return type Optional, because empty collections don't have a greatest element.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:As for the getMax() method, you should make the return type Optional, because empty collections don't have a greatest element. . . .
Or insist empty Collections cannot be passed to the constructor by throwing an IllegalArgumentException.
 
Stephan van Hulst
Saloon Keeper
Posts: 7969
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree.
 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:The most elegant solution is to just accept that arrays and collections are not compatible, and to avoid accommodating the use of arrays. If a client really wants to use arrays, it's up to them to do the transformation.

I agree that the way the class is called makes things difficult, but the original OP's question came from a job interview, and these are the "specs" he was given. If I were designing the project for a client, I would suggest a different interface. But part of my question is, this seems really hard given the interviewer's specs. Is this just a bad interview question, or is there really a good way to implement this?
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In some respects it is a good interview question because it allows the candidate to explain why it is impossible to realise.
 
Piet Souris
Master Rancher
Posts: 2041
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about letting the user do all the hard work? I'm thinking of the simple:


Greetz,
Piet
 
Stephan van Hulst
Saloon Keeper
Posts: 7969
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't really think that's useful Piet. The class is nothing else but a wrapper around a function, and doesn't do any useful work.

Why would a user pass a lambda to a constructor, then call a method, when they can also just call the function directly?
 
Piet Souris
Master Rancher
Posts: 2041
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan,

often you are right, but this time too.

I was hoping for some ease of use, like in this:

but having just tried the above, I agree that my little idea does have
a high degree of dustbinworthyness.

Greetz,
Piet
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!