Search code examples
javacomparisoncomparable

Java interface to compare various data types


I need to have an interface that will allow this to work for all data types if implemented with generics. Something to this effect.

  if (myIter.myDataType < SearchDataType){
      return myIter;
  }

or

  if (myIter.compareTo(SearchDataType)){
      return myIter;
  }

needs to work with integers, char, strings and even Boolean if possible. I'm new to java so I'm not aware of something that does this.


Solution

  • Something along the lines of:

    class MyDataType<T extends Comparable<T>>{
        T data;
    
        public MyDataType(T _data){
            data = _data;
        }
    }
    

    Feels like a homework question. ;)