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

  • Home
  • SEARCH
  • 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 6976241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:28:16+00:00 2026-05-27T17:28:16+00:00

I have the following inline Comparator. private static class SampleSorter implements Comparator<SampleClass>{ public int

  • 0

I have the following inline Comparator.

private static class SampleSorter implements Comparator<SampleClass>{
    public int compare(SampleClass o1, SampleClass o2) {
        if (o1 instanceof Comparable) {
            return ((Comparable) o1).compareTo(o2);
        } else if (o2 instanceof Comparable) {
            return -((Comparable) o2).compareTo(o1);
        }
        return 0;
    }
}

Which generates the following warnings:

Comparable is a raw type. References to generic type Comparable<T> should be parameterized


And if I instead parameterize the types as suggested:

        if (o1 instanceof Comparable) {
            return ((Comparable<SampleClass>) o1).compareTo(o2);

Then I get warnings…

Type safety: Unchecked cast from SampleClass to Comparable<SampleClass>


And if I do typechecking:

    if (o1 instanceof Comparable<SampleClass>) {
        return ((Comparable<SampleClass>)o1).compareTo (o2);

I get the following error:

Cannot perform instanceof check against type Comparable<SampleClass>. Use the form Comparable<?> instead since generic type information will be erased at runtime


And again, if I follow the advice of the error message:

    if (o1 instanceof Comparable<?>) {
        return ((Comparable<?>)o1).compareTo (o2);

I get this error:

The method compareTo(capture#4-of ?) in the type Comparable<capture#4-of ?> is not applicable for the arguments (SampleClass)


Now I don’t know how to procede, I really prefere code which is warning- and error-free.
How do I produce warning-free code with the wanted behaviour?

  • 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-27T17:28:17+00:00Added an answer on May 27, 2026 at 5:28 pm

    The problem you have is there is no way for the compiler to know that what you are doing is safe. Instead you have to trust you know what you are doing and you can add this annotation

    @SuppressWarnings("unchecked")
    

    tot he method or the class and the warning go away.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following static function: static inline HandVal StdDeck_StdRules_EVAL_N( StdDeck_CardMask cards, int n_cards
I have the following code: template <typename Provider> inline void use() { typedef Provider::Data<int>
I have the following code: template <int size> inline uint hashfn( const char* pStr
I have following two classes: template <size_t size> class Cont{ public: char charArray[size]; };
Suppose I have the following HTML: <div id=test> <span style=display:inline;>foo</span> <span style=display:none;>bar</span> <span style=display:inline;>baz</span>
The following practice is fairly commonplace in the inline JavaScript I have to work
I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public
I have the following that I execute inline on a number of screens: tinyMCE.init({
i have the following css code: .tag { display: inline; font-size: 10px; padding: 5px;
I have the following markup with inline javascript and would like to change it

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.