0

I have wrote a search app that put found values in list but now I must to limit it - not more then 10 results. Well - if I have found Equivalent string I will put it to the first place (insert at 0 position) - all others I must store in any other position. Could you help me - what storage to choose - to fit this requirements?

so the sample search string: aa data for search in: bb, 1aa, aa, .........2aa,.........cc,............9aa,................

result on N-step (aa, 1aa, ....9aa) and we need to add 13aa so result must be (aa, 1aa, ..., 13aa) (<-put as last for simplicity)

2 Answers 2

1

I would use ArrayList and check the size of it either by adding a couple of conditions or if you want something more centralized solution then by overriding the add methods like this:

public class LimitedArrayList extends ArrayList<String> {
  @Override
  public boolean add(String e) {
      if (this.size() == 10) {
          // Remove an item or return
      }
      return super.add(e);
      // Or add as first item
      // return add(0, e);
  }
  public void add(int index, String e) {
      if (this.size() == 10) {
          // Remove an item or return
      }
      super.add(index, e);
  }
}

and the addAll methods also.

Edit: fixed type parameter

Sign up to request clarification or add additional context in comments.

Comments

0

There are many solutions to this. simple solution is: Store all the entries in a database and retrieve only top 10 rows or entries from the database.

ex: select top 10 * from table_name;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.