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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:31:22+00:00 2026-06-11T15:31:22+00:00

I would like to create an HashMap where the key is a string and

  • 0

I would like to create an HashMap where the key is a string and the value is a List. All the values are taken from a Mysql table. The problem is that I have an HashMap where the key is the right one while the value is not the right one, because it is overwritten. In fact I have for all different keys the same list with the same content.
This is the code:

public static HashMap<String,List<Table_token>> getHashMapFromTokenTable() throws SQLException, Exception{

DbAccess.initConnection();
List<Table_token> listFrom_token = new ArrayList();
HashMap<String,List<Table_token>> hMapIdPath = new HashMap<String,List<Table_token>>();

String query = "select * from token";
resultSet = getResultSetByQuery(query);

while(resultSet.next()){

    String token=resultSet.getString(3);
    String path=resultSet.getString(4);
    String word=resultSet.getString(5);
    String lemma=resultSet.getString(6);
    String postag=resultSet.getString(7);
    String isTerminal=resultSet.getString(8);
    Table_token t_token = new Table_token();
    t_token.setIdToken(token);
    t_token.setIdPath(path);
    t_token.setWord(word);
    t_token.setLemma(lemma);
    t_token.setPosTag(postag);
    t_token.setIsTerminal(isTerminal);
    listFrom_token.add(t_token);
    System.out.println("path "+path+" path2: "+token);
    int row = resultSet.getRow();

    if(resultSet.next()){
        if((resultSet.getString(4).compareTo(path)!=0)){    
            hMapIdPath.put(path, listFrom_token);
                listFrom_token.clear();
            }
        resultSet.absolute(row);

    }
    if(resultSet.isLast()){
        hMapIdPath.put(path, listFrom_token);
            listFrom_token.clear();
    }

}

DbAccess.closeConnection();

return hMapIdPath;

}

You can find an example of the content of the HashMap below:

key: p000000383
content: [t0000000000000019231, t0000000000000019232, t0000000000000019233]

key: p000000384
content: [t0000000000000019231, t0000000000000019232, t0000000000000019233]

The values that are in “content” are in the last rows in Mysql table for the same key.

    mysql> select * from token where idpath='p000003361';
+---------+------------+----------------------+------------+
| idDoc   | idSentence | idToken              | idPath     |
+---------+------------+----------------------+------------+
| d000095 | s000000048 | t0000000000000019231 | p000003361 |
| d000095 | s000000048 | t0000000000000019232 | p000003361 |
| d000095 | s000000048 | t0000000000000019233 | p000003361 |
+---------+------------+----------------------+------------+
3 rows in set (0.04 sec)
  • 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-11T15:31:23+00:00Added an answer on June 11, 2026 at 3:31 pm

    You need to allocate a new listFrom_token each time instead of clear()ing it. Replace this:

                listFrom_token.clear();
    

    with:

                listFrom_token = new ArrayList<Table_token>();
    

    Putting the list in the HashMap does not make a copy of the list. You are clearing and refilling the same list over and over.

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

Sidebar

Related Questions

i get a list from my java code and I would like to create
I would like to create 5-6 classes,I am storing values in hashmap in 1st
I would like to have a HashMap with only one key-value object. I have
I have the following problem: I would like to change value of ExpandableListView's cell
I am retrieving a ParameterizedType object from Field.getGenericType(), and I would like to create
I would like to create a map that uses a class as a key
I would like to create a base class that all classes in my program
Can you create an XML object resource with key-value pairs (basically a hashmap) using
i would like create a array of structure which have a dynamic array :
I would like to create this shape using just css. I am pretty sure

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.