user1281029
user1281029

Reputation: 1533

Collections for getting data in the inserted order

Which collections can I use if I want to put employee details 1)as key value 2) without key value. In both case I want to retrieve data in the order in which they are inserted.Please Help.

Upvotes: 1

Views: 871

Answers (3)

Anuj Balan
Anuj Balan

Reputation: 7729

For Key-Value pair in ordered format: LinkedHashMap

For non - Key-Value pair in ordered format: ArrayList

Upvotes: 0

Greg Kopff
Greg Kopff

Reputation: 16575

LinkedHashMap:

Hash table and linked list implementation of the Map interface, with predictable iteration order. This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order). Note that insertion order is not affected if a key is re-inserted into the map. (A key k is reinserted into a map m if m.put(k, v) is invoked when m.containsKey(k) would return true immediately prior to the invocation.)

Upvotes: 3

Alexis King
Alexis King

Reputation: 43852

For simple List implementations (simple value list storage), you can use the ArrayList class. For Map (key-value storage), use LinkedHashMap. Both of these implementations will preserve insertion order.

Upvotes: 5

Related Questions