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 928983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:05:52+00:00 2026-05-15T20:05:52+00:00

I get a warning about unchecked casts on the return (T) value; line. Is

  • 0

I get a warning about unchecked casts on the “return (T) value;” line. Is there a better way to do this, or should I just suppress the warning?

class SomeClass<T>
{
    /* ... other methods ... */

    private Set<T> aSet;
    public T filter(Object value)
    {
        if (this.aSet.contains(value))
            return (T) value;
        else
            return null;
    }
}

edit: I’m stuck with public T filter(Object value) as a signature.

  • 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-15T20:05:52+00:00Added an answer on May 15, 2026 at 8:05 pm

    Elaborating on Tom Hawtin’s answer, you have the option of using a Map<T,T> instead, which gets around the casting issue. If you’re using a HashSet<T> for aSet‘s implementation, HashSet uses a HashMap<T,HashSet<T>> behind the scenes anyway (it uses references to itself as the values – not sure if there’s a reason for this other than choice – with the set’s “values” as keys) and performs the bulk of its operations just by reinterpreting the return values of the Map functions.

    Consequently, you could do this, if you wanted to (and I don’t see an immediate reason why it would be any less performant than a HashSet):

    class SomeClass<T>
    {
        /* ... other methods ... */
    
        private Map<T,T> aSet;
    
        public T filter(Object value)
        {
            // Will return the properly-typed object if it's in
            // the "set" otherwise will return null
            return aSet.get(value);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I cant understand what this warning I get on Xcode is about. Searching for
I get an warning about [Accessibility]Missing contentDescription attribute on image in eclipse. This warning
I just learned about this fine looking syntax Collections.<String>emptyList() to get an empty List
I get this: Warning: session_start(): Cannot send session cookie - headers already sent by
I get this warning in my error logs and wanted to know how to
I only get this warning if I import my helper class (#import JLHelper.h). An
How can I implement the ContactService interface without getting warning about unchecked conversions from
I get notified about unchecked warnings when I compile my Scala project with SBT
I know this is wrong and gcc will give you a warning about it,
Is there a way to avoid Unused variable warning on get_or_create method? level,created= Level.objects.get_or_create(

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.