简体   繁体   English

HashMap中的对象被覆盖-Java

[英]Object in the HashMap are overwritten - java

I would like to create an HashMap where the key is a string and the value is a List. 我想创建一个HashMap,其中的键是一个字符串,值是一个List。 All the values are taken from a Mysql table. 所有值均取自Mysql表。 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. 问题是我有一个HashMap,其中的键是正确的键,而值不是正确的键,因为它被覆盖了。 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: 您可以在下面找到HashMap内容的示例:

key: p000000383 content: [t0000000000000019231, t0000000000000019232, t0000000000000019233] 键:p000000383内容:[t0000000000000019231,t0000000000000019232,t0000000000000019233]

key: p000000384 content: [t0000000000000019231, t0000000000000019232, t0000000000000019233] 键:p000000384内容:[t0000000000000019231,t0000000000000019232,t0000000000000019233]

The values that are in "content" are in the last rows in Mysql table for the same key. “内容”中的值在Mysql表的同一键的最后一行中。

    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)

You need to allocate a new listFrom_token each time instead of clear() ing it. 您需要每次分配一个新的listFrom_token而不是clear()对其进行分配。 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. 把在列表HashMap 不会使列表的副本。 You are clearing and refilling the same list over and over. 您正在一次又一次地清除并重新填充相同的列表。

Your data shows that idPath is not a primary key. 您的数据显示idPath不是主键。 That's what you need to be the key in the Map. 那就是您需要成为地图中的关键。 Maybe you should make idToken the key in the Map - it's the only thing in your example that's unique. 也许您应该将idToken设置为Map中的键-这是示例中唯一的唯一特征。

Your other choice is to make the column name the key and give the values to the List. 您的另一选择是使列名成为键并将值提供给列表。 Then you'll have four keys, each with a List containing four values. 然后,您将有四个键,每个键都有一个包含四个值的列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM