-3

I have a bean putting a query into a LinkedHashSet as I want the results going in the order the query is. The query going in as an example is id, forename, surname...... How can I find a name in the LinkedHashSet and return the element number for that record. Thanks

Chris

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
user3805878
  • 13
  • 1
  • 3
  • 3
    It's not clear what you're talking about. Please illustrate with some code. – Oliver Charlesworth Jan 05 '15 at 22:48
  • Why is the `[jsf]` tag? If you are talking about `LinkedHashSet` in the context of pure Java SE only then, the tag `[jsf]` is utterly superfluous and you should remove it. If you are however, experiencing some difficulties with a `LinkedHashSet` on JSF side, for example, in accessing a `LinkedHashSet` in an XHTML template then, please be more specific and ask as such by editing the question and providing the necessary details with possible suitable examples, if any. – Tiny Jan 06 '15 at 03:45

1 Answers1

4

LinkedHashSet implements the Set interface. The contract for Set does not require implementations to keep the items in any particular order. As a result, Set does not have methods such as indexOf(Object object) or get(int index). As it happens, a LinkedHashSet does keep the elements in order (insertion order), but this does not mean you can conveniently access the item at a particular index or find the index of an item. It just means that when you iterate over the entire Set, the order will be insertion order.

While your question is not entirely clear, it sounds like you should be using a List rather than a Set. Unless you have a very good reason you should use the most common implementation of List which is ArrayList. To find the index of an item in a List do list.indexOf(item);

Paul Boddington
  • 37,127
  • 10
  • 65
  • 116