[英]How do I store objects if I want to search them by multiple attributes later?
I want to code a simple project in java in order to keep track of my watched/owned tv shows, movies, books, etc. Searching and retrieving the metadata from an API (themovieDB, Google Books) is already working. 我想用Java编写一个简单的项目,以便跟踪自己观看/拥有的电视节目,电影,书籍等。通过API(themovieDB,Google图书)搜索和检索元数据已经可以了。
How would I store some of this metadata together with user-input (like progress or rating)? 如何将其中一些元数据与用户输入(例如进度或等级)一起存储?
I'm planning on displaying the data in a table like form ( example ). 我打算在表格中显示数据( 例如 )。 Users should also be able to search the local data with multiple attributes.
用户还应该能够搜索具有多个属性的本地数据。 Is there any easy way to do this?
有没有简单的方法可以做到这一点? I already thought about a database since it seemed that was the easiest solution.
我已经考虑过数据库,因为这似乎是最简单的解决方案。
Any suggestions? 有什么建议么? Thanks in advance!
提前致谢!
You can use lightweight database as H2 , HSQLDB or SqlLite . 您可以将轻量级数据库用作H2 , HSQLDB或SqlLite 。 These databases can be embedded in the Java app itself and does not require extra server.
这些数据库可以嵌入Java应用程序本身,不需要额外的服务器。
If your data is less, you can also save it in XML or Json by using any XMLParser or JsonParser (eg Gson()). 如果数据较少,则还可以使用任何XMLParser或JsonParser(例如Gson())将其保存为XML或Json。
Your DB table will have various attributes which are fetched from API as well as user inputs. 您的数据库表将具有从API以及用户输入中提取的各种属性。 You can write query on the top of these DBs to fetch and show the various results.
您可以在这些数据库的顶部编写查询以获取并显示各种结果。
There are many ways to accomplish this but as another user posted, a database is the clear choice. 有许多方法可以完成此操作,但是当另一个用户发布时,数据库是明确的选择。
However, if you're looking to make a program to learn with or something simple for personal use, you could also use a multi dimensional array of strings to hold the name of the program, as well as any other metadata fields and treat the array like a table in excel. 但是,如果您要制作一个程序以供学习或简单易用,则还可以使用多维字符串数组来保存程序的名称以及任何其他元数据字段并处理该数组就像Excel中的表格一样。 This is not the best way to do it, but you can get away with it with very simple code.
这不是最好的方法,但是您可以通过非常简单的代码来摆脱它。 To search you would only need to loop through the array elements and check that the name of the program (ie
movieArray[x][0]
matches the search string. Once located you can perform actions or edit the other array indexes pertaining to that movie. 要搜索,您只需要遍历数组元素并检查程序的名称(即,
movieArray[x][0]
与搜索字符串匹配。找到后,您可以执行操作或编辑与该电影有关的其他数组索引。
For a little more versatility, you would create a class to hold the movie information with fields to hold any metadata. 要获得更多的通用性,您可以创建一个类来保存电影信息,并使用字段来保存任何元数据。 The advantage here is that the metadata fields can be different types rather than having to conform to the array type, and their packaged together in the instance of the class.
这样做的好处是元数据字段可以是不同的类型,而不必遵循数组类型,并且可以将它们打包在类的实例中。 If you're getting the info from an API then you can update or create the classes from the API response.
如果您要从API获取信息,则可以从API响应中更新或创建类。 These objects can be stored in an ArrayList and searched with a loop that checks for a certain value ie
这些对象可以存储在ArrayList中,并使用检查特定值的循环进行搜索,即
for (Movie M : movieArrayList){
if(m.getTitle().equals("Arrival")){
return m;
}
}
Alternatively of course for large scale, a database would be the best answer but it all depends what this is really for and what it's needs will be in the real world. 当然,对于大规模的替代方案,数据库将是最好的答案,但是这完全取决于数据库的真正用途以及在现实世界中的需求。
Either write everything to files, or store everything on a database. 要么将所有内容写入文件,要么将所有内容存储在数据库中。 It depends on what you want though.
这取决于您想要什么。
If you choose to write everything to files, you'll have to implement both the writing and the reading to suit your needs. 如果您选择将所有内容写入文件,则必须实现写入和读取操作以适合您的需求。 You'll also have to deal with read/write bugs and performance issues yourself.
您还必须自己处理读/写错误和性能问题。
If you choose a database, you'll just have to implement the high level read and write methods, ie, the methods that format the data and store it on the appropriate tables. 如果选择数据库,则只需实现高级读写方法,即格式化数据并将其存储在适当表上的方法。 The actual reading and writing is already implemented and optimized for performance.
实际的读写已经实现并针对性能进行了优化。
Overall, databases are usually the smart choice. 总体而言,数据库通常是明智的选择。 Although, be careful of which one you choose.
虽然,要小心选择哪一个。 Some types might be better for reading, while others are better for writting.
一些类型可能更适合阅读,而其他类型则更适合书写。 You should carefully evaluate what's best, given your problem's domain.
给定问题的领域,您应该仔细评估最好的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.