Java LinkedHashMap: what's the difference between the two?

EDIT: All code and database script creation can be found from http://gitorious.org/scheator . The script database is located in Schema /.

I have the following Java code:

LinkedHashMap link defined in an abstract class as

LinkedHashMap<Object, Data> list; 

The descendant class that initializes this list as follows:

 list = new LinkedHashMap<Integer, Data>(); 

I add the following elements:

  String id = rs.getString(FIELDS[0]); String name = rs.getString(FIELDS[1]); Data team = new Data(Integer.parseInt(id.trim()), name); list.put(id, team); 

Now when I do this:

  System.err.println("delete() count: " + list.size()); System.err.println("delete() key value " + key); Data obj; obj = (Data)list.remove(key); deletedList.put(key, obj); System.err.println("delete() count: " + list.size()); 

Nothing is removed from the list, i.e. the first and last prints print the same size (). The key is also correct (I checked that there is an element of this identifier).

However, and this is my question, if I add the following values:

  Integer id = rs.getInt(FIELDS[0]); String name = rs.getString(FIELDS[1]); Data team = new Data(id, name); list.put(id, team); 

The code works! Should parseInt () create a similar key for getInt ()? Why does the second version work, but the first does not work? I spent a good hour debugging until I found a reason, and I still can't figure out the reason.

+4
source share
5 answers

Yanamon is right. This is pretty clear when you look at diff :

  while (rs.next()) { - String id = rs.getString(FIELDS[0]); + Integer id = rs.getInt(FIELDS[0]); String name = rs.getString(FIELDS[1]); - Data team = new Data(Integer.parseInt(id.trim()), name); + Data team = new Data(id, name); list.put(id, team); 

Note that in the original version, int is passed to the Data constructor (with an automatic field in Integer). But the id that is being set is still a string.

+1
source

There should not be a difference, but there are a number of things that are not clear from your example:

  • deletedList does not apply to list object
  • the records in your database that are used in both cases are the same (perhaps the first uses a different int that is already on the Map)

Auto boxing can also complicate the problem. Replace the Integer id in the second int id example to pass the same arguments to your Data constructor.

Maybe you can post the full code so that we can accurately reproduce the script?


Update

You use String values โ€‹โ€‹as keys in the source code. Then you have the Object key in the remove (key) method, so I expect you to pass Integer to the method at this point. The string will not match an integer as a key, which explains why your remove did not work.

If you use generics to specify your HashMap ( LinkedHashMap<Integer, Team> instead of <Object, Team> ), this type of error cannot be - the compiler will say something like

The method put(Integer, Object) in the type HashMap<Integer,Object> is not applicable for the arguments (String, String)

+2
source

First example:

 String id = rs.getString(FIELDS[0]); 

Second example:

 Integer id = rs.getInt(FIELDS[0]); 

I cannot say for sure, since I cannot see the rest of the code, but if the key variable is Integer in this call:

 obj = (Data)list.remove(key); 

then deleting will only work if the object was placed on the map using Integer, and that is why it only works when the id is an integer when you call the put method. The string "123" is not equal to the integer 123.

I also assume that you just skipped the line in the first example, but there was no call to list.put (id, team), but that could also be the source of your problems.

+2
source

My assumption is that in the second case, you explicitly point it to Integer

 Integer id = rs.getInt(FIELDS[0]); 

whereas in the first case it remains int

 Integer.parseInt(id.trim()) 

from javadoc parseInt

 static int parseInt(String s) Parses the string argument as a signed decimal integer. 
0
source

If I were you, I would check the contents of LinkedHashMap with a debugger before and after your posting and before and after deleting. Step into the remove() method (source code is part of the JDK) and see what it does. Odds - Your code does not add or remove the object correctly. This is hard to see here because the sample code is incomplete.

As for rs.getInt() and Integer.parseInt() , the first one is database specific (I assume rs is a ResultSet ), and therefore they may not have the same behavior. However, as soon as the Integer key is created (you can verify this using the debugger), it should be equivalent for HashMap or LinkedHashMap purposes. But your sample code complicates the situation; you use rs.getString() and then Integer.parseInt() . Although I would be surprised if this happened, it is possible that the database driver formats the id column to a string that confuses parseInt() . Itโ€™s much more convenient for me to do rs.getInt() .

0
source

Source: https://habr.com/ru/post/1302786/


All Articles