• Post Reply Bookmark Topic Watch Topic
  • New Topic

How can we create a generic class that accepts Integer, Float, and Double.  RSS feed

 
Vignesh Ganesh
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All
Greetings.

I have got one Coding Question fired at me in an Interview.
The question is ...
I had to create one Generic class which supports the following client code.

Now I need to implement the generic Stats class that has getMax() method which returns max value from the given array.
Here is the template for the Stats class

What I understood from the question is we need to create three constructors that accept int, float and double arrays.
But I do not have any idea how this class can be implemented.
Kindly throw some light on this.

Best Regards
Vignesh


 
Campbell Ritchie
Marshal
Posts: 56592
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have thought of two possibilities:-
  • Create a base class with Int Float and Double subclasses
  • Make the class generic <T extends Number> and get a T[] in the constructor.
  •  
    Henry Wong
    author
    Sheriff
    Posts: 23295
    125
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    This question doesn't explain the purpose of the generic. It implies that there is a relationship between the generic and its equivalent primitive array, but generics can't really enforce that.

    Henry
     
    Vignesh Ganesh
    Greenhorn
    Posts: 13
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi there ...

    Thanks for your suggestions.

    @Mr. Henry, what if I want to pass a wrapper type array rather than a primitive array as below.

     
    Vignesh Ganesh
    Greenhorn
    Posts: 13
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I think I did it with passing the wrapper type array.


    With the primitive arrays, I thought of writing 3 overloaded constructors this way.

    Kindly check, and provide me some improvements If I am doing it in an ugly way.





     
    Campbell Ritchie
    Marshal
    Posts: 56592
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Those overloaded constructors look like a disaster. What if you write this?
     
    Vignesh Ganesh
    Greenhorn
    Posts: 13
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi there

    @Mr. Ritchie

    You are right.
    I just tried with your line of code. The getMax() is still returning an int value even if the Item is of type Double which is meaningless.

    If we pass the wrapper type arrays, its simple. But I do not think that there is a way to deal with the case of passing primitive arrays from the client code.
    If there is any other way of doing it, then kindly let me know.




     
    Campbell Ritchie
    Marshal
    Posts: 56592
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Nobody else has said it is possible, so I suspect it isn't. There isn't a simple way to convert a primitive array to an Object[]. The nearest I can think of would only work in Java8:-You can get that to work for int[]s long[]s and double[]s but I don't think you can get that sort of trick to work for float[]s.
  • You have known for ages what new int[]{...} means.
  • Arrays.stream turns an int[] into an IntStream.
  • The boxed() call boxes the elements of the IntStream into Integers and returns a Stream<Integer>.
  • The toArray method will create an array. It requires an argument, however, unless you are happy with an Object[].
  • Integer[]::new means to look for the constructor in the Integer‑array class. Yes, there is such a thing but you never see it. That constructor is called to create an Integer[].

  • Alternative: Leave out the Integer[]::new part, and return an Object[]. In your stats class, cast that to T[] and mark the constructor with the @SuppressWarnings("unchecked") annotation. I think that will work for [Dd]ouble[]s and [Ii]nt[eger][]s but not for float[]s, because there ain't no such thing as a FloatStream, because people don't use floats for arithmetic.

    Check carefully whether I have made some sort of syntactic mistake there, and look for my older posts with toArray in, because you may find similar code which has actually compiled.
     
    Vignesh Ganesh
    Greenhorn
    Posts: 13
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hello Mr. Ritchie

    Thank you very much for such a good explanation.


    Best Regards
    Krishna
     
    Campbell Ritchie
    Marshal
    Posts: 56592
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You're welcome
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!