reorder a list in java I have three ways of reordering a list (think baseball order) and I am sure you can come up with more.  What is a the best way?

For example if input is list is 1,2,3,4 and current is 3 then the output should be 3,4,1,2.
Another example list: 1,2,3,4 and current 4 output is 4,1,2,3.

public void reOrder(List&lt;String&gt; list, String current) {

        if (list.size() &gt; 1) {
            for (int i = 0; i &lt; list.size(); i++) {
                String host = list.remove(0);
                if (host.equals(current)) {
                    list.add(0, host);
                    break;
                }
                list.add(host);

            }
        }
    }

   public void reOrder2(List&lt;String&gt; list, String current) {

        if (list.size() &gt; 1) {
            int indexOfWorkingHost = list.indexOf(current);
            if (indexOfWorkingHost != -1) {
                for (int i = 0; i &lt; indexOfWorkingHost; i++) {
                    list.add(list.remove(0));
                }
            }
        }
    }

public void reOrder3(List&lt;String&gt; list, String current) {

    if (list.size() &gt; 1) {
        List&lt;String&gt; tmpServers = new ArrayList&lt;String&gt;();
        tmpServers.addAll(list);
        for (String host : tmpServers) {
            if (host.equals(current)) {
                break;
            }
            String first = list.remove(0);
            list.add(first);
        }
    }
}
The first thing you need to do is define what you mean with "best". Fastest to execute? Most readable? Best Java-ness?

In any case neither solution is very "nice". The first one unecessarily removes the "current" item und re-adds it, the second one uses indexOf, thus indirectly has two loops, and finally the third one makes a copy of the list, which is even slower.

When using "low level" removing/adding and modification of the original list, I guess I'd do it like this (untested):

public void reOrder(List&lt;String&gt; list, String current) {
   int i = list.length;
   while (i-- &gt; 0 &amp;&amp; list.get(0) != current)
      list.add(list.remove(0));
}


(BTW, consider making use of the generics: public &lt;T&gt; void reOrder(List&lt;T&gt; list, T current) {...)

There is also a Java API function that does this (Collections.rotate), but it requires the index of the "current" element. If you have a "better" or more optimized why to get the index, other than the "left to right" search of indexOf (such as a binary search, or a map), then that would be a better solution.

In any case, using the current item's index instead of the item itself is something you should consider (depending on the the architecture of your project), because it generally not a good idea to have to search for the index of an item every time you do something with the list.

If speed is really a practical problem for you, then another thing you should check, is if you really need to reorder the actual list. There are ways just to create a "view" of the list in the desired order which would basically take virtually no additional time (especially it would be independently from the length of the list) to create and use.