Reputation: 105
I have a method where I read data from a DB, it goes like this:
public Collection<Map<String, String>> getAllFieldsValues() throws Exception
...
mapaTemp.put("DNI", dni.toString());
mapaTemp.put("NOMBRE", nombre.toString());
mapaTemp.put("APELLIDOS", apellidos.toString());
mapaTemp.put("CURSO", curso.toString());
mapaTemp.put("DIRECCION", direccion.toString());
allFieldsValues.add(mapaTemp);
...
return allFieldsValues;
Then I have another method to display the data in a JTable, but the problem is that I read it in a different order from what I put it, I read it in this order. DNI,DIRECCION,NOMBRE,APELLIDOS,CURSO.
This is a problem because when I display the data in the JTable it appears in wrong order. Anyone knows why can it be? Thanks!
Upvotes: 2
Views: 2405
Reputation: 94489
Try using a LinkedHashMap
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
public class Test {
public static void main(String[] args) {
Map mapaTemp = new LinkedHashMap();
// Add some elements
mapaTemp.put("DNI", "1");
mapaTemp.put("NOMBRE", "2");
mapaTemp.put("APELLIDOS", "3");
mapaTemp.put("CURSO", "4");
mapaTemp.put("DIRECCION", "5");
for (Iterator it = mapaTemp.keySet().iterator(); it.hasNext();) {
Object key = it.next();
Object value = mapaTemp.get(key);
System.out.println(value);
}
}
}
Upvotes: 6
Reputation: 6613
What type is mapaTemp and how do you read the values from the map? For example a Java HashMap has no order so you cannot expect to get the results in insertion order.
You should be looking at a LinkedHashMap
Upvotes: 9