Design pattern for filtering a collection of items?

前端 未结 6 948
既然无缘
既然无缘 2021-02-04 04:43

Imagine the typical type of application where you have a list of items with different properties. E.g. a tree-view with 100 items, each having a name, a rating

6条回答
  •  既然无缘
    2021-02-04 05:21

    Your looking for a relational database, from which you can use SQL. You can stand up a full one and connect to it from your application, or you can do something in between a full database and straight objects. In Java, for example, you could use an in-memory database like HSQLDB / JavaDB and use the featuers of that. You could also use JoSQL which will let you operate in SQL directly on your objects without the database at all.

    Alternatively, if you wanted to program the thing yourself, you would start by keeping 2 copies of your data. One is your full set of data, the other is your view of the data after filtering. Then, you would create an index on your data for each column, by sorting the data and keeping its position in the sorted list. The same setup works for a filter match. If something matches a filter for a column, give it a 1, or 0 if not. Then when somebody switches the sort order you copy your data from the full list into the view list, or when the change the filter information, you would only take the data that had a match.

提交回复
热议问题