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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:21:26+00:00 2026-05-31T21:21:26+00:00

I need to access the HashMap key elements much like a linked list with

  • 0

I need to access the HashMap key elements much like a linked list with prev and curr pointers to do some comparisons. I typecasted the HashMap Key Iterator to List Iterator to access current as well as previous key elements. Below is the code

HashMap<Node,Double> adj;
ListIterator<Node> li = (ListIterator<Node>) adj.keySet().iterator();

while (li.hasNext()) {
    if (li.hasPrevious()) {
                prev = li.previous();
    } else {
                prev = null;
    }
...
}

But I am getting the below exception

Exception in thread "main" java.lang.ClassCastException: java.util.HashMap$KeyIterator cannot be cast to java.util.ListIterator
at Types$AdjList.makeConnected(Types.java:357)
at Main.main(Main.java:89)

Is there some way that I can typecast a HashMap Key Iterator to List Iterator to solve my purpose. Any help will be appreciated.

Thanks,

Somnath

  • 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-31T21:21:27+00:00Added an answer on May 31, 2026 at 9:21 pm

    The iterator of adj.keySet() cannot be casted to a ListIterator because its keys set (returned by the keySet()) method is not a list, but a Set. Thus it doesn’t have an order.

    You can try to use LinkedHashMap for this purpose or create a new List instance from the keySet like this

    List<Node> nodes = new ArrayList<Node>(adj.keySet());
    

    and then perform the desired manipulations.

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

Sidebar

Related Questions

I have intialized Hash map inside static block, I need to access the hashmap
I need access to the uint64_t typedef from stdint.h in some wrapper code that
I need to access some members marked internal that are declared in a third
I need access to some private variables. Can I remove the private, let them
I need some help to store some data efficiently. I have a large list
I have a file (js.phtml) where my code executes. I need to access some
I have two different modules that need access to a single file (One will
I can't create a NT account for everyone that need access to the reports.
I've got an app running maximized in a borderless window and need access to
I am working on a theme for Opencart and am finding I need access

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.