I get data something like
{"Employee 1 of ABC", "ABCX"}, {"Employee 2 of ABC", "ABCY"}, {"Employee 3 of ABC", "ABCZ"}
from database through a RefCursor
.
I have a case where I need to preserve the order in which data is read from the database.
Since my data is kind of 'key-value' ,I thought of using a implementation of Map
which is ordered. Hence 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 have to pass the keys to the client, in the same order in which it was retrieved from the database (Cursor).
List<String> empList = new ArrayList<String>(Common.empMap.keySet());
keySet() - The documentation says "Returns a Set view of the keys contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa"
What I expect is that Since ArrayList
is also an ordered collection, I should get the keys in the same way in which it was retrieved/inserted into the Map
.
When I do a sample test program, I am getting 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);
}
}
output: [Employee 1 of ABC, Employee 2 of ABC, Employee 3 of ABC]
However my question is , if this is guaranteed output or is it that I am getting just randomly, and it might vary(?),
- update 09/14/2019
I am separating the answer from the question to avoid any confusion.