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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:34:19+00:00 2026-06-12T18:34:19+00:00

I want to define generic static method in my one of project. Requirement is

  • 0

I want to define generic static method in my one of project.
Requirement is to method return type to be one of method parameter.

The below is my solution.

public static <T> List<T> convertMapToAttribute(Class<T> attrClass, T attr) {
    List<T> list = null;
    if (attrClass.equals(String.class)) {
        list = (List<T>) new ArrayList<String>();
        list.add(attr);
    } else if (attrClass.equals(Integer.class)) {
        list = (List<T>)new ArrayList<Integer>();
        list.add(attr);
    }
    return list;
 }

I have two questions.

  1. Can we avoid this waring “warning: [unchecked] unchecked cast”
    without adding @SuppressWarnings(“unchecked”)?
  2. Is there any other elegant way to solve this above problem
  • 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-06-12T18:34:20+00:00Added an answer on June 12, 2026 at 6:34 pm

    By having Class<T> as a parameter, the way to do a checked cast (and skip the annoying warning) is to invoke attrClass.cast() which will throw ClassCastException if the casting fails. In this case, T should be either String or Integer.

    The problem here is that you’re doing an unchecked cast from a list of T to a list of either String or Integer when you should define the list directly and add the element using a cast:

    if (attrClass.equals(String.class)) {
        List<String> list =  new ArrayList<String>();
        list.add(attrClass.cast(attr));
    }
    

    And the same goes for Integer.

    There’s something weird with your method tough (I don’t understand the intention, actually), you’re creating a list of elements from a type that you’re also passing a parameter… Shouldn’t this work as well? (since you create a list of T types and add an element of T type).

    public static <T> List<T> convertMapToAttribute(T attr) {
        List<T> list = new ArrayList<T>();
        list.add(attr);
        return list;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In C#, I can define an extension method for a generic array of type
I want to write a generic extension method that will return a specified set
I want to define a generic function for printing contents of std::map like types.
I want to define different parameter according to different device, but I got a
I have a project that is using a generic list of a custom class.
In C# one could write something like this: public static class Test { }
I want to define a view which displays a list of label and checkbox,
I am making a package where I want to define a new method for
I'm writing a C++ project and have a generic evaluate method in a class
In Scala, I'd like to be able to define a generic type of abstract

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.