-1

this method is supposed to iterate on the TreeMaps keys. Then it should add the key to an ArrayList variable because I need to return it as one. One more thing I want to do is to sort the ArrayList.

    ArrayList<String> getVehiclenames() {
         ArrayList<String> vehicleList = new ArrayList<>();
         for (String elem : vehicles.keySet()) {
             vehicleList.add(elem);
         }
    Collections.sort(vehicleList);
    return vehicleList;
}

I am not 100%ly sure wether thats working, but I still couldnt believe anyways that calling a collections method just sort my vehiclelist like that. I expected something like vehicleList = Collections.sort(vehicleList);.

My questions: Is this working like that? And if yes: How is that working? I tried to look it up but my knowledge is to norrow right now.

yemerra
  • 1,352
  • 4
  • 19
  • 44
  • By sorting I mean alphabetically sorting. – yemerra Apr 20 '14 at 19:12
  • by getting keys from the treemap, you have already got a sorted list, why do you need sort it again? – Kent Apr 20 '14 at 19:14
  • 1
    The keys from the `TreeMap` will already be sorted. There's no need to sort them again, unless you're after a different sort order. – Dawood ibn Kareem Apr 20 '14 at 19:14
  • @David Wallace: Yes, I read that before. But I wasn't sure about how TreeMap sort it. If I have strings there, does it alphabetically sort it everytime I add an element there? – yemerra Apr 20 '14 at 19:17
  • see [this](http://stackoverflow.com/questions/40480/is-java-pass-by-reference) question. it should hep you to understand. – maxx777 Apr 20 '14 at 19:17
  • Sorry, but I don't get it from this question. – yemerra Apr 20 '14 at 19:25
  • I've always thought that the best answer to "does the following code work" is invariably "test it and find out." – Dawood ibn Kareem Apr 20 '14 at 21:14
  • @David Wallace: You are right! But my question was more focused on whether is make sense to do it with Collections.sort() – yemerra Apr 21 '14 at 09:58

2 Answers2

0

From the documentation:

This implementation dumps the specified list into an array, sorts the array, and iterates over the list resetting each element from the corresponding position in the array.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
  • Sorry, english is not my mothertounge. By "resetting each element" they mean resetting the elements of the ArrayList I put in sort() ? – yemerra Apr 20 '14 at 19:15
0

Colletoions.sort((List list) method of the collection interface sorts based on natural ordering. The dataType of list passed in the sort method must be of type Comparable in other words, it should implement comparable interface.

In your case, you have a list of String. and String class implements the comparable interface.

So to answer your question, yes your code should work fine.

For more information, read about comparator & comparable interface http://docs.oracle.com/javase/7/docs/api/java/util/package-summary.html

user3553237
  • 112
  • 1
  • 1
  • 7