【发布时间】:2014-01-11 21:18:01
【问题描述】:
我为我的学校编写了一个 Android 应用程序,它生成一个 HashMap,它将课程名称映射到该课程可用部分的 ArrayList(字符串)。使用JSoup生成地图,连接学校网站,获取当前所有课程信息,解析格式化,创建HashMap>()。
它有效。但是,在 Android 设备上生成 HashMap 实际上需要大约 5 分钟。我是编程方面的新手,我想知道是否还有其他更有效的方法来存储和处理如此大量的数据(HashMap 映射到大约 800 个 ArrayList,每个 ArrayList 又包含几个字符串)。理想情况下,每次应用程序运行时数据都会更新,所以我不确定写入内部存储是否有效。
有什么建议吗?
谢谢
编辑:这是创建 HashMap 的方法。这有点令人费解,但我从中提取数据的网站并不容易使用。
public HashMap<String, ArrayList<String>> generateCourseSectionMap()
{
ArrayList<String> store = new ArrayList<String>();
CourseLinks courses = new CourseLinks();
HashMap<String, String> courseLinks = courses.getCourseMap();
StringUtils util = new StringUtils();
HashMap<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();
String sectionFormat = "((\\d){5};(\\d)+;(.*?) \\((.*?)\\);(.*?);(\\d)+ \\/ (\\d)+;(.*?);(TBA|Mo|Tu|We|Th|Fr|Sa|Su)+( (\\d){1,2}:(\\d){2}(AM|PM) - (\\d){1,2}:(\\d){2}(AM|PM))*?;(.*?));";
Document doc;
try
{
for (Map.Entry<String, String> entry : courseLinks.entrySet())
{
doc = Jsoup.connect(entry.getValue()).get();
Elements links = doc.select("*+tr>*:not(tr[class~=SectionTopic.*]>*):not(tr[class~=SectionTitle.*]>*)");
if (!links.isEmpty())
links.remove(0);
String build = "";
for (Element e : links)
{
String s = util.trim(e.text());
if (!s.isEmpty())
build = build + s + ";";
}
String rebuilt = rebuild(build);
store = util.toArrayList(rebuilt.split("BREAK"));
for (String d : store)
{
Pattern p = Pattern.compile(sectionFormat, Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
Matcher m = p.matcher(d);
String[] array = d.split(";");
String firstKey = d.substring(0, d.indexOf(";"));
ArrayList<String> sectionList = new ArrayList<String>();
while (m.find())
sectionList.add(array[0] + ";" + array[1] + ";" + m.group());
map.put(firstKey, sectionList);
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
return map;
}
【问题讨论】:
-
确实有很多时间。您能否提供有关如何生成地图的更多详细信息?来源、基本算法、数组列表中的字符串长度...
-
我猜大部分时间都花在了数据传输和解析上。 HashMap 中的 800 个条目应该很快。
-
我用我正在使用的方法更新了问题。
-
地图中的 800 个条目并不是“大量数据”。这没什么。它实际上应该是瞬时的。你找错地方了。
-
doc = Jsoup.connect(entry.getValue()).get();这会建立新的连接吗?
标签: java android performance jsoup