• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Java Homogenous or Heterogenous types

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, I just started getting into Java a couple of weeks ago and i stumbled on a rather strange problem. Its about creating a Homogeneous or Heterogeneous collections in Java. So far I have created a generic BinaryTree class which can use every type i throw at it. So far just the basic one.
But i was wondering how would one implement a Collection which can take ANY type inside (such as String,Double,Integer) all at the same time inside the same collection. As far as i know the collection should be of type Object. The problem i am facing is how would i implement that witohut having to repeat my BinaryTree class and just replacing the generic with object. Is it possible to make it all in one Collection that can take The same and different types of objects at the same time.
For the objects , being diffrent types and all i guess i should override compareTo and i plan on comparing different objects based on their hash code. Comparing is irrelevant in this case since the main idea is to force the collection to take same and different types at the same time.
I want to take ArrayList as an example, it can do exactly that.

 
Saloon Keeper
Posts: 14502
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to CodeRanch, Svetlozar!

I don't understand the problem. If you already have a generic BinaryTree, then why not just declare BinaryTree<Object>?
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java lacks containers and maps with heterogeneous values. As expected, Java objects support fields with heterogeneous types.
 
Svetlozar Iliev
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Welcome to CodeRanch, Svetlozar!

I don't understand the problem. If you already have a generic BinaryTree, then why not just declare BinaryTree<Object>?



Well When i declare Tree<T extends Object & Comparable<T>> , compiler is not happy. Then if i try to do Tree<T extends Comparable<T>> i can't create Tree<Object> , compiler isn't happy again. so i went with Tree<T extends Object> implements Comparable<Object> , this way its all fine but i cant seem to understand how would i be able to compare say Integer to Integer , but i can compare Objects with hashCodes
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Svetlozar Iliev wrote: ... this way its all fine but i cant seem to understand how would i be able to compare say Integer to Integer , but i can compare Objects with hashCodes



Comparable is not only used to determine equality, but is also used to determine which value is greater, etc. How do you do a "compare Objects with hashCodes"? There is no guaranteed way to determine which is greater (or even equality) with hashcodes -- as the relationship is not guaranteed to exist.

Also, on the same note, the Comparable<Object> type doesn't make much sense, as the Object class doesn't implement the Comparable interface.

Henry
 
Ranch Hand
Posts: 239
12
Scala IntelliJ IDE Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the ranch! Generics are one place where Java really could use some work and there are plenty of languages that do it better. Unfortunately it isn't going to change due to early decision in the language design. You might try Scala instead (compiles to the Jvm also) if you are looking for more flexibility in this area.
 
Stephan van Hulst
Saloon Keeper
Posts: 14502
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Personally I think Java is great at generics, the only pity is that the generic type arguments don't carry over to runtime.

If you want a heterogeneous tree, you will have to give it a comparator:
 
Stephan van Hulst
Saloon Keeper
Posts: 14502
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Creating a naturally ordered tree:

Creating a heterogeneous tree that compares keys by their string representation:

This is a very questionable use case though. It's sure to lead to confusion and bugs.
 
If you were a tree, what sort of tree would you be? This tiny ad is a poop beast.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic