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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:39:36+00:00 2026-05-27T06:39:36+00:00

I have a HashMap where the key is of type String and the value

  • 0

I have a HashMap where the key is of type String and the value is of type LinkedList of type String.

So basically, here’s what I’m trying to do.

while (contentItr.hasNext()) {
    String word = (String) contentItr.next();
    if (wordIndex.containsKey(word)) {
        LinkedList temp = (LinkedList) w.get(word); //Error occurs here
        temp.addLast(currentUrl);
    } else {
        w.put(word, new LinkedList().add(currentUrl));
    }
}

The first time that I add a key,value pair, I receive no error. However, when I try to retrieve the linked list associated with an existing key, I get the following exception:

java.lang.Boolean cannot be cast to java.util.LinkedList. 

I don’t have a possible explanation of why this exception occurs.

  • 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-27T06:39:37+00:00Added an answer on May 27, 2026 at 6:39 am

    Try this instead:

    while (contentItr.hasNext()) {
        String word = (String) contentItr.next();
        if (wordIndex.containsKey(word)) {
            LinkedList temp = (LinkedList) w.get(word);
            temp.addLast(currentUrl);
        } else {
            LinkedList temp = new LinkedList();
            temp.add(currentUrl);
            w.put(word, temp);
        }
    }
    

    The problem, as you can see, was in the line that adds a new element to the Map – the method add returns a boolean value, and that’s what was being added to the Map. The code above fixes the problem and adds what you intended to the Map – a LinkedList.

    As an aside note, consider using generic types in your code, in that way errors like this can be prevented. I’ll try to guess the types from your code (adjust them if necessary, you get the idea), let’s say you have these declarations somewhere in your program:

    Map<String, String> wordIndex = new HashMap<String, String>();
    Map<String, LinkedList<String>> w = new HashMap<String, LinkedList<String>>();
    
    List<String> content = new ArrayList<String>();
    Iterator<String> contentItr = content.iterator();
    

    With that, the piece of code in your question can be safely written, avoiding unnecessary casts and type errors like the one you had:

    while (contentItr.hasNext()) {
        String word = contentItr.next();
        if (wordIndex.containsKey(word)) {
            LinkedList<String> temp = w.get(word);
            temp.addLast(currentUrl);
        } else {
            LinkedList<String> temp = new LinkedList<String>();
            temp.add(currentUrl);
            w.put(word, temp);
        }
    }
    

    EDIT

    As per the comments below – assuming that you actually can replace the LinkedList by an ArrayList (which might be faster for some operations) and that the only LinkedList-specific method you’re using is addLast (which is a synonym for add), the above code can be rewritten as follows, in a more Object-Oriented style using interfaces instead of concrete classes for the containers:

    Map<String, String> wordIndex = new HashMap<String, String>();
    Map<String, List<String>> w = new HashMap<String, List<String>>();
    
    List<String> content = new ArrayList<String>();
    Iterator<String> contentItr = content.iterator();
    
    while (contentItr.hasNext()) {
        String word = contentItr.next();
        if (wordIndex.containsKey(word)) {
            List<String> temp = w.get(word);
            temp.add(currentUrl);
        } else {
            List<String> temp = new ArrayList<String>();
            temp.add(currentUrl);
            w.put(word, temp);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a HashMap where the key is a word and the value is
I have wanted to use a HashMap that maps a value of type String
I have HashMap object contains a key x-y-z with corresponding value test-test1-test2 . Map<String,String>
I have a hashmap with some information(key and value) in a perl file. I
I have a List of HashMap 's which has key of type Integer and
Hello if you search in an HashMap<String,String> for a specific value of a key-value-pair,
I am going to have a string like hello world as a hashmap key.
I have this problem.. I have a Hashmap containing the String as the key
I have an ArrayList of HashMap . Each HashMap contains many key-value-pairs. I want
I would like to have a HashMap with only one key-value object. I have

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.