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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:15:11+00:00 2026-06-13T12:15:11+00:00

I looked at the other typecasting Java generics questions I’m still confused. I have

  • 0

I looked at the other typecasting Java generics questions I’m still confused.

I have the following class hierarchy: WeightedThing<T> (just adds a weight to some random type) and a custom Vector class of which WeightedVector (no, WeightedVector is not just WeightedThing<Vector>) is a subclass.

I want to do nearest neighbor search and return a list of the closest vectors to a given query vector and their distances. For this, I defined a search method:

public List<WeightedThing<? extends Vector>> search(Vector, int limit) {...}

hoping I can do

List<WeightedThing<WeightedVector>> neighbors = (List<WeightedThing<WeightedVector>>)search(query, 1);

That doesn’t work (IntelliJ doesn’t mark it as an error, but compiling it with Sun’s jdk7u10 for Mac OS X fails). Neither does calling the same function with Vector.
I can force it to compile by adding an upcast to Object, but that seems horrible.

The purpose of this is so I can search and add vectors of any type but if I know I only added WeightedVectors, I want to cast the results back to WeightedVectors.

  • 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-13T12:15:12+00:00Added an answer on June 13, 2026 at 12:15 pm

    List of WeightedThing<? extends Vector> is not same as List of WeightedThing<WeightedVector>. To typecast it cast to wild char types that is any collection and then cast to specific type.

     List<WeightedThing<WeightedVector>> neighbors = (List<WeightedThing<WeightedVector>>)
                                                    (List<?>)search(query, 1);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have looked at other questions and online but still cant figure out where
I looked through other StackOverFlow questions, but I'm a little confused. I'm trying to
So I've looked at other questions and while they've been helpful I'm still horribly
I have looked at other questions regarding this problem but the solutions don't work
I've looked at other questions and still can't figure it out. Why won't it
I looked at other questions and can't figure it out... I did the following
i have looked through other related questions but none of them giving me right
I have looked at other questions in SO and did not find an answer
I have looked at other questions and answers regarding this, but can't seem to
I have looked over other questions on this website, but am yet to find

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.