我正在尝试设计一个在线电影票预订系统,用户可以在该系统中搜索某个城市的电影/影院,然后预订电影。假设他搜索了一部电影,我需要将所有影院的电影以及该电影的播放时间一起归还。同样地,如果他搜索剧院,我需要将剧院中的所有电影与节目时间一起返回剧院。 我无法弄清楚它是否是用户正在搜索的电影或影院。 我应该使用哪种数据结构进行有效搜索?这是我的想法,但它似乎是一种蛮力的方法。 这是我的班级(部分结构)
ServerAliveInterval
假设我将影院存储为arraylist,我最终会解析这些影院并与影院名称进行字符串比较,然后再与电影名称进行比较。
编辑:为了避免混淆,问题不是如何/在何处保留数据,而是用于加载数据的数据结构,以便我可以围绕它进行有效的搜索操作
答案 0 :(得分:2)
You could load the data into memory at startup, and have two hash maps: one that that is HashMap<TheaterId, ArrayList<MovieId>>
, and the other being HashMap<MovieId, ArrayList<TheaterId>>
. That way you could look things up either way. The problem with this approach is that you either have to periodically check the database for updates, or somehow have the database notify you when updates occur so that you can update your data structures. But then you have to worry about concurrency (updating the data while users are querying it), and things get messy in a hurry.
That's what databases are for. They already know how to handle concurrent updates and queries, and if you set up your indexes correctly, you can search by movie, or by theater. If you're using a dedicated database server, the database will likely keep much of the index and the data cached in memory, so searches are likely to be very fast.
You have a lot to learn if you think that "database is for storing data." Database is for storing, transforming, manipulating, and retrieving data. And probably a few more things, as well.