0

I have the following List:

private String[] myArray = new String[]{"A","B","C"} ;
List myList = new ArrayList();
for (int i = 0; i < myArray.length; i++) {
            myList.add(myArray[i]);
}

There is a method that returns an element of the myList

String returnValue=myMethod(); 

returnValue could be A or B or C

returnValue should be the first element of the List

myList.add(0, returnValue);

Issue is if returnValue="B" ,myList becomes {"B","B","C"}.

I could explicitly remove the returnValue and add it again to myList. But it seems rather redundant ,can anybody suggest a better design approach.

My Required Result would be {"B","A","C"}

codingBliss
  • 135
  • 5
  • 18

3 Answers3

4

As far as I understood, you want the returnValue to become the first element of the List, i.e. change the order. This can be achieved by using:

Collections.swap(myList, 0, myList.indexOf(returnValue));

Since this does not use adding nor removing, it allows to simplify your entire code:

List<String> myList = Arrays.asList("A","B","C");
String returnValue=myMethod();
Collections.swap(myList, 0, myList.indexOf(returnValue));

Note that this changes the order of the remaining elements. If you want to retain the order of all elements but the one you move to the front of the list (like remove followed by add would do) you need to use rotate:

List<String> myList = Arrays.asList("A","B","C");
String returnValue=myMethod();
Collections.rotate(myList.subList(0, myList.indexOf(returnValue)+1), 1);

But using swap will be faster on large lists, so if you don’t need the remaining elements to retain the original order, using swap is recommended.

Holger
  • 285,553
  • 42
  • 434
  • 765
  • Nice answer +1. But just swap() works fine. It's rotate() that will change indexes. – Ravi K Thapliyal Feb 17 '15 at 12:06
  • @Ravi Thapliyal: `rotate` will change indexes but retain the *relative* order of the elements which is relevant regarding the position of the formerly first element. With `swap` it will jump to the old position of the newly first element thus might have a different position relative to all other remaining elements. With `rotate` it will move to the second position thus stay before all remaining elements. – Holger Feb 17 '15 at 12:26
  • Isn't `rotate` redundant, when you can use a `LinkedList` and remove/add the item in the most efficient way? – simurg Feb 17 '15 at 12:28
  • @simurg: If your list is already a `LinkedList`, using `remove` followed by `add` would be sufficient, however, in most real life cases you don’t have a `LinkedList` as the overhead of `LinkedList` is so large that nobody uses it. Even filling it with the three elements of this example exceeds the overhead of using `rotate` on a three-element `ArrayList`. – Holger Feb 17 '15 at 12:32
  • @Holger Ah okay, my bad. Somehow, I read order as the index of the elements. – Ravi K Thapliyal Feb 17 '15 at 12:36
  • @Holger I think you're right. Here's a related good read: http://stackoverflow.com/questions/322715/when-to-use-linkedlist-over-arraylist) – simurg Feb 17 '15 at 12:43
1

List.add(index, element) doesn't replace the first element in your List, it inserts and thereby it moves all the following elements. You would end up with 4 entries in your list:

{RETURNVALUE, "A","B","C"}

There's also a replacing variant: List.set(index, element). With that you would end uo with 3 entries in your list:

{RETURNVALUE,"B","C"}

If you want to keep your List all sorted, there's the simple utility method

Collections.sort(mylist);

that sorts your collection in respect of it's element's compareTo() method, which would be a simple String ordering in your case.
If your RETURNVALUE is B and you've inserted the element, you would end up with

{"A","B","B","C"} 
Zhedar
  • 3,480
  • 1
  • 21
  • 44
0

If I good understand your problem, you should use set(int index, E element) method to replace given element in your list

krzysiek.ste
  • 2,246
  • 1
  • 26
  • 24