-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.

asked Jun 15, 2016 at 8:23
6
  • 3
    That depends on what mymap is. Commented Jun 15, 2016 at 8: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 Commented Jun 15, 2016 at 8:26
  • mymap actually is a properties object Commented Jun 15, 2016 at 8: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? Commented Jun 15, 2016 at 8:29
  • 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. Commented Jun 15, 2016 at 9:15

3 Answers 3

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.

answered Jun 15, 2016 at 8:27
3
  • 1
    ok thanks, i'll change the code Commented Jun 15, 2016 at 8: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. Commented Jun 15, 2016 at 8:34
  • @Ciurga as this is your first question you must know if this or any answer has solved your question please consider accepting it 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. Commented Jun 15, 2016 at 9: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

answered Jun 15, 2016 at 8:33
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<String> keys = new ArrayList(mymap.keySet());
Collections.sort(keys);
for (String key : keys) {
 [...]
}
answered Jun 15, 2016 at 8:34

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.