title: Java Play Framework using Find and JPQL to create an object list with duplicates from string list
body: <p>So I am new to Java and Play still get confused from time to time about quasi object oriented programming...</p>
  
  <p>I need to convert a delimited string with duplicate values into an Object list, also with those duplicate values, in the same order as the string.</p>
  
  <p>Example:</p>
  
  <p>I have a form where a user submits of space delimited string. For example:</p>
  
  <p><a href="http://www.feedme.com?food=apple+orange+banana+apple+grape+apple" rel="nofollow">http://www.feedme.com?food=apple+orange+banana+apple+grape+apple</a></p>
  
  <p>that goes to</p>
  
  <pre><code>public static List&lt;Food&gt; getFoodList(String foodString) {
      foodString = foodString.trim();
      List&lt;String&gt; foods = Arrays.asList(foodString.split("\\s+")); 
      List&lt;Food&gt; foodList = Food.find("name IN (:foods)").bind("foods", foods).fetch();
      return foodList;
  }
  </code></pre>
  
  <p>I print the loop of the String list to the screen and get the expected:
  apple
  orange
  banana
  apple
  grape
  apple</p>
  
  <p>I print the Food.name value for each in the loop of the foodList and get:
  Grape
  Apple
  Banana
  Orange</p>
  
  <p>This is clearly an issue with IN in the WHERE clause, as that's the expected behavior.</p>
  
  <p>So, my current solution is to manually populate the List by looping through List and running the following on each String in the List foods</p>
  
  <pre><code>List&lt;Food&gt; foodList = new ArrayList&lt;Food&gt;(foods.size());
      for ( String name: foods ){
          Food food = Food.find("byName", name).first();
          foodList.add(food);
      }
  </code></pre>
  
  <p>This works how I want it but just doesn't seem very elegant or efficient. The duplicates ARE required, so I think that rules out IN. Does anyone have any better ideas? If the answer is no, would appreciate that too!</p>
  
  <p>Many thanks.</p>