I get the data somehow like
{"Employee 1 from ABC", "ABCX"}, {"Employee 2 from ABC", "ABCY"}, {"Employee 3 from ABC", "ABCZ"}
from the database through RefCursor .
I have a case where I need to keep the order in which data is read from the database. Since my data is “key,” I thought about using a Map implementation that is streamlined. Therefore, chose LinkedHashMap
//defined as a static map inside a common utlity class public class Common{ public static final LinkedHashMap<String, String> empMap = new LinkedHashMap<String, String>(); } //rs is the resultset if (rs != null) { while (rs.next()) { key = rs.getString("name_text"); value = rs.getString("id"); Common.empMap.put(key, value); } }
I need to transfer the keys to the client in the same order in which it was extracted from the database (Cursor).
List<String> empList = new ArrayList<String>(Common.empMap.keySet());
keySet () - The documentation says: "Returns the specified type of keys contained in this map. The set is supported by the map, so changes to the map are reflected in the set, and vice versa"
I expect that since ArrayList also an ordered collection, I have to get the keys in the same way in which it was extracted / inserted into the Map .
When I do a test test program, I get as expected.
public class LinkedMap { public static void main(String[] args) { LinkedHashMap<String, String> map = new LinkedHashMap<String, String>(); map.put("Employee 1 of ABC", "ABCX"); map.put("Employee 2 of ABC", "ABCY"); map.put("Employee 3 of ABC", "ABCZ"); ArrayList<String> list = new ArrayList<String>(map.keySet()); System.out.println(list); } }
: [Employee 1 ABC, Employee 2 ABC, Employee 3 ABC]
However, my question is if this is a guaranteed result or is it something that I get only randomly and it can change (?),
Update: 09/30/2015
Thanks to everyone, each of you has provided valid points.
To summarize all the answers,
It is guaranteed that order is maintained.
According to Javadocs ,
LinkedHashMap is a hash table and a related implementation of the map list interface with a predictable iteration order. This implementation differs from HashMap in that it maintains a double-linked list through all of its entries. This linked list defines the iteration order, which is usually the order in which keys were inserted into the card (insert order)
ie: LinkedHashMap will iterate in the order in which the entries were placed on the map.
So, LinkedHashMap#keySet() will give me the same order in which the keys were inserted on the card, because LinkedHashMap#keySet().iterator() iterates in the specified order.
Going deeper into the implementation of iterator() , we see that
LinkedHashMap implements the newKeyIterator () method, which returns an instance of the class that inherits LinkedHashIterator, taking care of the "ordering"
// These Overrides alter the behavior of superclass view iterator() methods Iterator<K> newKeyIterator() { return new KeyIterator(); } ... private class KeyIterator extends LinkedHashIterator<K> {... private abstract class LinkedHashIterator<T> implements Iterator<T> { ...
To do this: ArrayList<String> list = new ArrayList<String>(map.keySet()); The ArrayList (Collection) constructor is documented to populate the list in the order in which the items are returned by the specified Collection iterator.