[英]Big database, many maps?
As part of a student project I am building a large database, which could theoretically contain millions of objects. 作为学生项目的一部分,我正在构建一个大型数据库,理论上可以包含数百万个对象。
I'm starting with the firstname (ie Person fName = "John"). 我从第一个名字开始(即Person fName =“John”)。
My plan is to convert "John" to a hashcode, convert the hashcode into an Integer, then store it in the map as - (as integer comparisons are faster). 我的计划是将“John”转换为哈希码,将哈希码转换为整数,然后将其存储在地图中 - (因为整数比较更快)。
Now here is my problem - To make the iteration faster, I want to have separate static maps, accessed depending on the first letter of the name. 现在这是我的问题 - 为了使迭代更快,我希望有单独的静态映射,根据名称的第一个字母访问。 Like
喜欢
public class FirstNameList {
private static Map<Integer, String> a = new HashMap<Integer, String>();
private static Map<Integer, String> b = new HashMap<Integer, String>();
private static Map<Integer, String> c = new HashMap<Integer, String>();
// etc
public void addFName(String word) {
if (word.length() == 0)
throw new IllegalArgumentException("No name entered");
word = word.toLowerCase();
char x = word.charAt(0);
Integer i = word.hashCode();
x-correctMap.put(i, word);
}
However it does not feel very efficient to have 26 if statements to pick the correct list. 但是,使用26个if语句来选择正确的列表并不是非常有效。 Does anyone know a way to pick the correct map?
有没有人知道如何选择正确的地图? Or just better ideas in general?
或者只是更好的想法?
I have three suggestions: the first is a direct answer to your question: 我有三个建议:第一个是直接回答你的问题:
First, you could store your maps in a map with the initial character as a key, then simply look up the correct map in the "master map." 首先,您可以将地图存储在地图中,并将初始字符作为关键字,然后只需在“主地图”中查找正确的地图。
Second, this won't be any faster -- likely slower -- than simply storing everything in a single map. 其次,这不会比简单地将所有内容存储在单个地图中更快 - 可能更慢。 Never assume you're more clever than the authors of a library class without measurements to prove there's a problem.
如果没有测量来证明存在问题,千万不要假设你比图书馆类的作者更聪明。
Third, you said "database", so why not use a database instead of all this? 第三,你说“数据库”,为什么不使用数据库而不是所有这些呢? Postgres and MySQL are both free and easy to use, and will serve your needs well.
Postgres和MySQL都是免费且易于使用的,可以很好地满足您的需求。
你可以嵌套你的地图,第一张地图的关键是字母。
You could have a list of maps, but you likely don't want to use a map at all. 你可以有一个地图列表,但你可能根本不想使用地图。 Something like TreeMap or a trie would work better, and allow you to have a single data structure.
像TreeMap或trie这样的东西可以更好地工作,并允许您拥有单一的数据结构。
Also, taking the hashcode of a string and then hashing is likely to be no faster than just using a String. 此外,获取字符串的哈希码然后散列可能不会比使用字符串更快。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.