Does an ArrayList create a LinkedHashMap keySet () from an insertion order?

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.

+5
source share
5 answers

This is guaranteed.

Although the Set interface itself does not guarantee any order (well, LinkedHashSet ), the fact that the Map implementation itself ensures that the insert is ordered is pretty much guaranteed that you will get the keys in that order. The interface returned by .keySet() is simply Set , since, well, the keys in Map guaranteed to be unique.

If this were not so, consider what will happen in this situation:

 // case 1 for (final Map.Entry<K, V> entry: map.entrySet()) { // entry.getKey(), entry.getValue() } // case 2 for (final K key: map.keySet()) { V value = map.get(key); } 

If these two codes had two different behaviors, then ...

+4
source

Yes, it is guaranteed.

In Oracle JDK, the LinkedHashMap class LinkedHashMap the newKeyIterator() method, which returns an instance of the class that inherits LinkedHashIterator . This is streamlined.

+2
source

Other answers explain that LinkedHashMap#keySet().iterator() iterates in that order, so let me add:

ArrayList Javadok talks about ArrayList(Collection) :

Creates a list containing the elements of the specified collection in the order in which they are returned by the collection iterator.

This ensures that the ArrayList elements are in the same order.

+1
source

The iteration order of LinkedHashSet guaranteed to be the same as the order in which the keys are inserted (unless you use a special constructor that allows you to request orders for the most recent access). You can find this in his documentation .

I suppose you can argue that the iterative order guarantee extends only to the set of records and not to other views, but the documents do not support this well (since they relate to the iteration order), and in practice, the collection views have a common iteration order.

For its part, the ArrayList(Collection) constructor is documented to populate the list in the order in which the elements are returned by the specified Collection iterator.

+1
source

How is this question explained Does entrySet () in LinkedHashMap also guarantee order? and noted in JavaDocs , iteration-type operations are defined in insertion order for LinkedHashMap . This affects the keySet , as well as the entrySet .

The ArrayList constructor will be inserted into the List in iterator order.

These two conditions are combined, which means that the API guarantees this behavior.

+1
source

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


All Articles