Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6726049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:52:49+00:00 2026-05-26T09:52:49+00:00

I have the following class. class MyClass<T> It uses the following constructor. MyClass(Comparator<T> comparator,

  • 0

I have the following class.

class MyClass<T>

It uses the following constructor.

MyClass(Comparator<T> comparator, Collection<? extends T> data)

And it has a field which is set in the constructor like so:

this.data = Collections.unmodifiableCollection(data);

In the special case where T implements Comparable, I don’t want to require that a comparator be passed in, since I can just use the natural ordering. So I thought I should be able to use this constructor:

public <T extends Comparable<T>> MyClass(Collection<T> data)

But there is apparently a type mismatch: cannot convert from Collection<T> to Collection<? extends T> in the assignment statement above. I’ve tried all sorts of things: adding more generic parameters, and so on, but none work. I seem unable to specify a bound that says: if you have a type T that implements Comparable, do the straightforward thing.

Any ideas?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T09:52:50+00:00Added an answer on May 26, 2026 at 9:52 am

    Unfortunately I don’t think this kind of “if Comparable do this else do that” logic is possible with the Java type system.

    You could split the Comparable and non-Comparable cases into separate classes and hide them behind an interface, something like this:

    interface Interface<T> {
        public void processData();
    }
    
    class MyClass<T> implements Interface<T> {
        private final Collection<? extends T> data;
        MyClass(Comparator<T> comparator, Collection<? extends T> data) {
            this.data = data;
        }
        public void processData() {
            // ...
        }
    }
    
    class MyCompClass<T extends Comparable<T>> implements Interface<T> {
        private final Collection<? extends T> data;
        MyCompClass(Collection<? extends T> data) {
            this.data = data;
        }
        public void processData() {
            // ...
        }
    }
    
    class Factory {
        static <T extends Comparable<T>> Interface<T> create(Collection<? extends T> data) {
            return new MyCompClass<T>(data);
        }
        static <T> Interface<T> create(Comparator<T> comparator, Collection<? extends T> data) {
            return new MyClass<T>(comparator, data);
        }
    }
    

    But this might result in a lot of duplicated code. Another option is to leave MyClass requiring a Comparator in its constructor, and build that comparator in the factory:

    class MyClass<T> {
        private final Collection<? extends T> data;
        MyClass(Comparator<T> comparator, Collection<? extends T> data) {
            this.data = data;
        }
        public void processData() {
            // ...
        }
    }
    
    class Factory {
        static <T extends Comparable<T>> MyClass<T> create(Collection<? extends T> data) {
            return new MyClass<T>(new Comparator<T>() {
                public int compare(T o1, T o2) {
                    return o1.compareTo(o2);
                }
            }, data);
        }
        static <T> MyClass<T> create(Comparator<T> comparator, Collection<? extends T> data) {
            return new MyClass<T>(comparator, data);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that looks like the following: public class MyClass { private
I have the following class, that has too many parameters in the constructor, and
I have the following class in my mind: abstract class MyClass (data: MyData) {
Let's say I have the following class: class MyClass { private: int Data; public:
I have the following class: Class MyClass Property MyInteger as Integer Set(ByVal value as
I have the following code: class MyClass def method foo = MyClass.all end end
I have the following code: class MyClass module MyModule class << self attr_accessor :first_name
I have the following structure public class MyClass : MyBaseClass<System.Int32> { } In a
If I have the following code: public class MyClass { public string myName {
I have the following pattern: template <int a, int b> class MyClass { public:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.