-2

This is the code:

for(String key : mymap.stringPropertyNames()) {
//mycode
}

This works correctly but I noticed I get the values I need in random order, is there a way to loop through the map using a particular order?

EDIT: Mymap is a properties object.

Ciurga
  • 19
  • 1
  • 5
  • 3
    That depends on what `mymap` is. – Eran Jun 15 '16 at 08:24
  • 1
    @Ciurga, Which map you're using? you need to use TreeMap inorder to store and iterate the contents based upon some logical ordering – Rahul Yadav Jun 15 '16 at 08:26
  • mymap actually is a properties object – Ciurga Jun 15 '16 at 08:27
  • 1
    Do you want a `Map` that respects the order in which the elements were originally inserted, such as `LinkedHashMap`, or do you want a `Map` which sorts the keys in a particular order, such as `TreeMap`? – Dawood ibn Kareem Jun 15 '16 at 08:29
  • @DavidWallace i need something like `LinkedHashMap` – Ciurga Jun 15 '16 at 08:34
  • 1
    Right. Looks like you've got your answer then. May I suggest that you mark one of the answers here as accepted, by clicking on the tick mark? That way, everyone knows that you don't need more information. – Dawood ibn Kareem Jun 15 '16 at 09:15

3 Answers3

6

This is because you are using a Map without sorting like HashMap

[...] This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.


Instead of this, you can use some concrete implementation like:

TreeMap:

The map is sorted according to the natural ordering of its keys or by a Comparator provided at map creation time, depending on which constructor is used.

LinkedHashMap if you need no duplicates...

Hash table and linked list implementation of the Map interface, with predictable iteration order.

Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
  • 1
    ok thanks, i'll change the code – Ciurga Jun 15 '16 at 08:30
  • @Ciurga if you need to loop them mantaining the order and you don't need to access them by key consider to use a List instead. – Davide Lorenzo MARINO Jun 15 '16 at 08:34
  • @Ciurga as this is your first question you must know if **this or any answer has solved your question please consider [accepting it](http://meta.stackexchange.com/q/5234/179419)** by clicking the check-mark. This indicates to the wider community that you've found a solution and **gives some reputation to both the answerer and yourself**. There is no obligation to do this. – Jordi Castilla Jun 15 '16 at 09:52
4

If you want predictable iteration order (insertion order) then use a LinkedHashMap

If you want the elements to be sorted you need to use a TreeMap , in this case the Keys need to implement Comparable interface

Sorin J
  • 542
  • 1
  • 4
  • 14
1

Either change the Map implementation into one of the ones that support ordering or sort the keys before iterating over them. I am a bit confused though, normally one gets the keys of a map by the keySet method. I am not familiar with stringPropertyNames but if it is a Map you should be able to do something like (untested code):

List keys = new ArrayList(mymap.keySet())
Collections.sort(keys)
for ( String key : keys ) {
    [...]
}
jonalv
  • 5,706
  • 9
  • 45
  • 64