0

I have a list of Strings and I am required to ensure that one particular element is always in the firt position in the list. Here is an illustration. Assuming that my list contains [sicav action, droits de souscription, famille action, fcp actions]. I am required to make sure that 'famille action' is always at the first position before a further processing on the list occurs.

Here is how I did it:

/**
 * Force this list to alway keep the constrained value on top
 * @param liste
 * @param constraint
 */
public void doConstrainList(List<String> liste, String constraint) {

    System.out.println("List initial state: " + liste);
    if (!liste.contains(constraint)) {
        return;
    }

    int indexToProcess = liste.indexOf(constraint);

    String keeper = constraint;

    liste.remove(indexToProcess);
    liste.add(0, keeper);

    System.out.println("List state after processing:" + liste);
}

When I call this method with the list example mentionned above for the same constraint value I obtain the following result

method invocation result

So this is working as expected, but I would like to know if there is a better way to do it. For chances are the list will keep growing in size and I would not like this method to be time consumming. I am using Java 1.6 and I can't use newer Java version. Thanks for any help

alainlompo
  • 4,414
  • 4
  • 32
  • 41
  • 3
    Maybe a list is not the best data structure to use. Can't you create a custom class, containing the first element separately and the rest in a list? This class might even implement the list interface, so to the outside it would just be a list, but internally it could maintain that constraint. – tobias_k Mar 19 '15 at 10:16
  • @tobias_k. Thank you I will study this option. For more information the List is coming from a service and that service obtains it from a Spring Data repository. I receive it in a spring mvc controller. That's when I have to apply this constraint before sending the list to a view as an attribute of the model. – alainlompo Mar 19 '15 at 10:23
  • try priorityQueue instead? – Rusheel Jain Mar 19 '15 at 10:24
  • @Rusheel Jain, how would you do that? – alainlompo Mar 19 '15 at 11:40
  • 1
    http://stackoverflow.com/questions/683041/java-how-do-i-use-a-priorityqueue especially the second example of doctor-patient. Should help you in my opinion. – Rusheel Jain Mar 19 '15 at 15:10
  • @Rusheel Jain, thank you. Yes I believe it should help me. I will try it. – alainlompo Mar 19 '15 at 16:27
  • @Rusheel Jain, thanks again. I successfully used the PriorityQueue to solve the issue – alainlompo Mar 20 '15 at 13:32
  • @alainlompo glad to help. Welcome :) – Rusheel Jain Mar 22 '15 at 18:46

1 Answers1

1

I think you can save some processing time by refactoring your code to this:

public void doConstrainList(List<String> liste, String constraint) {

    System.out.println("List initial state: " + liste);
    if (liste.remove(constraint)) {
        liste.add(0, constraint);
    }

    System.out.println("List state after processing:" + liste);
}
owenrb
  • 535
  • 2
  • 7